Thursday, July 9, 2009

C++ COmputer programming help?

We have to write a guess the numver game program in visual c++. This is what it reads your program chosesthe number to be guessed by selecting an integer at random in the range 1 to 1000. The Programthen displays the following





I have a number between 1 and 1000


can you guess my number?


Please type your first guess.





The player then typesa first guess. With one of the following.





excellent


too low try again


too high try again





It then wants you to modify the program to count the number of guesses the player makes. If the number is 10 or fewer print "either you know the secret or you got lucky. " If player guesss the number in ten tries then print " you should do better"


THis is what I have.





#pragma once





class GuessNumber


{


public:


GuessNumber(void);


~GuessNumber(void);


};











#include %26lt;iostream%26gt;


#include %26lt;cstdlib%26gt;


#include %26lt;ctime%26gt;





#include "GuessNumber.h"





using namespace std;


int main()


{


srand(time(0)); // seed random number generator





int theNumber = rand() % 100 + 1; //random number between 1 and 100


int tries = 0, guess;





cout %26lt;%26lt; "\tWelcome to Guess My Number\n\n";





//Guessing Loop


do


{





cout %26lt;%26lt; "Enter a guess:";


cin %26gt;%26gt; guess;


++tries;





if (guess %26gt; theNumber)


cout %26lt;%26lt; "Too high!\n\n";





if (guess %26lt; theNumber)


cout %26lt;%26lt; "Too low!\n\n";


} while (guess != theNumber);


cout %26lt;%26lt; "\nThat's it! You got it in" %26lt;%26lt; tries %26lt;%26lt; "guesses!\n";





return 0;


}

C++ COmputer programming help?
Add the following lines before "return 0" statement:





=================================


if( tries %26lt; 10 )


cout %26lt;%26lt; "Either you know the secret or you got lucky" %26lt;%26lt; endl;


else


cout %26lt;%26lt; "You should do better" %26lt;%26lt; endl;


=================================





Sounds silly that you ask this much if you have written the rest of it. Are you sure that YOU wrote the program you quoted? Whataver, please do not forget to give me 10 points :-)
Reply:You aren't using the GuessNumber class at all, so its not necessary. It looks ok, you just need to add the feedback about the number of tries being less than 10 or greater than 10.


No comments:

Post a Comment