Magic Number Program

I have to write a program that allows the user 5 chances to guess a random value. This random value changes 7 times and each time i have to allow the user 5 attempts to guess these values. My main issue is that the nested for loop is not executing 7 times. Instead when the program runs, it only allows the user 5 attempts at the first guess value, THEN it just automatically closes. Not sure why, would be awesome if someone could point out my error(s). Thanks very much in advance.

PS : Pay no mind to the code commented out, its just stuff i was fooling around with.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Programmed by Anthony Altreb. Date 10-16-12 
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()

{

	int magic;// magic number.
	int guess;// user's guess.
	int c=0;// the amount of right guesses
	magic=rand()%20+1;//get a random number. between 0-19 then adds one.
	
	
	/*char name;*/
	/* int flag=0; */


	
	// This is the for loop that will control how many magic number values you have to guess

	/* cout<<"Please enter your name \n";
	cin>>name;
	cout<<" Hello\n "<< name <<" you have 5 chances to guess the right value for 7 DIFFERENT numbers.";
	*/
	

    for (int guesschance=1; guesschance >= 8;guesschance++);
	
	{
		
		// This is the for loop that will control the number of chances you get to guess the value.

		magic=rand()%20+1;

		for (int guessnum=1; guessnum < 6; guessnum++) 

		{
			cout<<"Please enter guess #" <<guessnum<<" for guess value "<<guesschance<<": "; // The prompt message.
			
			// Obtain a guess value from user.
			
			cin>>guess; 
			
		
				
				if (guess==magic) 
				
				{
					
					// Prompt message that tells user that their guess value was correct.
					
					cout<<"\n***You are well built, my respects!!!***\nMagic Number was"<<' '<<magic<<"!\n"; 
					
					// Prompt message that tells the user at which attempt the user guessed correctly.
					
					cout<<"Your guess was the #"<< guessnum <<" attempt.\n"; 
					
					// The expression is rechecked and found to be false thus breaking out of this block of code and ends the program.
					
					guessnum=6; /* && flag=1*/ 		
				
				}
				
				else 
				
				{
					
					cout<<"\n...Sorry, you are wrong, please try again! \n"; // prompt message that lets user know their guess value was incorrect.
					
					if (guess>magic) cout<<"\nYour approximation has exceeded my expectation. Please lower your guess value.\n"; // prompt message that lets user know their guess value was too high
					
					else cout<<"\nYour approximation is far lower then I expected. Please raise the value of your guess.\n"<<endl; // prompt message that lets user know their guess value was too low. 
				
				}
				
				if (guessnum==5 && magic!=guess)
					
				{
					cout<<"The magic number was "<<magic<<endl;
				}
		}
			}
		}	

	{
		cout<<"Your success rate was "<< (c/7)*100<<"%"<<endl;
	}
	
	return 0; // end of program.

}
Last edited on
@aa6

Noticed this often done mistake..

for (int guesschance=1; guesschance >= 8;guesschance++);

Should be
for (int guesschance=1; guesschance < 8;guesschance++)
The semi-colon shouldn't be there, so I removed it, plus you're checking for when guesschance has been used 7 times, hence the 1 to less than 8. After this, we can check if there are other small errors. Good luck..
Topic archived. No new replies allowed.