#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
 
 
 
int main()
{
srand(time(0));
int randNum = rand() % 100 + 1; // Generates random number between 1 and 100
int num = 0;
int name;//Name of player

cout << "Welcome to the Random Number Guessing Game!!"<<endl; // Name of Game.
cout<<"Your name is?"<<endl;//Request name of player.
 
do
{
// Loops until user finds the number
cout << "Enter your choice on the keyboard (#1-100): ";
cin >> num; // User's input of guessed number

if (num < randNum) // If num is < than the random number, inform player to try again.
cout <<"Your number was too low\n\n"; 

if (num > randNum) // If num is > than the random number, inform player to try again.
cout<<"Your number was too high\n\n";

} while (num != randNum); // Informs player if their number is correct.
cout<<"Excellent Job!! Your number is correct!!";

return 0;
}

----------------------------------------------------------------------------------

The program compiles ok-but i have a few bugs-
First-How do I get the program to request for a player's name (which will require input by player) and allow the name to be used throughout the game?
Second-How can I keep track of the user's guesses with a counter?
Third-I'm suppose to enter a score at the end of the program when the guess is right? So pretty much I'll need a tally i.e. The amount of ""Your number was too low" vs "Your number was too high".

>>How do I get the program to request for a player's name
declare a std::string object to hold the name, then call getline() to get the name from the keyboard.

>>How can I keep track of the user's guesses with a counter
declare an integer object to be the counter then increment it each time the user makes a guess.

>>third question
declare int counters for the number of guesses too low and another for the number of guesses too high.

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <string>
using namespace std;


int main()
{
srand(time(0));
int randNum = rand() % 100 + 1; // Generates random number between 1 and 100
int num = 0;
int wins = 0;
int losses = 0;
int tries = 0;
string uname;


cout <<"Please enter your name:"<<endl;//Request name of player.
getline (cin,uname);
cout<<uname<<endl;
cout <<"Welcome to the Random Number Guessing Game!!"<<endl; // Name of Game.



do
{
// Loops until user finds the number


cout <<uname<<" "<<"Enter your choice on the keyboard (#1 - 100): ";
cin >> num; // User's input of guessed number


if (num < randNum) // If num is < than the random number, inform player to try again.
cout <<uname<<""<<"Your number was too low-please try again!"<< endl;
{
wins = wins ++;
break;
}



if (num > randNum) // If num is > than the random number, inform player to try again.
cout<<uname<<""<<"Your number was too high-please try again!"<< endl;



else
tries = tries ++;


} while (num != randNum); // Informs player if their number is correct.
cout<<uname<<endl;
cout<<"   "<< "Excellent Job!! Your number is correct!!"<< endl;//Informs if the player is successfull
cout << "Your total wins: " << wins;//Display the user's score at the end of the program
cout << "  Your total losses: " << losses;//Display the user's score at the end of the program



return 0;
}

I don't see what i'm doing wrong-it compiles ok...but with all the numbers that I put in it says whether it's too low or too high...but it still prints out "Excellent Job!! Your number is correct!!" Pretty much all i need to be able to do is to keep track of the user's guesses with a counter & have a counter for the number of guesses too low and another for the number of guesses too high. I know it's something with regards to my if/else statement, but i can't see it. Thx for your help.

The assignment is to write a number guessing game. In your game, the player will choosea number from 1-100. Your program will tell the user if the number is too hi got too low and they will continue until they guess the correct number.

Your conditionals are a little wacky. In particular this one:

if (num < randNum)
  cout <<uname<<""<<"Your number was too low-please try again!"<< endl;
{
  wins = wins ++;
  break;
}

I'm reasonably sure you don't realize what it's actually doing, which is always incrementing wins and breaking. A little whitespace and extra braces should show you how the compiler sees this:

if (num < randNum)
{
  cout <<uname<<""<<"Your number was too low-please try again!"<< endl;
}

{
  wins = wins ++;
  break;
}

The second block isn't a part of the conditional, it's just an unnamed block that always executes like any other statement. Consider something more like this:

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

using namespace std;

int main()
{
  srand ( static_cast<unsigned int> ( time ( 0 ) ) );

  int randNum = rand() % 100 + 1;
  int num = 0;
  int tries = 0;
  string uname;

  cout<<"Please enter your name: ";
  getline ( cin, uname );
  cout<< uname <<'\n';
  cout<<"Welcome to the Random Number Guessing Game!!\n";

  do {
    cout<< uname <<" Enter your choice on the keyboard (#1 - 100): ";
    cin>> num;

    if ( num < randNum )
      cout<< uname <<" Your number was too low-please try again!\n";
    else if ( num > randNum )
      cout<< uname <<" Your number was too high-please try again!\n";
    else // num == randNum
      break;

    ++tries;
  } while ( tries < 3 );

  cout<< uname <<'\n';

  if ( tries < 3 )
    cout<<"Excellent Job!! Your number is correct!!\n";
  else
    cout<<"Too many tries. You lose\n";
}
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <string>
using namespace std;


int main()
{
	srand ( static_cast<unsigned int> ( time ( 0 ) ) );

  int randNum = rand() % 100 + 1;
  int num = 0;
  int wins = 0;    
  int losses = 0;
  int guess=0;
  string uname;
  
  cout<<"Please enter your name: ";
  getline ( cin, uname );
  cout<< uname <<'\n';
  cout<<"Welcome to the Random Number Guessing Game!!\n";

  
    {
        do
        {
            // Loops until user finds the number
            
            cout <<uname<<" "<<"please enter your choice between (#1 - 100): ";
            cin >> num; // User's input of guessed number
            
            if (num < randNum) // If num is < than the random number, inform player to try again.

               cout <<uname<<" "<<"Your number was too low-please try again!";
			 else
            
            if (num > randNum) // If num is > than the random number, inform player to try again.
               cout<<uname<<" "<<"Your number was too high-please try again!"<< endl;
			
            else
            
            break;
            
        } while (num != randNum);
		wins++;
		guess++;
    
    
        cout<<uname<<endl;
        cout<<" "<< "Excellent Job!! Your number is correct!!"<< endl;//Informs if the player is successfull
        cout << "Your total wins: " << wins;//Display the user's score at the end of the program
        cout << " Your total losses: " << losses;//Display the user's score at the end of the program
		cout<< " Total number of guesses: "<<guess;//Display the # of guesses by user
        } 


	return 0;
}

-------------------------------------------------------
We haven't spoken about nested loops in class as of yet-i guess we'll do so in the upcoming class...I kinna understand. When i run the code as above, it shows wins =1, regardless of the amount of time i play. My two last aims for this program is to: 1ST- Keep track of the user guesses with a counter. 2ND-Display the user’s score at the end of the program(i guess a tally between the number of Your number was too low-please try again! vs the number of "Your number was too high-please try again!" Thx for the assistance. I'm goning to try work on this and repost a code to see if i get it to work.

line 48 needs to be inside the do loop, probably just above line 34.

ancient dragon-how do i put my source code in tags?

>how do i put my source code in tags?
Start by being more perceptive. There's a button on the extended editor that adds code tags for you, and the text editor itself has a permanent watermark with instructions on how to use them.

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <string>
using namespace std;


int main()
{
	srand ( static_cast<unsigned int> ( time ( 0 ) ) );

  int randNum = rand() % 100 + 1;
  int num = 0;
  int wins = 0;    
  int losses = 0;
  int guess=0;
  string uname;
  
  cout<<"Please enter your name: ";
  getline ( cin, uname );
  cout<< uname <<'\n';
  cout<<"Welcome to the Random Number Guessing Game!!\n";

  
    {
        do
        {
            // Loops until user finds the number
            
            cout <<uname<<" "<<"please enter your choice between (#1 - 100): ";
            cin >> num; // User's input of guessed number
			guess++;
            
            if (num < randNum) // If num is < than the random number, inform player to try again.

               cout <<uname<<" "<<"Your number was too low-please try again!";
			   
			 else
            
            if (num > randNum) // If num is > than the random number, inform player to try again.
               cout<<uname<<" "<<"Your number was too high-please try again!";
				
			
            else
            
            break;
            
        } while (num != randNum);
		
		
    
    
        cout<<uname<<endl;
        cout<<" "<< "Excellent Job!! Your number is correct!!"<< endl;//Informs if the player is successfull
        cout << "Your total wins: " << wins;//Display the user's score at the end of the program
        cout << " Your total losses: " << losses;//Display the user's score at the end of the program
		cout<< " Total number of guesses: "<<guess;//Display the # of guesses by user
        } 


	return 0;
}

I have placed the guess++; within the loop and it works, just now gottoa work on the program keeping a tallie as to much much times it prints out "Your number was too high-please try again!"vs "Your number was too low-please try again!".

Got it.....thanks for all assistance-finally figured it all out. not alot of good c++ instructors/professors out there....thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.