# Making a random number guessing game (exit failure? wtf)

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 02-28-2011
muffinman8641
Making a random number guessing game (exit failure? wtf)
Hi, this is my first post here. Hopefully you fine people could be of assistance. I'm somewhat of a newbie at C++; I just went to Borders and bought a book on it and I'm teaching myself along with a casual programming class at school (high school, that is; not in college yet).

I know this is overly complicated, but as long as it works I'll worry more about a simplified version later. I put it into codepad.org and it said "exit failure". Whenever I use my regular compiler (Dev CPP) to run it, it gives me no errors but closes instantly, despite my use of the system ("pause") statement. Any help?

The source code (don't judge me for my choice of words in cout sections :p):
Code:

```#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { int t, l1, l2, l3, l4, l5, x1, x2, x0; t = 1; // Variables: // L - Listed Item //  l1 = unlimited //  l2 = 10 //  l3 = 8 //  l4 = 5 //  l5 = 1 // X - User Input //  x1 = List Selection //  x2 = Guess //  x0 = Try Again? // t - Attempt Count start: {  srand((unsigned)time(0));     int random_integer, x;     int lowest = 1, highest = 1000;     int range = (highest - lowest) + 1;     for(int index=0; index<1; index++) {         random_integer = lowest+int(range * rand() / (RAND_MAX + 1.0));         return random_integer;   } cout << "--------------------------------------------------"; cout << "A number between 1 and 1,000 has been randomly " << endl; cout << "generated. To play the game, type a number and press ENTER. " << endl; cout << "The program will tell you to guess higher or lower. You " << endl; cout << "may choose five options, as listed below. To begin the game, " << endl; cout << "type the number of the option you want and press ENTER. " << endl; cout << "Good luck! " << endl << endl << endl; cout << "[1] Easy - Unlimited Guesses " << endl; cout << "[2] Medium - 10 Guesses " << endl; cout << "[3] Hard - 8 Guesses " << endl; cout << "[4] Extreme - 5 Guesses " << endl; cout << "[5] OMGWTF - 1 Guess " << endl; cout << "[6] (sigh) - Infinite Guesses " << endl; cout << "--------------------------------------------------"; cin >> x1; list_error: while ((x1 < 1) || (x1 > 6)) { cout << "Please enter a number from 1-6. " << endl; goto start; } ////////////////////////////////////// if (x1 == 6) {         x_6_retry:                 cout << "Enter your guess. " << endl;         cin >> x2;         if (x2 == rand()) {                 cout << "You win! " << endl;                 goto try_again;         }         else {                 while (x2 > rand()) {                         cout << "Guess a smaller number. " << endl;                     goto x_6_retry;                 }                 while (x2 < rand()) {                         cout << "Guess a larger number. " << endl;                         goto x_6_retry;                 } } ////////////////////////////////////// if (x1 == 5) { cout << "Enter your guess. " << endl; cin >> x2; if (x2 == rand()) { // replaced all a1s (now renamed x in return in the random number function) with rand(). cout << "You win! " << endl; goto try_again; } else { cout << "EPIC FAILSAUCE " << endl; goto try_again; } } else { ////////////////////////////////////// if (x1 == 4) { cout << "Enter your guess. " << endl; x_4_retry: cin >> x2; while (x2 < rand()) { cout << "Guess a larger number. " << endl; goto x_4_retry; ++t; } while (x2 > rand()) { cout << "Guess a smaller number. " << endl; goto x_4_retry; ++t; } while (t > 5) { cout << "EPIC FAILSAUCE " << endl; cout << "The number was "; cout << rand(); cout << " . " << endl; goto try_again; } } else { ////////////////////////////////////// if (x1 == 3) { cout << "Enter your guess. " << endl; x_3_retry: cin >> x2; while (x2 < rand()) { cout << "Guess a larger number. " << endl; goto x_3_retry; t++; } while (x2 > rand()) { cout << "Guess a smaller number. " << endl; goto x_3_retry; t++; } while (t > 8) { cout << "EPIC FAILSAUCE " << endl; cout << "The number was "; cout << rand(); cout << " . " << endl; goto try_again; } } else { ////////////////////////////////////// if (x1 == 2) { cout << "Enter your guess. " << endl; x_2_retry: cin >> x2; while (x2 < rand()) { cout << "Guess a larger number. " << endl; goto x_2_retry; t++; } while (x2 > rand()) { cout << "Guess a smaller number. " << endl; goto x_2_retry; t++; } while (t > 8) { cout << "EPIC FAILSAUCE " << endl; cout << "The number was "; cout << rand(); cout << " . " << endl; goto try_again; } } else { ////////////////////////////////////// if (x1 == 1) { cout << "Enter your guess. " << endl; x_1_retry: cin >> x2; while (x2 < rand()) { cout << "Guess a larger number. " << endl; goto x_1_retry; t++; } while (x2 > rand()) { cout << "Guess a smaller number. " << endl; goto x_1_retry; t++; } while (t > 10) { cout << "EPIC FAILSAUCE " << endl; cout << "The number was "; cout << rand(); cout << " . " << endl; goto try_again; } } else { ////////////////////////////////////// goto list_error; } } } } } try_again: cout << "Try again? Enter a 1 for yes or a 0 for no. " << endl; cin >> x0; if (x0 == 1) { goto start; } else { goto end_prgrm; } end_prgrm:           system ("pause"); return 0; } } }```
Yes, I handwrote the whole thing other than the random number function because I was lost with those.

Today, my programming class teacher gave us basically the same program without knowing I'm working on this and said it took him 10 minutes. I've probably spent 3-4 hours on this. :rolleyes:
• 02-28-2011
Elysia
The code is in a pretty sad state at the moment, though.
Before anything else, you need to learn to indent the code and avoid goto.
• 02-28-2011
muffinman8641
Quote:

Originally Posted by Elysia
The code is in a pretty sad state at the moment, though.
Before anything else, you need to learn to indent the code and avoid goto.

I've heard to avoid those. Why and how?
• 02-28-2011
Elysia
Google
You avoid it by using loops.
• 02-28-2011
kmdv
Ok, first of all, do NOT use goto and indent your code. I just copied it to autoformatter and noticed that there is no 'return 0;' instruction at the end of main. Well, there is one (or more), but at least it might never execute.
• 02-28-2011
muffinman8641
Quote:

Originally Posted by kmdv
Ok, first of all, do NOT use goto...

How and why?

Quote:

Originally Posted by kmdv
... and indent your code.

Any examples? I guess this is for organization purposes, right?

Quote:

Originally Posted by kmdv
I just copied it to autoformatter and noticed that there is no 'return 0;' instruction at the end of main. Well, there is one (or more), but at least it might never execute.

Where should it be? I put it at the end with system ("pause")...

I apologize for my astounding lack of knowledge.
• 03-01-2011
Elysia
Oh, come on!
I even gave you a link! Did you miss it?

Quote:

Originally Posted by muffinman8641
How and why?

Let me google that for you

Quote:

Any examples? I guess this is for organization purposes, right?
Let me google that for you

Quote:

Where should it be? I put it at the end with system ("pause")...
return is optional, but otherwise it shall be the last statement in the function, because it ends the function execution.
• 03-01-2011
kmdv
This google automated page is great, didn't know about it.
I wonder why so many people waste time on joining the forum and waiting long for the answers instead of performing a few clicks.

Anyway:
Code:

```end_prgrm:           system ("pause"); return 0; } } return 0; }```
• 03-01-2011
muffinman8641
I clicked the "Submit Post" button right as the bell rang so I left class and closed the browser window. Without checking, I posting the same mindless question again. Sorry.
:[

Anyway, I did look into the horrors of goto; it's just messy and badly used, not goto itself (or so the interwebz tells me).
• 03-01-2011
kmdv
Quote:

Originally Posted by muffinman8641
Anyway, I did look into the horrors of goto; it's just messy and badly used, not goto itself (or so the interwebz tells me).

No matter how you use, it will be messy. I recall only one situation in which I used goto, but it was a performance critical low level code.
• 03-01-2011
Elysia
There are instances where goto may be useful (breaking out of nested loops is a fine example), but any other use of goto results in messy, bad code. So avoid it, and apply it only where it will help.
• 03-01-2011
rogster001
Goto we were always taught, would be bad code for the additional reason of stack overflows, i did a short course in C years ago, the class had to use some ancient UNIX terminals to write, build and save their code, stuff was always crashing the terminals due to injudicious use of goto by students wrestling with the course problems.
• 03-01-2011
muffinman8641
Anyway, the goto isn't the problem here. I'm yet to try it on my compiler at home, but I use codepad at school and it still says "exit failure", even though I dropped in the edited chunk of code kmdv was kind enough to provide (thank you sir!).
• 03-01-2011
kmdv
Quote:

Originally Posted by rogster001
Goto we were always taught, would be bad code for the additional reason of stack overflows, i did a short course in C years ago, the class had to use some ancient UNIX terminals to write, build and save their code, stuff was always crashing the terminals due to injudicious use of goto by students wrestling with the course problems.

So it was the problem of the compiler not the language instruction.
• 03-01-2011
muffinman8641
I think he meant it was the students misusing goto.
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last