| View previous topic :: View next topic |
| Author |
Message |
Blazin Grandmaster Cheater
Reputation: 0
Joined: 17 Sep 2006 Posts: 770 Location: Somewhere over the rainbow.
|
Posted: Sun Mar 30, 2008 5:22 pm Post subject: Won't Compile? C++ program |
|
|
I'm trying to compile another program, and when i do it gives me the error that there is an expected '}' at the end of input. However, everything that should be is there. I'm trying to write this program to practice C++, I'm just learning it. Why is it giving me that error?
| Code: | // This is a program to find out how many calories a certain
// person are
// egg = 100 calories
// Pancakes = 200 calories
// chocolate = 300 calories
// apple = 50 clories
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
string nNamePerson;
cout << "What is the name of the person who's calorie intake you are"
<< " calculating?\nDon't enter negative values.";
cin >> nNamePerson;
int nLoop1 = 1;
while (nLoop1-- > 0)
{
int nEgg;
cout << "\nHow many eggs did they consume?";
cin >> nEgg;
if (nEgg < 0)
{
cout << "Error-Cannot enter negative values.";
continue;
}
int nPancakes;
cout << "\nHow many pancakes did they consume?";
cin >> nPancakes;
int nChocolate;
cout << "\nHow many chocolates did they consume?";
cin >> nChocolate;
int nApple;
cout << "\nHow many apples did they consume?";
cin >> nApple;
int nEggCal;
nEggCal = nEgg * 100;
int nPancakeCal;
nPancakeCal = nPancakes * 200;
int nChocCal;
nChocCal = nChocolate * 300;
int nAppleCal;
nAppleCal = nApple * 50;
int nCalTotal;
nCalTotal = nEggCal + nPancakeCal + nChocCal + nAppleCal;
cout << "\nThe total calories " << nNamePerson << " consumed is: "
<< nCalTotal;
system("PAUSE");
return 0;
} |
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sun Mar 30, 2008 5:25 pm Post subject: |
|
|
You didnt close your While Loop....
_________________
|
|
| Back to top |
|
 |
Blazin Grandmaster Cheater
Reputation: 0
Joined: 17 Sep 2006 Posts: 770 Location: Somewhere over the rainbow.
|
Posted: Sun Mar 30, 2008 5:26 pm Post subject: |
|
|
| lurc wrote: | | You didnt close your While Loop.... |
When i try to close it, it tells me I didn't declare what nEgg was, and I obviously did. Any other solutions?
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sun Mar 30, 2008 5:29 pm Post subject: |
|
|
Put int nEgg outside the loop, and close it.
Edit: Actually, i'd just declare all variables outside the loop.
_________________
|
|
| Back to top |
|
 |
Blazin Grandmaster Cheater
Reputation: 0
Joined: 17 Sep 2006 Posts: 770 Location: Somewhere over the rainbow.
|
Posted: Sun Mar 30, 2008 5:41 pm Post subject: |
|
|
| lurc wrote: | Put int nEgg outside the loop, and close it.
Edit: Actually, i'd just declare all variables outside the loop. |
Thanks, it compiles now, but where i put the continue function, it doesn't start at the top of the loop, it just continues. Why is that?
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sun Mar 30, 2008 6:03 pm Post subject: |
|
|
replace the check with a do while loop, like this:
| Code: | do
{
cout << "\nHow many eggs did they consume\? (No Negative Numbers)";
cin >> nEgg;
}
while ( nEgg < 0 ); |
_________________
|
|
| Back to top |
|
 |
Blazin Grandmaster Cheater
Reputation: 0
Joined: 17 Sep 2006 Posts: 770 Location: Somewhere over the rainbow.
|
Posted: Sun Mar 30, 2008 6:10 pm Post subject: |
|
|
| lurc wrote: | replace the check with a do while loop, like this:
| Code: | do
{
cout << "\nHow many eggs did they consume\? (No Negative Numbers)";
cin >> nEgg;
}
while ( nEgg < 0 ); |
|
Thanks, it worked. I haven't learned about the do loop yet, but I'm sure I will. Thanks. +Rep.
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sun Mar 30, 2008 6:33 pm Post subject: |
|
|
do-while loop is basically the same as the while loop, the only difference is that the do while loop runs through once before checking the argument that was stated in the while( )
_________________
|
|
| Back to top |
|
 |
|