Guess the number program

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();

for(int guess=1;guess < 8; guess++)
{
for(int guessnum=1; guessnum < 6; guessnum++)
{
cout<<"Enter your guess:";
cin>>g;

if(g=n)
{
cout<<"You're right You've got the magic number!";
}
else
{
if(g>n)
cout<<"Your guess is TOO HIGH!";
else
cout<<"Your guess is TOO LOW!";
}

}
}

getch();
return 0;
}


I really need your help guys.
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.

What specifically are you stuck on?
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>

using namespace std;

main()
{
    int n,g;

    cout<<"Enter the magic number:";
    cin>>n;

    for(int guess=1;guess < 8; guess++)
    {
        for(int guessnum=1; guessnum < 6; guessnum++)
        {
            cout<<"Enter your guess:";
            cin >> g;

            if(g == n)
            {
                cout << "You're right You've got the magic number!";
            }
            else
            {
                if( g > n )
                    cout << "Your guess is TOO HIGH!";
                else
                    cout << "Your guess is TOO LOW!";
            }
        }
    }
    return 0;
}


Your turn from here ...
Last edited on
@LB

hmm, conio.h and iostream.h were introduced to my class by our C++ Programming teacher, anyway i reworked at my code here it is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream.h>
#include<conio.h>
main(){
	 int mnumber,guess,tries;
	 clrscr();
	 cout<<"Enter the magic number:";
	 cin>>mnumber;
	 clrscr();

	 for(tries=1;tries<=10;tries++)
	 {
		for(int guessnum;guessnum<99;guessnum++)
		{
		cout<<"Enter your guess:";
		cin>>guess;
			if(guess==mnumber)
			{
			cout<<"You're right! You've got the magic number!";
			}
			else if (guess<mnumber)
			{
			cout<<"Your guess is TOO LOW!";
			}
			else
			{cout<<"Your guess is TOO HIGH!";
			}
		}
	 }
	 cout<<"\nThe number of tries is "<<tries<<"";
	 getch();
	 return 	0;

}
@LB

Do i need to use a nested for loop or not?
abujuguluy wrote:
hmm, conio.h and iostream.h were introduced to my class by our C++ Programming teacher
Your teacher isn't teaching you C++ then, they are teaching you something that used to be C++ over two decades ago. Oh well.
abujuguluy wrote:
Do i need to use a nested for loop or not?
I believe you do not need nested loops for this at all, if I understand correctly.
Last edited on
@LB

well i guess the IT program of my school is very very very very old.

i'll update the code asap and then i'll post it here.
@LB

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 here for(tries=1;tries<=10;tries++) so that the loop will stop when (guess=mnumber)?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream.h>
#include<conio.h>
main(){
	 int mnumber,guess,tries;
	 clrscr();
	 cout<<"Enter the magic number:";
	 cin>>mnumber;
	 clrscr();
	 for(tries=1;tries<=10;tries++)
	 {
		cout<<"\nEnter your guess:";
		cin>>guess;
			if(guess=mnumber)
			{
			cout<<"You're right! You've got the magic number!";
			}
			else if (guess<mnumber)
			{
			cout<<"Your guess is TOO LOW!";
			}
			else
			{cout<<"Your guess is TOO HIGH!";
			break;
			}
	 }
	 cout<<"\nThe number of tries is "<<tries<<"";
	 getch();
	 return 0;

}
closed account (48T7M4Gy)
Your teacher isn't teaching you C++ then, they are teaching you something that used to be C++ over two decades ago. Oh well.


To say a thing like that you just have to be a yank.
@kemort

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.
closed account (48T7M4Gy)
@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.)
;)
here is the updated code, i think i'm near

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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!";
			}
			else if (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;

}
That looks like it should work, you just need to end the loop when the uses guesses correctly. Use the break; statement ;)
@LB

I think I got it now. Here it is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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;
			}
			else if (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.
closed account (48T7M4Gy)
I ran it here and it works OK even though I had to remove a few lines etc You can go through it carefully and make any changes you want



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>//.h>
//#include<conio.h> You have to leave it out to use on this website

using namespace 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!";
			}
			else if (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.

should be
for(int tries = 0; etc etc
Last edited on
I don't have your compiler so I can't test myself, but it definitely looks like it should work now.

Feel free to start new threads for your other questions - I only give help in public in order to help people who Google search :)
@LB @kemort

Thanks to the both of you. I'll be starting a new thread now.
Topic archived. No new replies allowed.