| View previous topic :: View next topic |
| Author |
Message |
bucky13 Cheater
Reputation: 0
Joined: 07 Dec 2007 Posts: 43
|
Posted: Mon Sep 15, 2008 9:25 pm Post subject: c++ coding help |
|
|
ok im trying to self teach my self c++ but i have run into a problem i need to subract 2 strings to make another string heres the code.
| Code: | // first practice game.
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
int pies, ate, fed, remaining;
string leader;
cout << "enter a number from 2 to 1,000,000,000: ";
cin >> pies;
cout << "you have " << pies << " pies" << endl;
cout << "enter a smaller number than " << pies << endl;
cin >> ate;
cout << "you ate " << ate << " pies" << " you now have " << pies - ate = remaining << endl;
cout << "now enter a number smaller than " << remaining << endl;
cout << "press enter 2 times to exit" << endl;
cin.ignore(cin.rdbuf()->in_avail() + 3);
return 1;
} |
|
|
| Back to top |
|
 |
xV I post too much
Reputation: 1
Joined: 03 Jan 2008 Posts: 3783 Location: Seattle
|
Posted: Mon Sep 15, 2008 9:56 pm Post subject: |
|
|
Remove all that using Namespace garbage.
| Code: |
using namespace std; |
(pies)-(ate)
lol
_________________
|
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Mon Sep 15, 2008 10:00 pm Post subject: |
|
|
You need to learn about different data types. declare them in as int instead of string.
| Code: | | cout << "you ate " << ate << " pies" << " you now have " << pies - ate = remaining << endl; |
You can't store expressions like that in another command. and you've also got it backwards.
| Code: | remaining = pies - ate
cout << "you ate " << ate << " pies" << " you now have " << remaining << endl; |
The reason for tutorials is not ending up stuck like this. Pick a tutorial and follow it, atleast until you get a strong command of the language. then, and only then can you go off and do whatever you want. Answering questions like this that could be solved by looking at a tutorial will get old fast.
_________________
|
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Mon Sep 15, 2008 10:02 pm Post subject: |
|
|
| Code: | // first practice game.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int pies, ate, fed, remaining;
string leader;
cout << "enter a number from 2 to 1,000,000,000: ";
cin >> pies;
cout << "you have " << pies << " pies" << endl;
cout << "enter a smaller number than " << pies << endl;
cin >> ate;
remaining = pies -ate;
cout << "you ate " << ate << " pies" << " you now have " << remaining << endl;
cout << "now enter a number smaller than " << remaining << endl;
// you didnt put a cin >> to take in a smaller number than remaining here
cout << "press enter 2 times to exit" << endl;
cin.ignore(cin.rdbuf()->in_avail() + 3); // uhm....wtf? what are you trying to do here?
return 0;
} |
also, you made no attempt to use the string "leader", so why declare it?
_________________
|
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Mon Sep 15, 2008 10:06 pm Post subject: |
|
|
@vibes
The problem with including the entire namespace is that it bloats the executable. bucky is doing the correct thing in setting up only the few functions that he uses.
_________________
|
|
| Back to top |
|
 |
bucky13 Cheater
Reputation: 0
Joined: 07 Dec 2007 Posts: 43
|
Posted: Tue Sep 16, 2008 12:01 am Post subject: |
|
|
thank you for the help
|
|
| Back to top |
|
 |
|