Number Guessing Game

Write your question here.
I just hit a wall on this again, I really need specific instructions on how to make this work please. I am trying to get it to allow the user to enter the min and max number for the random number to guess and also the number of tries from 1-20. I just started doing c++ after VB, Thanks.
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
  Put the code you need help with here.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>                      

using namespace std;

int chooseRandomNumber(int num, int min, int max)
{	
	num = rand() % (min - max) + min;
	return num;
}
int setNumbers()
{ 
		int minBoun (int min);
		int maxBound (int max);
		int number = rand() %1000000 +1;
		int guess;
		int tries = 0;
		int difTries;
		char answer;
{
int upperBound()
{
	cout << "Enter a max number: " << max << endl;
			getline(cin, maxBound);
}
int lowerBound();
{
			cout << "Enter a min number: " << min << endl;
			getline(cin, minBound);
}
int getNumofGuesses()
{
		if(tries >= cout << "How many tries do you want from 1-20? " << difTries << endl;
			getline(cin, difTries);
			{
			cout << "You ran out of tries!\n\n";
			} else {
			
			cout<<"Congratulations" << endl;
			cout<<"You got the right number in " << tries << " tries!\n";
			}
}
	
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++;
} 

bool playAgain()
{
			
			cout << "Would you like to play again (Y/N)? ";
			cin >> answer;
			
			if(answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y') 
			 else {
				cout << "Please enter \'Y\' or \'N\'...\n";
			}
		}
		
		if(answer == 'n' || answer == 'N') {
			cout << "Thank you for playing!";
		} else {
			cout << "\n\n\n";
}

void main()
{
	srand(time(0));
	chooseRandomNumber();
	upperBound();
	lowerBound();
	playAgain();
	guess_number();
	getNumofGuesses();
}
Last edited on
First, function parameters and calling a function. See http://www.cplusplus.com/doc/tutorial/functions/

Second, if you want to play multiple times, then you do need a loop. See http://www.cplusplus.com/doc/tutorial/control/

Lines 18 and 19 are syntactically function declarations.

Lines 64-66 are not within any function.

Line 86 is not what the C++ standard requires.

There is even more, but start with these subtasks:
the user to enter the min and max number ... and also the number of tries from 1-20.


1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
  srand( time(0) );

  // 1. get lower bound
  // 2. get upper bound
  // 3. get number of tries

  // 4. print the three numbers

  return 0;
}
thanks i will do that now
This is the code I have now. And this is due by the end of the day tomorrow so if you can or anyone can it would be awesome if someone can help me. I cant miss this deadline. And just btw, I am trying to do it on my own but i just keep running into problems that i dotn understand. Thanks

TThis is the link: cpp.sh/55jc
Last edited on
1. If you want to insert a comment in code do it like so:
std::cout << "Hello World!"; // some comment
, your first line in code is interpreted by compiler as an actual code

2. Is there really a need for functions like this one?
1
2
3
4
5
int lowerBound(int minBound, int min);{
	cout << "Enter a min number: " << min << endl;
	cin >> min;
	return min;
}
Without function it'd use 2 lines of your code

3. Maybe try to use some programming style. You make serious mistakes, e.g. you write code outside a function 'cuz you didn't realize you've already closed the function(playAgain(), guess_number()). The most important thing with this is to be adamant. If you use it, use it with no exceptions. Here you can read about it: http://www.cprogramming.com/tutorial/style.html

4. I think you use Visual Studio. Next time you make a project click on next button -> check empty project check box. You don't know some basics, so you probably don't know anything about separate compilation too. I wite it 'cuz you include stdafx.h header file in your code

5. You don't know anything about scope of variables, you use variables declared as local variables in other functions(they exist only in this function) in the whole file.
Last edited on
Topic archived. No new replies allowed.