| View previous topic :: View next topic |
| Author |
Message |
MegaForum Grandmaster Cheater
Reputation: 0
Joined: 20 Aug 2007 Posts: 558
|
Posted: Mon Jan 21, 2008 6:47 pm Post subject: C++ query. |
|
|
Im making a simple addition program which takes 2 intergers and add them up. My code is very noob so flame away lol.
| Code: |
#include <iostream>
#include <stdlib.h>
int main()
{
cout << "Enter Firstnumber" endl;
int Firstnumer = a
cout << "Enter Secondnumber" endl;
int Secondnumber= b;
cin >> "Firstnumber + Secondnumber " end1;
system("PAUSE");
return 0;
}
|
Well can anyone tell me what im doing wrong. I have a book that i got for christmas, and i already know cin/cout commands, storing intergers/variables like so. but when it comes to coding im not sure what order to actually write it in.
PLEASE, DON'T WRITE WHAT THE CODE SHOULD BE Just tell me what im doing wrong. i wanna write ti for myself without using someone else's code .
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Mon Jan 21, 2008 7:03 pm Post subject: |
|
|
Since you don't want answers, I will give hints.
| Code: | | cout << "Enter Firstnumber" endl; |
You need another "<<" somewhere in there, guess.
Also, if you don't put "using namespace std;" at the beginning, you must prefix each cout/cin/endl/etc with "std::".
| Code: | cout << "Enter Firstnumber" endl;
int Firstnumer = a |
That is wrong, it won't store the input in "Firstnumber".
HINT: Use cin.
| Code: | | cin >> "Firstnumber + Secondnumber " end1; |
What the hell is that?
HINT: Its just wrong, so horribly wrong.
PROTIP: "I am a string!"
No.
|
|
| Back to top |
|
 |
MegaForum Grandmaster Cheater
Reputation: 0
Joined: 20 Aug 2007 Posts: 558
|
Posted: Mon Jan 21, 2008 7:31 pm Post subject: |
|
|
| Flyte wrote: | Since you don't want answers, I will give hints.
| Code: | | cout << "Enter Firstnumber" endl; |
You need another "<<" somewhere in there, guess.
Also, if you don't put "using namespace std;" at the beginning, you must prefix each cout/cin/endl/etc with "std::".
| Code: | cout << "Enter Firstnumber" endl;
int Firstnumer = a |
That is wrong, it won't store the input in "Firstnumber".
HINT: Use cin.
| Code: | | cin >> "Firstnumber + Secondnumber " end1; |
What the hell is that?
HINT: Its just wrong, so horribly wrong.
PROTIP: "I am a string!"
No. |
Ty for ripping my program to shreds . i know it was crap anyways.
for | Code: | | cout << "Enter Firstnumber" endl; |
the other << goes at the end right?
| Code: | | cout << "Enter Firstnumber" << endl; |
then the beginning of the code should start
#include <iostream>
#include <stdlib.h>
int main()
using namespace std;
| Code: |
umm for storing firstnumber im kinda lost on that. lol
and
isn't
system("PAUSE")
required so that when it shows the sum of firstnumber + secondnumber
it won't close, it'll pause and show the result?
|
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Mon Jan 21, 2008 7:47 pm Post subject: |
|
|
cin is supposed to assign a variable a value. what you are assigning is a string
cin is used to gain a users input
system("pause") only suspends the command line until a user presses a key. but yes, it lets you see what is printed. not the best way to do so tho.
i recommend _getch(); (Hint: you have to include another header library for this function)
Hint: The "=" sign is an Assignment operator. (Assigning the Declared Variable a Value)
Hint2: "a" and "b" are not declared as integers, "firstNumber" and "secondNumber" are.
Hint3: You can get a user's input at anytime during the code, doesnt have to be after both cout's
Hint4: To store user input you must declare a space for that variable (firstNumber, and secondNumber are already declared as integers and u can use them to store the users input )
to end this lets just make this straight
cout - Output ( << )
cin - Input ( >> )
_________________
|
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Mon Jan 21, 2008 7:55 pm Post subject: |
|
|
-You can keep int Firstnumer if you do the same with a and use cin. Same goes for Secondnumber.
-You can get rid of one of the #include
- cin doesn't take "" or endl
- cin can take multi variables but it has to be separated by >> not +
system("pause"); will keep it from closing, it's ok, but it isn't the preferred method. The best way is to just return 0 and open it up in command prompt. If you are running vs you can just press ctrl + f5. That will keep it open if you just have a return.
|
|
| Back to top |
|
 |
MegaForum Grandmaster Cheater
Reputation: 0
Joined: 20 Aug 2007 Posts: 558
|
Posted: Mon Jan 21, 2008 8:04 pm Post subject: |
|
|
| lurc wrote: | cin is supposed to assign a variable a value. what you are assigning is a string
cin is used to gain a users input
system("pause") only suspends the command line until a user presses a key. but yes, it lets you see what is printed. not the best way to do so tho.
i recommend _getch(); (Hint: you have to include another header library for this function)
Hint: The "=" sign is an Assignment operator. (Assigning the Declared Variable a Value)
Hint2: "a" and "b" are not declared as integers, "firstNumber" and "secondNumber" are.
Hint3: You can get a user's input at anytime during the code, doesnt have to be after both cout's
Hint4: To store user input you must declare a space for that variable (firstNumber, and secondNumber are already declared as integers and u can use them to store the users input )
to end this lets just make this straight
cout - Output ( << )
cin - Input ( >> ) |
Can you psot like a little code to tell it to sotre the users input as firstnumber and secondnumber... idk how to do that lol.
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Mon Jan 21, 2008 8:34 pm Post subject: |
|
|
| Code: | | cin >> firstNumber; |
that will store the users inputted number to the variable "firstNumber" (earlier declared as an integer)
| Code: | | cout << firstNumber; |
that will display the value of firstNumber ( if it has one yet, u'll have to assign the value using cin before the output of its value )
_________________
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Mon Jan 21, 2008 8:41 pm Post subject: |
|
|
I recommend Google'ing for a C++ tutorial for a compiler-specific tutorial, like Microsoft VC++; usually books are a bit out-dated nowadays.
_________________
|
|
| Back to top |
|
 |
Cx Master Cheater
Reputation: 0
Joined: 27 Jul 2007 Posts: 367
|
Posted: Mon Jan 21, 2008 8:42 pm Post subject: |
|
|
| Code: | #include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "\nEnter second number: ";
cin >> num2;
cout << "\n" << num1 << " + " << num2 << " = " << num1+num2 << endl;
cin.ignore();
cin.get();
return 0;
} |
Last edited by Cx on Mon Jan 21, 2008 9:28 pm; edited 1 time in total |
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Mon Jan 21, 2008 8:53 pm Post subject: |
|
|
| cstdlib, not stdlib.h
|
|
| Back to top |
|
 |
XxOsirisxX Grandmaster Cheater Supreme
Reputation: 0
Joined: 30 Oct 2006 Posts: 1597
|
Posted: Mon Jan 21, 2008 9:22 pm Post subject: |
|
|
| Code: | | "Firstnumber + Secondnumber" |
Don't use " " to declare variables. Else, it will be used as String.
| Code: | cout << "Enter Firstnumber" endl;
int Firstnumer = a
cout << "Enter Secondnumber" endl;
int Secondnumber= b; |
Is recommed to declare variables at the "start", it's a lot better to locate.
_________________
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Jan 22, 2008 8:26 am Post subject: |
|
|
| Cx wrote: | | Code: | #include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "\nEnter second number: ";
cin >> num2;
cout << "\n" << num1 << " + " << num2 << " = " << num1+num2 << endl;
cin.ignore();
cin.get();
return 0;
} |
| What's the #include <cstdlib> for? Oh well.. It's the thread starter's fault.
What if I type a char? What if my integer is too big to fit in int? What if num1+num2 is too big? :)
I bet you meant std::cin.sync(), not std::cin.get()?
0 isn't always EXIT_SUCCESS.
|
|
| Back to top |
|
 |
MegaForum Grandmaster Cheater
Reputation: 0
Joined: 20 Aug 2007 Posts: 558
|
Posted: Tue Jan 22, 2008 1:38 pm Post subject: |
|
|
| appalsap wrote: | | cstdlib, not stdlib.h |
according to my book, C++ for dummies , its stdlib.h
|
|
| Back to top |
|
 |
mer0x Advanced Cheater
Reputation: 0
Joined: 06 Jan 2008 Posts: 63
|
Posted: Tue Jan 22, 2008 5:08 pm Post subject: |
|
|
| Code: |
#include <iostream>
using namespace std;
void main()
{
int a,b;
int c = a + b;
printf("Enter first number");
cin >> a;
printf("Enter second number");
cin >> b;
printf("Result: %s",c);
gets();
}
|
|
|
| Back to top |
|
 |
Cx Master Cheater
Reputation: 0
Joined: 27 Jul 2007 Posts: 367
|
Posted: Tue Jan 22, 2008 8:44 pm Post subject: |
|
|
Haha.
Funny how constructive people can be (and correctly so) about a simple console number addition program.
|
|
| Back to top |
|
 |
|