random number program

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-10-2003
thatdude
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-----------```
• 11-10-2003
LuckY
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.
• 11-10-2003
thatdude
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); }```
• 11-10-2003
LuckY
dude, read my previous post. I explained how to do an if-else block.

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 {   //... }```
• 11-10-2003
thatdude
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); }```
• 11-10-2003
LuckY
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.
• 11-10-2003
...
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 )
• 11-11-2003
ct26torr
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?
• 11-11-2003
laserlight
You might have to post your corrected code.
• 11-11-2003
thatdude
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-2003
LuckY
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;
• 11-11-2003
laserlight
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.
• 11-11-2003
makveli
wen people post corrected code thet should compile it to see if it works first:rolleyes:
• 11-11-2003
major_small
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?
• 11-11-2003
laserlight
Quote:

Originally posted by makveli
wen people post corrected code thet should compile it to see if it works first:rolleyes:
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?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last