kenshin Posted October 16, 2005 Posted October 16, 2005 Im taking a c++ class and im stuck on how to do something. If anyone can help me that would be great. Im trying to keep count of how many times a user enters a value and display it. Im not sure how to go about doing that in c++. Thanks Quote
Leaders snarfblam Posted October 16, 2005 Leaders Posted October 16, 2005 How does the program on a whole work? What sort of values are being entered. Are you simply letting the user enter numbers and totalling the number of times each number is entered? Quote [sIGPIC]e[/sIGPIC]
kenshin Posted October 16, 2005 Author Posted October 16, 2005 (edited) #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int main(); int getRandom(); int game(); int playAgain() //ask if user wants to play agian { char answer, y, n; //answer is the response to the question //y and n are the answers the user can give cout << "Do you want to play again (y or n)?: "; cin >> answer; if(answer == 'y') //if the user types y then it starts the main() main(); else if(answer == 'n') //If n then exits cout << "Thanks for playing" << endl; return 0; } int getRandom() // genurates a random # { int max, value; // max is the max value the user picks // value is the random number cout << "Enter the maximum random number value: "; cin >> max; srand( time(0) ); value = rand() % max + 1; //random number equation return value; } int game() // ask the user their guess { int guess, random, count; random = getRandom(); do { cout << "What is your guess?: "; cin >> guess; if(guess < random) // The if statements checks to see if they are to high or low cout << "Too low." << endl; // then gives an answer if(guess > random) cout << "To High." << endl; }while( guess != random ); // ask the question until guess = random count += 1; // keeps track of trys cout << "You got it in " << count << " try(s)" << endl; return 0; } main() // runs the game { do { game(); }while(playAgain()); return 0; } in the game() is where im trying to do it. Edited October 20, 2006 by PlausiblyDamp Quote
kenshin Posted October 17, 2005 Author Posted October 17, 2005 nevermind i just need to move the count after the guess. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.