Magic numbers program

Hey, I modified this program from another persons post. Is there any way to make this any better than it already 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
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
93
94
95
96
97
// Programmed by Anthony Altreb. Date 10-16-12
//Reprogramed by jasonwynn10. Date 9-5-14
#include <iostream>
#include <cstdlib>
#include <string>
/*Windows only!
#include <windows.h>*/

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.
	
	
	string name;
    int flag=1;

	 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.\n";
	
	
	
	// This is the for loop that will control how many magic number values you have to guess
    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 && flag==1) 
				
				{
					
					// 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; 		
				
				}
				
				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;
				}
		}	
	    c++;
    	cout<<name<<", your success rate was "<< (c/7)*100<<"%"<<endl;
	    /*Windows only!
	    system("pause");
	    system("cls");*/
}
	return 0; // end of program.

}
Last edited on
just a few things that come to mind

In your program when you ask the user for their name what happens if the user enters his first and last name?

When the user is asked to enter a number what happens if the user accidently enters a character by mistake?

You never tell the user what range of numbers they should be guessing between.

what happens if the user enters a number outside this range?

you need to seed your rand() function with srand()
rand() and srand() are deprecated, use the classes in the <random> header instead:
http://www.cplusplus.com/reference/random/
http://en.cppreference.com/w/cpp/header/random
With a healthy sense of irony, I'd like to suggest that you do not use magic numbers in your code. ;)
Thanks for the help!
Topic archived. No new replies allowed.