Thread: random number program

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    31

    random number program

    Can someone tell me what I am doing wrong. I have so many errors I dont know where to begin.

    Code:
    #include <iostream.h>
    #include "random.h"
    
    int main()
    {
    	int die1, die2, dietotal, pointstaken, pointsrisked, userpoints=1000, temp, totalpoints;
    
    
    	cout <<"Welcome to the Dice Program.  Press 0 to exit or any other key to begin " <<;
    	cin >> temp;
    		if (temp == 0){
    			break;
    		else;
    			cout <<"You have " << userpoints << " points." << endl;
    			cout <<"How many points do you want to risk: " <<;
    			cin >> pointsrisked; //get risked points
    			die1 = 1 + random(5) //generate random number as dice roll
    			die2 = 1 + random(5) //generate random number as dice roll
    			dietotal= die1 + die2 //calculate dice total
    				if ((dietotal % 2) ==0){ // determines if dice total is even or odd number
    					totalpoints = (userpoints - pointsrisked);
    					userpoints = totalpoints;
    					cout << "You rolled a " << die1 << " and " << die2 endl;
    					cout << "You Lose" endl;
    					cout <<"You now have " << userpoints << " points." << endl;
    				else;
    					totalpoints = (userpoints +(pointsrisked * 2));
    					userpoints = totalpoints;
    					cout << "You rolled a " << die1 << " and " << die2 endl;
    					cout << "You Win" endl;
    					cout <<"You now have " << userpoints << " points." << endl;
    			}
    		}
    	return (0);
    }
    Here is the code from my random.h file

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    //--------RANDOM LIBRARY--------------
    //------------------------------------
    #if defined(_MSC_VER)
    #include <stdlib.h>
    #include <time.h>
    void randomize()
    {
    	 time_t t;
    	 srand((unsigned) time(&t));
    }
    //------------------------------------
    int random(int limit)
    {
    	 return rand()%limit;
    }
    #endif
    //------------------------------------
    //----END OF RANDOM LIBRARY-----------
    please help.

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Just glancing at your code I see a few things you might wanna consider:
    Code:
    #include <iostream.h> //--> <iostream>
    #include <stdlib.h> //--> <cstdlib>
    
    // add a 
    using namespace std; // after the #include's
    
    // use separate lines (it's just easier to look at)
    int die1, die2, dietotal, pointstaken, pointsrisked, userpoints=1000, temp, totalpoints;
    
    // remove the final <<
    cout <<"Welcome to the Dice Program.  Press 0 to exit or any other key to begin " <<;
    
    // this requires you to hit enter, but you only ask the user to "press any key"
    cin >> temp;
    
    // you open a block here, but only have a line of body
    // either remove the open brace or add a close brace
    // after the body 
    if (temp == 0){
      break;
    
    // don't terminate an else with a semicolon
    else;
    
    //a normal if..else block looks like this:
    if (foo) {
      //doSomething();
    }
    else {
      //doSomethingElse();
    }
    
    // you need to end these with semicolons
    die1 = 1 + random(5) //generate random number as dice roll
    die2 = 1 + random(5) //generate random number as dice roll
    dietotal= die1 + die2 //calculate dice total
    those are the compile errors I caught at a glance. I didn't look for logic errors.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    Well I am down to 5 errors now. I seem to have a problem with the break statement. And something with the first if statement.

    Code:
    #include <iostream.h>
    #include "random.h"
    
    int main()
    {
    	int die1, die2, dietotal, pointstaken, pointsrisked, userpoints=1000, temp, totalpoints;
    
    
    	cout <<"Welcome to the Dice Program.  Press 0 to exit or the enter key to begin " <<;
    	cin >> temp;
    	if (temp == 0){
    			break;
    		else
    			cout <<"You have " << userpoints << " points." << endl;
    			cout <<"How many points do you want to risk: " <<;
    			cin >> pointsrisked; //get risked points
    			die1 = 1 + random(5); //generate random number as dice roll
    			die2 = 1 + random(5); //generate random number as dice roll
    			dietotal= die1 + die2; //calculate dice total
    				if ((dietotal % 2) ==0){ // determines if dice total is even or odd number
    					totalpoints = (userpoints - pointsrisked);
    					userpoints = totalpoints;
    					cout << "You rolled a " << die1 << " and " << die2 << endl;
    					cout << "You Lose" << endl;
    					cout << "You now have " << userpoints << " points." << endl;
    				else
    					totalpoints = (userpoints +(pointsrisked * 2));
    					userpoints = totalpoints;
    					cout << "You rolled a " << die1 << " and " << die2 << endl;
    					cout << "You Win" << endl;
    					cout << "You now have " << userpoints << " points." << endl;
    				}
    	}
    	return (0);
    }

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    dude, read my previous post. I explained how to do an if-else block.

    your block looks like this:
    Code:
    if (temp == 0){
      break;
    else
      //...
    }
    but it should look like this:
    Code:
    if (temp == 0) {
      break;
    }
    else {
      //...
    }
    but most simplified like this:
    Code:
    if (temp == 0)
      break;
    else {
      //...
    }

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    I made more changes and now I am down to just 2 errors. An error still in the break line somewhere and one in the second else line. It says illegal else without if so I am guessing it has something to do with my braces. Thanks for all the help so far.

    Code:
    #include <iostream.h>
    #include "random.h"
    
    int main()
    {
    	int die1, die2, dietotal, pointstaken, pointsrisked, userpoints=1000, temp, totalpoints;
    
    
    	cout << "Welcome to the Dice Program.  Press 0 to exit or the enter key to begin " << endl;
    	cin >> temp;
    		if (temp == 0)
    			break;
    		else {
    			cout <<"You have " << userpoints << " points." << endl;
    			cout <<"How many points do you want to risk: " << endl;
    			cin >> pointsrisked; //get risked points
    			die1 = 1 + random(5); //generate random number as dice roll
    			die2 = 1 + random(5); //generate random number as dice roll
    			dietotal= die1 + die2; //calculate dice total
    		}
    				if ((dietotal % 2) ==0) // determines if dice total is even or odd number
    					totalpoints = (userpoints - pointsrisked);
    					userpoints = totalpoints;
    					cout << "You rolled a " << die1 << " and " << die2 << endl;
    					cout << "You Lose" << endl;
    					cout << "You now have " << userpoints << " points." << endl;
    				else {
    					totalpoints = (userpoints +(pointsrisked * 2));
    					userpoints = totalpoints;
    					cout << "You rolled a " << die1 << " and " << die2 << endl;
    					cout << "You Win" << endl;
    					cout << "You now have " << userpoints << " points." << endl;
    				}
    	return (0);
    }

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    The second if is your problem. You are trying to execute multiple statements without a block. You are doing this:
    Code:
    if (...)
      something1;// if previous line is true do this, but always do the next instructions
      something2;
      something3;
      something4;
    else {
      something5;
      something6;
      something7;
      something8;
    }
    put brackets around that if() block. Also you need to move the close bracket of the first else block down after the second if-else block.

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    a break is used to exit a loop. since the body of your program isnt inside of a loop, instead of

    if ( temp == 0 )
    break;

    it would be better to put

    if ( temp == 0 )
    return 0;

    or even more simply, get rid of the first if and change the else to

    if ( temp != 0 )
    I came up with a cool phrase to put down here, but i forgot it...

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    Thanks for all the help. I corrected the braces and things look good now. I just keep getting an error message saying "illegal break". Am I using it improperly?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might have to post your corrected code.

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    Here is my new code. The problem is the break statement. Am I using it improperly?

    Code:
    #include <iostream.h>
    #include "random.h"
    
    int main()
    {
    	int die1, die2, dietotal, pointstaken, pointsrisked, userpoints=1000, temp, totalpoints;
    
    
    	cout << "Welcome to the Dice Program.  Press 0 to exit or the enter key to begin " << endl;
    	cin >> temp;
    		if (temp == 0)
    			break;
    		else {
    			cout <<"You have " << userpoints << " points." << endl;
    			cout <<"How many points do you want to risk: " << endl;
    			cin >> pointsrisked; //get risked points
    			die1 = 1 + random(5); //generate random number as dice roll
    			die2 = 1 + random(5); //generate random number as dice roll
    			dietotal= die1 + die2; //calculate dice total
    		
    			if ((dietotal % 2) ==0){ // determines if dice total is even or odd number
    					totalpoints = (userpoints - pointsrisked);
    					userpoints = totalpoints;
    					cout << "You rolled a " << die1 << " and " << die2 << endl;
    					cout << "You Lose" << endl;
    					cout << "You now have " << userpoints << " points." << endl;
    			}
    				else {
    					totalpoints = (userpoints +(pointsrisked * 2));
    					userpoints = totalpoints;
    					cout << "You rolled a " << die1 << " and " << die2 << endl;
    					cout << "You Win" << endl;
    					cout << "You now have " << userpoints << " points." << endl;
    				}
    		}
    	return (0);
    }

  11. #11
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    How funny I didn't even pick up on the fact that you're using break! break is used to prematurely end a loop ('for' or 'while' for example).
    break would be used like this:
    Code:
    while (true) {
      doSomething();
      if (done)
        break;
    }
    You don't want to use break in your program because it is not being used within a loop. You just want the program to end so you would just return 0;

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use:

    Code:
    #include <iostream.h>
    #include "random.h"
    
    int main()
    {
    	int die1, die2, dietotal, pointstaken, pointsrisked, userpoints = 1000, temp, totalpoints;
    
    	cout << "Welcome to the Dice Program.  Press 0 to exit or the enter key to begin " << endl;
    	cin >> temp;
    	if (temp != 0) {
    		cout <<"You have " << userpoints << " points." << endl;
    		cout <<"How many points do you want to risk: " << endl;
    		cin >> pointsrisked; //get risked points
    		die1 = 1 + random(5); //generate random number as dice roll
    		die2 = 1 + random(5); //generate random number as dice roll
    		dietotal = die1 + die2; //calculate dice total
    
    		if ((dietotal % 2) == 0) { // determines if dice total is even or odd number
    			totalpoints = (userpoints - pointsrisked);
    			userpoints = totalpoints;
    			cout << "You rolled a " << die1 << " and " << die2 << endl;
    			cout << "You Lose" << endl;
    			cout << "You now have " << userpoints << " points." << endl;
    		}
    		else {
    			totalpoints = (userpoints +(pointsrisked * 2));
    			userpoints = totalpoints;
    			cout << "You rolled a " << die1 << " and " << die2 << endl;
    			cout << "You Win" << endl;
    			cout << "You now have " << userpoints << " points." << endl;
    		}
    	}
    	return 0;
    }
    Since you cant really break out of an if conditional like that.

    Also, you might want to note that
    #include <iostream.h>
    in deprecated in C++, you should use
    #include <iostream>
    and then use the namespaces appropriately.

  13. #13
    Registered User
    Join Date
    Nov 2003
    Posts
    43
    wen people post corrected code thet should compile it to see if it works first

  14. #14
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    maybe we don't want to do all the work for you...

    and did you consider that we don't all have compilers in front of us?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Originally posted by makveli
    wen people post corrected code thet should compile it to see if it works first
    Would you like to correct it for me, seeing that you have random.h and its associated object file(s), and that you are so eager to correct the use of <iostream.h> instead of <iostream> for the OP?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random number gen & looping probelm :?
    By FoxeySoulSLayer in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2009, 05:44 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. Generating a random number?
    By Konspiracy in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 12:33 AM
  5. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM