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

  1. #1
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76

    Question 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 ):
    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.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    Quote Originally Posted by Elysia View Post
    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?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Google
    You avoid it by using loops.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    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.

  6. #6
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    Quote Originally Posted by kmdv View Post
    Ok, first of all, do NOT use goto...
    How and why?

    Quote Originally Posted by kmdv View Post
    ... and indent your code.
    Any examples? I guess this is for organization purposes, right?

    Quote Originally Posted by kmdv View Post
    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.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh, come on!
    I even gave you a link! Did you miss it?

    Quote Originally Posted by muffinman8641 View Post
    How and why?
    Let me google that for you

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

    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    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;
    }

  9. #9
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    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).

  10. #10
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by muffinman8641 View Post
    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.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    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.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  13. #13
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    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!).

  14. #14
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by rogster001 View Post
    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.

  15. #15
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    I think he meant it was the students misusing goto.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cygwin on win64
    By Vanzemljak in forum Tech Board
    Replies: 3
    Last Post: 01-12-2011, 04:28 PM
  2. Replies: 15
    Last Post: 10-20-2009, 09:39 AM
  3. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  4. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  5. non repeating random number generation?
    By gencor45 in forum C# Programming
    Replies: 1
    Last Post: 02-08-2005, 05:23 PM