Hi guys, i need your help. I need to make a program that works just like a game, where the program will ask the user to input a magic number then it will ask the user to guess the number. If the guess number is > the magic number it will print "You're Guess is TOO HIGH!", same thing when the guess number is < the magic number. And it should also put the number of tries of the user while guessing the magic number. Anyways the program should be in for loop.
this is where i am right now, and i am very lost.
#include<iostream.h>
#include<conio.h>
main(){
int n,g;
clrscr();
cout<<"Enter the magic number:";
cin>>n;
clrscr();
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.
conio.h and iostream.h are not standard C or C++ headers, and you should avoid using them. Modern compilers don't even support them, so whatever compiler you are using must be ancient - I recommend updating to a newer compiler.
Here it is, the problem i am facing right now is that the loop won't stop even if the if statement is already true. what should i put herefor(tries=1;tries<=10;tries++) so that the loop will stop when (guess=mnumber)?
It's alright for him to say that dude, I live in a 3rd world country and I study at a public university, public education here isn't the first priority of the government, and it sucks.
@abu I knew that is probably the case. Some people don't understand there are differences around the world and lots of people have to work a lot harder than where money and educational resources are more available for whatever reason.
Keep up the good work.
You don't need conio etc as he said. If you want to run your program then just use the gear wheel at the top right hand corner of the purple box with your code in it on this page. (It picks up conio as not being recognised and all you do is delete that line or comment it out.)
;)
#include<iostream.h>
#include<conio.h>
main(){
int mnumber,guess,tries=1;
clrscr();
cout<<"Enter the magic number:";
cin>>mnumber;
clrscr();
for(;tries<10;tries++)
{
cout<<"\nEnter your guess:";
cin>>guess;
if(guess==mnumber)
{
cout<<"You're right! You've got the magic number!";
}
elseif (guess<mnumber)
{
cout<<"Your guess is TOO LOW!";
}
else
{
cout<<"Your guess is TOO HIGH!";
}
}
cout<<"\nThe number of tries is "<<tries<<"";
cout<<"\nThank you for using this program!";
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
main(){
int mnumber,guess,tries=1;
clrscr();
cout<<"Enter the magic number:";
cin>>mnumber;
clrscr();
for(;tries<100000;tries++)
{
cout<<"\nEnter your guess:";
cin>>guess;
if(guess==mnumber)
{
cout<<"You're right! You've got the magic number!";
break;
}
elseif (guess<mnumber)
{
cout<<"Your guess is TOO LOW!";
}
else
{
cout<<"Your guess is TOO HIGH!";
}
}
cout<<"\n\nNumber of Tries:"<<tries<<"";
cout<<"\n\nThank you for using this program!";
getch();
return 0;
}
Thank you very much for the help bro. If possible I would also like to seek another help/guidance from you regarding 2 other different machine problems. You're an angel dude.
#include<iostream>//.h>
//#include<conio.h> You have to leave it out to use on this website
usingnamespace std;
int main(){
int mnumber,guess,tries=1;
//clrscr(); not necessary here
cout<<"Enter the magic number:";
cin>>mnumber;
//clrscr(); not necessary here
for(;tries<10;tries++)
{
cout<<"\nEnter your guess:";
cin>>guess;
if(guess==mnumber)
{
cout<<"You're right! You've got the magic number!";
}
elseif (guess<mnumber)
{
cout<<"Your guess is TOO LOW!";
}
else
{
cout<<"Your guess is TOO HIGH!";
}
}
cout<<"\nThe number of tries is "<<tries<<"";
cout<<"\nThank you for using this program!";
//getch(); not necessary here
return 0;
}
It runs OK and I tested a few guesses which are OK
Enter the magic number:6
Enter your guess:5
Your guess is TOO LOW!
Enter your guess:6
You're right! You've got the magic number!
Enter your guess:
Only 1 problem. At line 12 your for loop is incomplete so the program never stops You'll also need to think about how you can break out once you have found the correct number. Otherwise you have up to 9 unecessary tries to complete.