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.
Learning C++, wrote a little thingy.
Started by Katzewurzt, Dec 09 2006 08:18 PM
6 replies to this topic
#1
Posted 09 December 2006 - 08:18 PM
#2
Posted 09 December 2006 - 08: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();
}
#3
Posted 09 December 2006 - 08: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 ^^
Oh and by the way, I didn't forget the "#" , I just missed it when copying it ^^
#4
Posted 09 December 2006 - 09:25 PM
Katzewurzt said:
I also tried
... cout<<"This equals to "<<multiply*nrOne+nrTwo+nrThree<<"."; ...
But then it would just multiply the first number and ignore the rest
... 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
#5
Posted 09 December 2006 - 09:39 PM
Oh thank you Geon, taught me a little something there, I'll remember that ^^
#6
Posted 11 December 2006 - 05:21 PM
Also, I recommend using
#include <iostream>instead of
#include <iostream.h>
#7
Posted 13 December 2006 - 03:13 PM
c++ ebooks== "http://www.flazx.com/category2.php"
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


This topic is locked









