Random Number Game

Write your question here.
I am trying to make the random number game and have it so the user can set in min and max number to guess between as well as set the number of tries the user has. Can someone show me what code i need to add, i really just hit a wall on this one. I also need to change it from dasychained to separate functions to call in the main. I don't know how to. Thank You so much.
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
  Put the code you need help with here.

#include "stdafx.h"
#include "iostream"
#include "string"
#include <ctime>
#include <stdlib.h>           
#include <time.h>            

using namespace std;

void ChooseRandomNumber()
	return << rand()(maxBound - minBound0) + minBound;

void main()
{
	srand(time(0));
	
	while(true) { 
		
		int minBoun (int min);
		int maxBound (int max);
		int number = rand() %1000000 +1;
		int guess; 
		int tries = 0;
		char answer;
		
		while(true) { 
			
			cout << "Enter a min number: " << min << endl;
			getline(cin, minBound);

			cout << "Enter a max number: " << max << endl;
			getline(cin, maxBound);

			if(guess > number) {
				cout << "Too high! Try again.\n";
			} else if(guess < number) {
				cout << "Too low! Try again.\n";
			} else {
				break;
			}
		
			tries++;
		}
		
	
		if(tries >= 20) {
			cout << "You ran out of tries!\n\n";
		} else {
			
			cout<<"Congratulations" << endl;
			cout<<"You got the right number in " << tries << " tries!\n";
		}
		
		while(true) {
			
			cout << "Would you like to play again (Y/N)? ";
			cin >> answer;
			cin.ignore();
			
			
			if(answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y') {
				break;
			} else {
				cout << "Please enter \'Y\' or \'N\'...\n";
			}
		}
		
		
		if(answer == 'n' || answer == 'N') {
			cout << "Thank you for playing!";
			break;
		} else {
			cout << "\n\n\n";
		}
	}
	
	cout << "\n\nEnter anything to exit. . . ";
	cin.ignore();
	return 0;
}
Lines 4-5: standard library headers should use <>, not quotes.

line 7: The correct header for C++ is <cstdlib>.

line 8: Not needed since you have the <ctime> header.

Line 13: You need {} around your function. You're missing an operator between rand() and the expression that follows it. maxbound, minbound are not defined in this scope. minbound0 Is this a typo?

Line 21-22: These are function prototypes, not variables.

Lines 30-34: What is the purpose of minbound and maxbound. They are not used. They're referenced in ChooseRandomNumber, but ChooseRandomNumber is never called.

Any place you have a while (true) loop, is probably a good condidate to replace with a function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool guess_number (int number)
{  int guess;
    
    cout << "Enter a guess: ";
    cin >> guess; 
    if (guess > number) 
    {   cout << "Too high! Try again.\n";
        return false;
    } 
    if (guess < number) 
   {    cout << "Too low! Try again.\n";
        return false;     
   } 
    return true; 
}

...
    while (! guess_number(number))
    {  tries++;
    } 

Last edited on
Yeah the minBound0 is a typo, woops. I will work on what you said, thanks. I might have questions later. Thanks
Topic archived. No new replies allowed.