Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • Leaders
Posted
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?
[sIGPIC]e[/sIGPIC]
Posted (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 by PlausiblyDamp

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...