last line error, URGENT

This is a discussion on last line error, URGENT within the C++ Programming forums, part of the General Programming Boards category; 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> ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    10

    Exclamation 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

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    You are using do{} incorrectly. In C/C++, the only usage for do is the following:
    Code:
    do
    {
    
        // some code here
    
    } while (cond);

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    10
    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~
    Last edited by deng17; 03-25-2007 at 06:25 AM.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    10
    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.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    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'm just trying to be a better person - My Name Is Earl

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    10
    I have fixed it and it's running. Thanks.

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Did you remove the goto statements you stated you put in?
    I'm just trying to be a better person - My Name Is Earl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file line by line
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 03-19-2009, 12:44 AM
  2. Pointer and Polymorphism help.
    By Skyy in forum C++ Programming
    Replies: 29
    Last Post: 12-18-2008, 09:17 PM
  3. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  4. Finding carriage returns (\c) in a line
    By JizJizJiz in forum C++ Programming
    Replies: 37
    Last Post: 07-19-2006, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21