Basically after every guess i want the loop to run until the user guesses right.

This is just a simple number guessing game.

I ask the user for more input after every wrong guess but the program ends after the second guess is given.

Can anyone help?

#import <iostream>
#include <ctime> 
#include <cstdlib>

using namespace std;

int main()
{ 
    srand((unsigned)time(0)); 
    int random_integer; 
    int lowest=1, highest=10; 
    int range=(highest-lowest)+1; 
    for(int index=0; index<1; index++){ 
        random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); 
    } 
    cout << random_integer << endl;
    
    int user_guess;
    cout << "please try and guess the number between " << lowest << " and " 
    << highest << endl << endl;
    
    cin >> user_guess;
    
    int number_of_tries = 0;
    
    if (user_guess > 10)
       {
        cout << endl << "You're supposed to pick a number between " << lowest << " and " 
        << highest << "!" << endl << endl;
        cout << "Please pick another number..." << endl << endl;
        number_of_tries = (number_of_tries + 1);
        cin >> user_guess;
       }
       
    else if (user_guess < random_integer)
         {
          cout << endl << "Sorry, but that number is too LOW!" << endl << endl;
          cout << "Please Guess again" << endl << endl;
          number_of_tries = (number_of_tries + 1);
          cin >> user_guess;
          }
          
    else if (user_guess > random_integer)
         {
          cout << endl << "Sorry, but that number is too HIGH!" << endl << endl;
          cout << "Please Guess again" << endl << endl;
          number_of_tries = (number_of_tries + 1);   
          cin >> user_guess;          
         }
    
    else if (user_guess == random_integer)
       {
       cout << "Good guess, you won!" << endl << endl;
       cout << "It took you " << number_of_tries << " tries" << endl << endl;
       cout << "but... I bet you can't do that again!" << endl;
       }
       
    
    system("pause");
}

The lines ranging from 26 to 50 should be enclosed in a while loop that keeps running until the user guess = the random number.

#include <iostream.h>
#include <time.h>
#include <stdlib.h>

int main()
{
    srand((unsigned)time(0)); 
     int random_integer;
     int lowest=1, highest=10;
     int range=(highest-lowest)+1;
     for(int index=0; index<1; index++){
        random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); 
     }
    cout << random_integer << endl;

    int user_guess;
    cout <<

sorry, didn't post properly

#import <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;


int main()
{
	 srand((unsigned)time(0));
	 int random_integer;
	 int lowest=1, highest=10;
	 int range=(highest-lowest)+1;
	 for(int index=0; index<1; index++){
		  random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
	 }
	 cout << random_integer << endl;


	 int user_guess;
	 cout << "please try and guess the number between " << lowest << " and "
	 << highest << endl << endl;

	 cin >> user_guess;

	 int number_of_tries = 0;
	for ( int i=0;i!=-1;i++)
	{ if (user_guess > 10)
		 {
		  cout << endl << "You're supposed to pick a number between " << lowest << " and "
		  << highest << "!" << endl << endl;
		  cout << "Please pick another number..." << endl << endl;
		  number_of_tries = (number_of_tries + 1);
		  cin >> user_guess;
		 }

	 else if (user_guess < random_integer)
			{
			 cout << endl << "Sorry, but that number is too LOW!" << endl << endl;
			 cout << "Please Guess again" << endl << endl;
			 number_of_tries = (number_of_tries + 1);
			 cin >> user_guess;
			 }

	 else if (user_guess > random_integer)
			{
			 cout << endl << "Sorry, but that number is too HIGH!" << endl << endl;
			 cout << "Please Guess again" << endl << endl;
			 number_of_tries = (number_of_tries + 1);
			 cin >> user_guess;
			}

	 else if (user_guess == random_integer)
		 {
		 cout << "Good guess, you won!" << endl << endl;
		 cout << "It took you " << number_of_tries << " tries" << endl << endl;
		 cout << "but... I bet you can't do that again!" << endl;
		 i=-1;
		 }
 }

	 system("pause");
}

None of the above code seems to work, i am going to attempt the while loop. Thanks

This code works. Thanks for the while loop tip.

#import <iostream>
#include <ctime> 
#include <cstdlib>

using namespace std;

int main()
{ 
    srand((unsigned)time(0)); 
    int random_integer; 
    int lowest=1, highest=10; 
    int range=(highest-lowest)+1; 
    for(int index=0; index<1; index++){ 
        random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); 
    } 
    cout << random_integer << endl;
    
    int user_guess;
    cout << "please try and guess the number between " << lowest << " and " 
    << highest << endl << endl;
    
    int number_of_tries = 0;
    
    while (user_guess != random_integer)
    {
    cin >> user_guess; cin.ignore();
    
    if (user_guess > 10)
       {
        number_of_tries = (number_of_tries + 1);
        cout << endl << "You're supposed to pick a number between " << lowest <<
        " and " << highest << "!" << endl << endl;
        cout << "Please pick another number..." << endl << endl;
       }
       
    else if (user_guess < random_integer)
       {
        number_of_tries = (number_of_tries + 1);
        cout << endl << "Sorry, but that number is too LOW!" << endl << endl;
        cout << "Please Guess again" << endl << endl;
       }
          
    else if (user_guess > random_integer)
       {
        number_of_tries = (number_of_tries + 1);
        cout << endl << "Sorry, but that number is too HIGH!" << endl << endl;
        cout << "Please Guess again" << endl << endl;   
       }
       }
    if (user_guess == random_integer)
       {
        number_of_tries = (number_of_tries + 1);
        cout << "Good guess, you won!" << endl << endl;
        cout << "It took you " << number_of_tries << " tries" << endl << endl;
        cout << "but... I bet you can't do that again!" << endl;
       }
       
    
    system("pause");
}

This code works. Thanks for the while loop tip.

#import <iostream>
#include <ctime> 
#include <cstdlib>

using namespace std;

int main()
{ 
    srand((unsigned)time(0)); 
    int random_integer; 
    int lowest=1, highest=10; 
    int range=(highest-lowest)+1; 
    for(int index=0; index<1; index++){ 
        random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); 
    } 
    cout << random_integer << endl;
    
    int user_guess;
    cout << "please try and guess the number between " << lowest << " and " 
    << highest << endl << endl;
    
    int number_of_tries = 0;
    
    while (user_guess != random_integer)
    {
    cin >> user_guess; cin.ignore();
    
    if (user_guess > 10)
       {
        number_of_tries = (number_of_tries + 1);
        cout << endl << "You're supposed to pick a number between " << lowest <<
        " and " << highest << "!" << endl << endl;
        cout << "Please pick another number..." << endl << endl;
       }
       
    else if (user_guess < random_integer)
       {
        number_of_tries = (number_of_tries + 1);
        cout << endl << "Sorry, but that number is too LOW!" << endl << endl;
        cout << "Please Guess again" << endl << endl;
       }
          
    else if (user_guess > random_integer)
       {
        number_of_tries = (number_of_tries + 1);
        cout << endl << "Sorry, but that number is too HIGH!" << endl << endl;
        cout << "Please Guess again" << endl << endl;   
       }
       }
    if (user_guess == random_integer)
       {
        number_of_tries = (number_of_tries + 1);
        cout << "Good guess, you won!" << endl << endl;
        cout << "It took you " << number_of_tries << " tries" << endl << endl;
        cout << "but... I bet you can't do that again!" << endl;
       }
       
    
    system("pause");
}
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.