C/C++ Guide to C++ Programming For Beginners

Click For Summary
The discussion focuses on a beginner's guide to C++ programming, emphasizing the importance of selecting a suitable integrated development environment (IDE), such as Bloodshed’s Dev-C++, for compiling and debugging code. Participants express appreciation for the tutorial's structure and suggest including deeper explanations of key concepts like conditional statements, iterative statements, and pointers. There is a request for clearer code examples to enhance understanding, particularly regarding function definitions and the use of header files. The conversation also touches on the potential for future tutorials on GUI toolkits and advanced topics. Overall, the thread serves as a collaborative platform for sharing insights and improving the learning experience in C++.
  • #61
Example

Number = 65536

log(65536)/log(2) = 16 bits = (16/8) bytes = 2 bytes

A 16-bit (2 byte) number ranges from 0 to 65535, or -32768 to 32767 depending if its signed or unsigned. To hold 65536 would require 17 bits.
 
Technology news on Phys.org
  • #62
Maybe I'm in the wrong place, but what the heck. In the basic tutorial, you mention that the int is usually 4 bytes. I don't know if it's something you want to get into, but it is compiler-dependent. I had occasion to use the Borland compiler a few years ago, and ints were all over the place. Sometimes they were longs, sometimes they were shorts, and I never did find a pattern to it. I finally gave up declaring ints at all and went straight to longs and shorts.

For what it's worth...
 
  • #63
The same problem obtains in a 64 bit environment where longs are 64 bits.
The Borland compilers are 16 bit, which is what MS-DOS and consumer Windows (Win95, Win98, WinME) are built on top of.
 
  • #64
I finally finished the basic c++ tutorial. If your interested in a particular topic in programming let me know and i'll see if I can accommodate you.
 
  • #65
dduardo, it was a really good tutorial. Apart from the basics what else do you think would be good to know to solve problems using c++; such as for the acm programming contest? Do you plan to have an intermediate tutorial?
 
  • #66
For the intermediate I'll do pointers, strings, basic classes, structures and file io.

Then for advanced I'll do overloading, polymorphism, virtual and templates.

I could also do a tutorial just on alorithms: sorting, trees, heaps, queues, stacks, etc.

I could also do special topics like posix threads, interprocess communication (IPC), x86 asm, etc.

In terms of ACM programming you would want a tutorial on dynamic programming. I know when I did ACM that was a big topic.

Which one do you want?
 
  • #67
How about a topic covering math functions, Stuff like trigonometry and so on?
 
  • #68
I could add math functions to intermediate and dynamic memory allocation to advanced.
 
  • #69
Without putting too much pressure/workload on you dduardo :-p, i would like to see as much included as possible to make the tutorial show most sides of c++ programming and the different aspects of it.

My suggestion is to try and cover as much as possible as clearly as possible as you have done so far. I like the fact that the posts are not several pages long but gives a more-than-basic idea about the specific topic and it is up to the reader to go and try for themselves a bit instead of reading every program code out of a book.

Great tutorial:biggrin:
 
  • #70
C++ using For statement

This is my first time using a C++ program...actually any programs. my C++ programming class is a lot harder than my physics and math classes. =(
i've never struggled so much so i would REALLY REALLY appreciate as much help as possible. here is my problem:

Write a program that asks the user how many numbers will be entered and then has the user enter those numbers. When this is done, report to the user the position of the first 7 entered and the last 7 entered. By position we mean, for example, that if the first 7 is the 2nd number entered then its position would be 2.

Sample screen output 1:

How many numbers will be entered? 8
Enter num: 5
Enter num: 7
Enter num: 6
Enter num: 7
Enter num: 7
Enter num: 3
Enter num: 8
Enter num: 6
The first 7 was in position 2
The last 7 was in position 5


Sample screen output 2:

How many numbers will be entered? 8
Enter num: 5
Enter num: 2
Enter num: 6
Enter num: 7
Enter num: 1
Enter num: 3
Enter num: 8
Enter num: 6
The first 7 was in position 4
The last 7 was in position 4


Sample screen output 3:


How many numbers will be entered? 8
Enter num: 5
Enter num: 1
Enter num: 6
Enter num: 5
Enter num: 9
Enter num: 3
Enter num: 8
Enter num: 6
Sorry, no sevens were entered.


All i know is that for the counter controlled loop, I use the For loop and include the if-else statement inside the loop. I just can't get the problem to tell me the positions for both 7s.
thanks again.
 
  • #71
if this was a wrong place to put my question, can you tell me where? Thanks
 
  • #72
I will do it by keeping two variables,
1. min_pos - will hold the position of first 7
2. max_pos - will hold the position of last 7

Steps :
1. Initialise min_pos and max_pos to -1
2. Whenever a 7 is read,
if min_pos = -1, update min_pos = counter
if counter > max_pos, update max_pos = counter
3. if both min_pos = -1, output no 7 was read
otherwise output min_pos and max_pos

Once you are done with the program, read the program carefully. Try to understand how the value of -1 was being used here. Such techniques are commonplace in many programs, so it should help you out.

-- AI
 
  • #73
I need help with tutorial.I'am working a assigment 1.I enterd this in the Bloodshed Dev C++.
Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
    char myCharacter = 'z';
    cout << "The datatype is" << sizeof(int) << " bytes!" << endl;
    cout << " the varible has a value of " << myCharacter << endl;
    return 0;

}
It works kind of.For some starge reason the box pops up but it closes as soon as it opens.
 
  • #74
scott1, you can either run the application directly from thecommand line or add the following:

This should go at the top:

#include <cstdlib>

and this should go right before the return line:

system("PAUSE");
 
  • #75
It works now.Thanks
 
  • #76
I'am working on the Pythegrom therom assigment.I need help
Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
  int a = 'a', b = 'b', c = 'c';
     cout << "Enter Length of A" << endl;
     cin >>  a;
     cout << "enter length of b" << endl;
     cin >> b;
  a * a * b * b == c * c
   cout  << "c =" <<  c << endl;
    system("PAUSE");
    return EXIT_SUCCESS
It says there somthing worng with line 13 and 4 things with line 15 but I think there's probally more stuff worng with it then that.
 
  • #77
Why are you doing a = 'a', b='b', c='c'?

Why don't you just do: double a, b, c;

Then the pythagorean theorem is simply c = sqrt(a*a+b*b) ;

remember to #include <cmath> for the sqrt function.
 
  • #78
It worked thanks
 
  • #79
Is there ever going to be another more advance tutoril
 
  • #80
I've don't this before and now I'm trying to do the hypotenuse thing again...here's what I've got:

Code:
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

int main(int argc, char *argv[])
{
   double a, b, c;
   
   a = sqrt(b*b+c*c);
   
   cin >> b >> c;
   cout << a;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

every time I run it a is equal to 0 EDIT: I'm stupid, I got it..
 
  • #81
I the "cin" needs to go before the equation.
 
  • #82
some problems about matrix class

I have an exercise of building matrix class with characters: plus, subtract, divide, convert. Can you help me?:confused:
 
  • #83
I suppose I may have posted this out of place, but it seemed to go beyond the tutorial. Here it is again.

I've been trying for days to get my boyfriend's C++ program to work, and I am almost ready to concede defeat, but I have frequented this site in the past as a lurker, actually learning a lot of what I know from the tutorials (thanks!).

Now, I have a problem I just can't seem to solve.

The object is to have the user pick a choose from a menu and have that value stored somewhere to be used by a function later. Once that choice is picked, the user needs to be able to pick another choice, until he or she fills in all three choices and then it's on to the formula! I also need a choice for clearing the data (I know how to set it all to 0) and one for quitting the program (just return 0; right?), as well as a display of the data that has been entered already once the formula calculates (I have this block of code written too). My formula works, but I'm not sure how to get all the values stored or how to let the user go back and pick a choice from the list after he or she has chosen one already.

Here's what I have so far, and it's a work in progress, but hopefully it's on the right track. I'm thinking I need to, maybe, use a switch statement for the menu, but I can't get that to work for me, and I'm still not sure how to store a value that's not going to be immediately used (maybe something with an array, but again I seem unable to produce the right code).


#include <iostream>
#include <math.h>
using namespace std;

doublemortgage (int c, double b, double a)
{
int i = c;
double j = b;
double k = a;
double l = b/(12 * 100);
double m = c * 12;
double result = k * (l / (1 - pow(1+l, -m)));
return result;
}


int main ()
{
int c;
double b;
double a;
double result;
int x;

cout << "Mortgage Calculation Menu \n";
cout << "1. Enter a Loan Amount \n";
cout << "2. Enter a Loan Rate \n";
cout << "3. Enter a Term in Years \n";
cout << "4. Calculate a Payment \n";
cout << "5. Clear All Input \n";
cout << "9. Quit \n";
cout << "Enter Your Selection: ";
cin >> x;

cout << " \n";

cout << "Enter a Loan Amount: $";
cin >> a;
cout << "Enter a Loan Rate: ";
cin >> b;
cout << "Enter a Term in Years: ";
cin >> c;
cout << "Your Monthly Payment: $"<< mortgage(c, b, a) << endl;

cout << " \n";

cout << "You Entered" << endl;
cout << "Loan Amount: $" << a << endl;
cout << "Loan Rate: " << b << "%" << endl;
cout << "Loan Term: " << c << "years" << endl;
cout << "Monthly Payment: $" << mortgage (c, b, a) << endl;


return0;
}
 
  • #84
Is there a version of the getSubString function in c++? If so, what is it and what are the parameters?

-Thanks in advance
 
  • #85
alright, so I am an Electrical Engineering major, and my current project requires me to program...
im using Visual STudio 2005, and I am having a little trouble with just the basics...

here's my program so far:

#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>

int main
{
printf("heuy");
cout<< "hey";


return 0;

}

i run it, and all it gives me is a blank window...
it doesn't write anything...
can anyone help me?
 
  • #86
mmm ...did tha tcode actually compile?
my guess would be that your main call is missing something.

if it closes to fast put a pause statement like delay, getch, getchar.
 
  • #87
hiiiiiii(mamboooooooooooooooooo)

I hope some of you will not understand may be the language i used in the brackets but are the swahili word so don't worry about it but what i do want to ask from your my friends is just the sammary of c programming language because am still learner of that language but also u can ask me about pascal programming language because somehow iam well on it i hope you will accept my request
 
  • #88
hi my friends
i am a new in this side and i wana your help as fast as you can pleaaaaaaaas>>

the "q" is how to write the function in c++ if
x1=6 ,f(x)=x^2-4 and f'(x)=2x by using Newton raphson method
and the next estimate of the root:
xk+1 = xk - f(xk) / f'(xk)

and the answer should be if i enter the (0.1):

- Enter precision (1/10, 1/100, 1/1000,...): 0.1

xk f(x) derived_f(xk) xk+1 dx
6.000 32.000 12.000 3.333 2.667
3.333 7.111 6.667 2.267 1.067
2.267 1.138 4.533 2.016 0.251
2.016 0.063 4.031 2.000 0.016


pleas don't be late>>>>>thanks
 
  • #89
which is better:
using c++ (graphics mode) or using Visual c++??
i'm sort of confused.
 
  • #90
Nuclear: graphics mode of what? opengl, directx, graphics.h??
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 27 ·
Replies
27
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
5
Views
7K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 13 ·
Replies
13
Views
6K