-
last line error, URGENT
I've been working for hours with this simple code(not for me). Here is the code:
Code:
#include <iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int main(int argc, char *argv[])
{
int cash=10000;
int guess;
int number;
int bet;
int prize=bet*2;
do
{
int clrscr();
play:
std::cout<<"Welcome to the Guess Game.'\n'You have $",cash," in hand.'\n'Place your bet.'\n'";
std::cin>>bet;
if(cash<bet)
{
std::cout<<"You do not have enough money. Please place a lower bet.";
continue;
}
else
{
cash=cash-bet;
std::cout<<"Bet: ",bet," Cash in hand: " ,cash;
}
std::cout<<"I will pick a random number from 0 to 20.";
std::cout<<"You must try to guess the number.";
int randomize();
number=(int)(rand()%20);
do
{
std::cout<<"What is your guess? (0 to 20) ";
std::cin>>guess;
if((guess<0)||(guess>20))
{
std::cout<<"Sorry, but your guess ",guess,"must be from 0 to 20.";
}
else if(guess < number)
{
std::cout<<"Wrong! ",guess," is low. I have picked ",number;
goto play;
}
else if(guess > number)
{
std::cout<<"Wrong! ",guess," is high. I have picked ",number;
goto play;
}
else
{
std::cout<<"Number ",guess," is correct. Congratulations!";
cash=cash+prize;
std::cout<<"You won $",prize," in hand. You now have $",cash;
goto play;
}
if(cash==0);
{
goto lost;
}
lost:
std::cout<<"Thank you for playing Guess Game.'\n'Press ENTER to quit.";
cin.get();
return 0;}
Here is what BloodShed displays on Error Log:
Code:
guessgame.cpp: In function `int main(int, char**)':
guessgame.cpp:70: error: expected `while' at end of input
guessgame.cpp:70: error: expected `(' at end of input
guessgame.cpp:70: error: expected primary-expression at end of input
guessgame.cpp:70: error: expected `)' at end of input
guessgame.cpp:70: error: expected `;' at end of input
guessgame.cpp:70: error: expected `}' at end of input
guessgame.cpp:70: error: expected `while' at end of input
guessgame.cpp:70: error: expected `(' at end of input
guessgame.cpp:70: error: expected primary-expression at end of input
guessgame.cpp:70: error: expected `)' at end of input
guessgame.cpp:70: error: expected `;' at end of input
guessgame.cpp:70: error: expected `}' at end of input
make.exe: *** [guessgame.o] Error 1
Execution terminated
-
You are using do{} incorrectly. In C/C++, the only usage for do is the following:
Code:
do
{
// some code here
} while (cond);
-
It's certainly a lesson in pressing "compile" as you go, rather than assuming that right at the end you can just dump the whole mess on a message board and hope someone will be bothered to figure out which brace you've missed from that poorly indented mess. :(
Start here
Code:
int main ( ) {
return 0;
}
Then add a few more declarations and statements, then compile again
Code:
int main ( ) {
int cash = 1000;
do {
std::cout << "How much to bet? ";
int bet;
cin >> bet;
cash -= bet;
} while ( cash > 0 );
std::cout << "You're broke" << std::endl;
return 0;
}
Repeat this process until your program is complete.
What should be immediately apparent is that braces, "" and ( ) never exist in an unmatched state for very long.
Eg.
for ( i = 0 ; i = 10 ; i++ ) {
}
I would typically type the characters
for ( ; ; ) {
}
first, then with a few cursor movements, type in the rest. Then I know the control structure is correct before I start to concentrate on the actual body.
If at any stage it doesn't compile then STOP adding new stuff and fix what you have so far.
It's also a good idea to press 'run' every few compiles to make sure the code is going to do what you want it to.
-
I have removed the do's and just replaced the code with goto's. Please look at this. Only one more error.
NO MORE ERRORS
~EDIT~
-
Ooops I have fixed it. I forgot to close one condition.
Okay I have compiled the code with Bloodshed Dev-C++ and it said "Done" but I can't see any exe's in the directory and when I run the code via Bloodshed it says "the program is not compiled". Weird... How to fix this??
Oh yeah how to compile the code with "cmd"??? I think I'll try this.
-
Try clicking "re-build" then compile and run to fix the problem
Why are you using GOTO to fix your loop problems? Goto should be avoided at all times except in exceptional cases like quitting from a deeply nested bunch of loops. You do not need to use goto at all in your program. Think about your structure and work around not using goto.
-
I have fixed it and it's running. Thanks.
-
Did you remove the goto statements you stated you put in?