Hi, i'm new to C++ and been trying to write a simple program to compare the randomly generator number with the number input. But once i got the num1 = num2; the program keep prompting for new input. Any help would be appreciated. Thanks in advance!
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main(){
char n;
int amt, num1, num2;
int count;
int tries;
srand(time(NULL));
do
{
//code for number generated from 0-99
for(int i=0; i < 1; ++i){
num2 = rand() % 100 + 1; }
cout<<num2 <<endl;
loop:
//amount input
cout << "Enter the amount $1, $2 or $5: ";
cin >> amt; // get input
//setting number of tries
switch (amt){
case 1: tries = 2;
break;
case 2: tries = 5;
break;
case 5: tries = 12;
break;
default:
cout << "Please enter $1, $2 or $5 only..." << endl; goto loop;
}
//comparing the number
do
{
for (count =0; count < tries ; ++count){
cout << "Please enter a number: ";
cin >> num1; // get input
if (num1 > num2)
cout << "Entered Number is higher than value, retry with a lower number" << endl;
else if (num1 < num2)
cout << "Entered Number is lower than value, retry with a higher number" << endl;
else if (num1 == num2)
{cout << "Congradulations, you have guess the number correctly!!" << endl; break;}
}
}while ( count < tries );
cout << "Sorry, you have exhausted all your tries..." << endl;
//To exit or continue
cout << "Enter 'N' or 'n' to exit, any other key to continue..";
cin >> n;
n = toupper(n);
}while (n != 'N');
return 0;
}