Thread: Something is wrong with my guessing game!

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Exclamation Something is wrong with my guessing game!

    If you compile this simple program you will se what I am trying to get it to do. It all works fine other than the fact that if you get it win on you'r last try, it will say that you lost even though when it displays the numbers out, you see that you did not loose, you won. Although the computer doesn't see it that way. I don't know why it is doing this.

    Thanks, August
    (Script I was talking about is below.)

    Code:
    #include <iostream>
    #include <conio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    #include <fstream>
    using namespace std;
    int rand_0toN1(int n);
    int main(){
    double lev_dif, lev_left, lev_play, lev_guess, lev_rand, goto_starter;
    srand(time(NULL)); // Set a seed for random-num. generation.
    souldnt_go:
    for(goto_starter=(99);goto_starter>50;){
    clrscr();
    cout<<"Choose level difficultey:"<<endl;
    cout<<endl;
    cout<<"1 - Super Hard"<<endl;
    cout<<"2 - Hard"<<endl;
    cout<<"3 - Medium"<<endl;
    cout<<"4 - Easy"<<endl;
    cout<<"5 - Super Easy"<<endl;
    cout<<"6 - Exit"<<endl;
    cout<<"\nOption: ";
    cin>>lev_dif;
    if(lev_dif == 6){
    clrscr();
    return 0; }
    else if(lev_dif < 1){
    goto_starter = (99); }
    else if(lev_dif > 5){
    goto_starter = (99); }
    else{
    goto go_break; }}
    go_break:
    clrscr();
    lev_play=(lev_dif + 2);
    lev_rand = rand_0toN1(99) + 1;
    cout<<"Guess a number bettween one and a hundred."<<endl;
    cout<<"Beginning Level "<<lev_dif;
    cout<<"\n"<<endl;
    for(lev_left = lev_play;lev_left > 0;lev_left = (lev_left - 1)){
    cout<<"Guess "<<lev_left<<" of "<<lev_play<<": ";
    cin>>lev_guess;
    if(lev_guess > lev_rand){
    cout<<"Wrong, guess lower."<<endl; }
    else if (lev_guess < lev_rand){
    cout<<"Wrong, guess higher."<<endl; }
    else if (lev_guess == lev_rand){
    cout<<"Right, you won!"<<endl;
    cout<<"The number was "<<lev_rand<<endl;
    getch( );
    goto souldnt_go; }
    else{
    clrscr();
    return 0; }}
    cout<<"\nSorry, but you lost!"<<endl;
    cout<<"The right number was "<<lev_guess<<endl;
    getch( );
    goto souldnt_go; }
    int rand_0toN1(int n) {
    return rand() % n; }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Computers don't like to let humans win at anything.

    You are going to have to figure out a better way to organize your code. Look through some of the posts on the forum, and look at which programs are easy to read, and which ones are very difficult to read. Then, emulate the good ones.

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    I agree, and you need to rethink the goto's as well, you must be coming from a BASIC background. While there are some legitimate uses of goto in c++, it should be avoided.

    I compiled it and it worked fine for me, even when I guessed right on the last guess. So not sure what's happening with yours.

    btw check out the nice formatting courtesy of Code::Blocks

    Code:
    #include <iostream>
    #include <conio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    #include <fstream>
    using namespace std;
    int rand_0toN1(int n);
    int main()
    {
        double lev_dif, lev_left, lev_play, lev_guess, lev_rand, goto_starter;
        srand(time(NULL)); // Set a seed for random-num. generation.
    souldnt_go:
        for(goto_starter=(99);goto_starter>50;)
        {
            clrscr();
            cout<<"Choose level difficultey:"<<endl;
            cout<<endl;
            cout<<"1 - Super Hard"<<endl;
            cout<<"2 - Hard"<<endl;
            cout<<"3 - Medium"<<endl;
            cout<<"4 - Easy"<<endl;
            cout<<"5 - Super Easy"<<endl;
            cout<<"6 - Exit"<<endl;
            cout<<"\nOption: ";
            cin>>lev_dif;
            if(lev_dif == 6)
            {
                clrscr();
                return 0;
            }
            else if(lev_dif < 1)
            {
                goto_starter = (99);
            }
            else if(lev_dif > 5)
            {
                goto_starter = (99);
            }
            else
            {
                goto go_break;
            }
        }
    go_break:
        clrscr();
        lev_play=(lev_dif + 2);
        lev_rand = rand_0toN1(99) + 1;
        cout<<"Guess a number bettween one and a hundred."<<endl;
        cout<<"Beginning Level "<<lev_dif;
        cout<<"\n"<<endl;
        for(lev_left = lev_play;lev_left > 0;lev_left = (lev_left - 1))
        {
            cout<<"Guess "<<lev_left<<" of "<<lev_play<<": ";
            cin>>lev_guess;
            if(lev_guess > lev_rand)
            {
                cout<<"Wrong, guess lower."<<endl;
            }
            else if (lev_guess < lev_rand)
            {
                cout<<"Wrong, guess higher."<<endl;
            }
            else if (lev_guess == lev_rand)
            {
                cout<<"Right, you won!"<<endl;
                cout<<"The number was "<<lev_rand<<endl;
                getch( );
                goto souldnt_go;
            }
            else
            {
                clrscr();
                return 0;
            }
        }
        cout<<"\nSorry, but you lost!"<<endl;
        cout<<"The right number was "<<lev_guess<<endl;
        getch( );
        goto souldnt_go;
    }
    int rand_0toN1(int n)
    {
        return rand() % n;
    }
    Last edited by Darryl; 04-26-2005 at 09:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  3. A guessing game
    By Lyanette in forum C++ Programming
    Replies: 5
    Last Post: 04-03-2003, 10:02 AM
  4. need help with a guessing game program
    By davidnj732 in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2003, 11:59 AM
  5. guessing game
    By wayko in forum C++ Programming
    Replies: 11
    Last Post: 09-19-2001, 06:10 PM