PDA

View Full Version : Learning C++, wrote a little thingy.


Katzewurzt
12-09-2006, 12:18 PM
I'm learning C++ and I wrote this thingy here (nothing big:P), which is not working... I would like some experienced dudeling maybe, tell me what I did wrong.
I'm using Bloodshed's Dev-C++.


include <iostream.h>

using namespace std;

int main()
{
int multiply;
long int nrOne, nrTwo, nrThree;
long int addition = nrOne+nrTwo+nrThree;

cout<<"First enter one number to multiply three others.";
cin>>multiply;
cin.get();
cout<<"Now enter three numbers to be added, then multiplied by the first.";
cin>>nrOne>>nrTwo>>nrThree;
cin.get();
cout<<"This equals to "<<multiply*addition<<".";
cin.get();
}

I also tried
... cout<<"This equals to "<<multiply*nrOne+nrTwo+nrThree<<"."; ...
But then it would just multiply the first number and ignore the rest, so:
3 x 2 + 2 + 2 = 6 + 4 = 10 ... when it Should've been 3 x 2 + 2 + 2 = 18.

kariem2k
12-09-2006, 12:40 PM
#include <iostream.h> //<----You forgot the sharp sign.

using namespace std;

int main()
{
long int multiply=0; //<-You should initialize variables (Optional,but it is critical for some cases).
long int nrOne=0, nrTwo=0, nrThree=0; //<-You should initialize variables.
cout<<"First enter one number to multiply three others.";
cin>>multiply;
cin.get();
cout<<"Now enter three numbers to be added, then multiplied by the first.";
cin>>nrOne>>nrTwo>>nrThree; //<----------You take the numbers first
int addition = nrOne+nrTwo+nrThree; //<------------Then make the sum on them :)
cin.get();
cout<<"This equals to "<<multiply*addition<<".";
cin.get();
}

Katzewurzt
12-09-2006, 12:47 PM
Oh thank you dear ... thingy. Now I know that, and I can continue practicing with these kind of stuff. And glad you didn't call me a nub posting these things of no importance. :happy:

Oh and by the way, I didn't forget the "#" , I just missed it when copying it ^^

geon
12-09-2006, 01:25 PM
I also tried
... cout<<"This equals to "<<multiply*nrOne+nrTwo+nrThree<<"."; ...
But then it would just multiply the first number and ignore the rest

That is called "operator precedence". It is specified very carefully in the language. If you want to change the way the numbers are multiplied/added, use parentheses:

a*(b+c+d)

instead of:

a*b+c+d

Wich would be the same as:

(a*b)+c+d

Katzewurzt
12-09-2006, 01:39 PM
Oh thank you Geon, taught me a little something there, I'll remember that ^^

EvilSmile
12-11-2006, 09:21 AM
Also, I recommend using #include <iostream> instead of #include <iostream.h>

i_robot
12-13-2006, 07:13 AM
c++ ebooks== "http://www.flazx.com/category2.php"