5 Replies - 617 Views - Last Post: 31 July 2012 - 06:15 PM Rate Topic: -----

#1 noja190  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 30-May 12

Number guessing game problem

Posted 31 July 2012 - 04:40 PM

NOJA190
{
C++ Knowledge level: Beginner
Books read: 0
Book Currently reading: Beginning C++ Through Game Programming (Second edition)
}

^ Accidently purchased the second edition, not the third...but i'm sure there isn't a big difference


I'm trying to make a number guessing game and it works, but 1 bit of the code isn't working

i'm trying to get the players previous score and tell him if he/she beat his old score, by making an int called timesPlayed and preScore, i will only have this code run if the player has played twice, and there is an old score to display...but I can't get the preScore(old score) to display...I'm stumped, if I wasn't I probably wound't be here asking for help...

Can some one take a look at my code and tell me what I did wrong, and try and teach me so I don't make the same mistake again.
Also could you give me tips on inproving my code, thank you.

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    char playAgain = 'y';

    while(playAgain == 'y')
    {

        srand(time(0));
        int theNumber = rand() % 100 + 1;
        int tries = 0;
        int guess;
        int timesPlayed = 1;

        cout << "\tWelcome to Guess My Number\n\n";

        do
        {
            cout << "Enter a guess: ";
            cin >> guess;
            tries++;

            if(guess > theNumber)
            cout << "\nToo High!\n\n";
            if(guess < theNumber)
            cout << "\nToo low!\n\n";
        }while (guess != theNumber);

        int score = 10000 / tries;


        cout << "\nThat's it! You got it in " << tries << " Guesses!\n\n";
        cout << "Your Score: " << score;

        int preScore = score;
        ++timesPlayed;

        if(timesPlayed >= 2)
        {
            if(preScore > score)
                cout << "You Beat your old score of " << preScore << endl;
            if(preScore < score)
                cout << "Im sorry, but you did not beat your old score of " << preScore << endl;
        }



        cout <<"\n\nPlay Again?\n";
        cout << "(y/n)\n\n";
        cin >> playAgain;
        if(playAgain == 'y')
            cout << "\n\n\n\n\n\n\n\n\n\n\n\n";
    }

    cout << "\n\nGoodbye, Play again some time.";


}

This post has been edited by GunnerInc: 31 July 2012 - 06:22 PM
Reason for edit:: Removed font tag


Is This A Good Question/Topic? 0
  • +

Replies To: Number guessing game problem

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,465
  • Joined: 05-May 12

Re: Number guessing game problem

Posted 31 July 2012 - 04:50 PM

On line 41, you always assign the current score to preScore. You probably want to declare the preScore variable outside the scope of the loop, as well as only assign score to preScore after you've done the comparisons.
Was This Post Helpful? 0
  • +
  • -

#3 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 840
  • View blog
  • Posts: 2,472
  • Joined: 20-March 10

Re: Number guessing game problem

Posted 31 July 2012 - 05:19 PM

It would also improve your code
If you returned an integer from main()

Usually at the end of the program you
Would

return 0;
Was This Post Helpful? 0
  • +
  • -

#4 noja190  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 30-May 12

Re: Number guessing game problem

Posted 31 July 2012 - 05:48 PM

View Postsnoopy11, on 31 July 2012 - 05:19 PM, said:

It would also improve your code
If you returned an integer from main()

Usually at the end of the program you
Would

return 0;


im using codeblocks, it isn't necessary, but i will remember that.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,465
  • Joined: 05-May 12

Re: Number guessing game problem

Posted 31 July 2012 - 05:58 PM

Try bumping up the warning level of your compiler and you'll discover that you should.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,434
  • Joined: 25-December 09

Re: Number guessing game problem

Posted 31 July 2012 - 06:15 PM

Both the C11 and C++11 standards state that if the closing brace of main() is reached this function will return 0 to the operating system.

From the C++11 standard.

Quote

3.6.1

5
A return statement in main has the effect of leaving the main function (destroying any objects with automatic
storage duration) and calling std::exit with the return value as the argument. If control reaches the end
of main without encountering a return statement, the effect is that of executing
return 0;


From the C11 standard.

Quote

5.1.2.2.3 Program termination
1
If the return type of the main function is a type compatible with int, a return from the
initial call to the main function is equivalent to calling the exit function with the value
returned by the main function as its argument;10) reaching the } that terminates the
main function returns a value of 0. If the return type is not compatible with int, the
termination status returned to the host environment is unspecified.


So as you can see you are not required to return a value from the main() function, and you should never get a warning if you fail to provide a return value for this function.

However, I also recommend you return a value from main, if for no other reason than to maintain consistency with every other function. If you don't return a value from any other function that should return a value you should get a warning.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1