Rand()

#include<iostream>
#include<ctime>
usingnamespace std;
void main()
{
int x,c=0;
srand(time(0));
cout<<"Enter a number betweeen 1 and 15\n";
cin>>x;
while(true)
{
c++;
int a=1+rand()%15;
if(a==x)
{
cout<<"Is your Number"<<a<<"? y"<<endl;
break;
}
elseif(a>x)
cout<<"Is your Number"<<a<<"? h"<<endl;
else
cout<<"Is your nuumber"<<a<<"? l"<<endl;
}
cout<<"That Took me "<<c<<" times"<<endl;

}



this a simple program that uses the function rand() the users enter a number between 1 and 15 and the computer tries to find this number the problem is ig i entered the value of x > 15 the program enters an infinte loop so is there a way to stop this
Change the argument within the while loop to while(x <= 15) But you probably want to add another if statement in there so that it won't print out cout<<"That Took me "<<c<<" times"<<endl; Even if the number is above 15.
Last edited on
Topic archived. No new replies allowed.