how to use functions.

ok i got my program to work but i can't figure out how to make it to come out right.

the user can input the numbers 1-10 but when they enter a low or high number a message should come out.

// Give Me a Number
// Demonstrates default function arguments

#include <iostream>
#include <string>

using namespace std;

int askNumber(int high, int low=0 );


int main()
{
int number = askNumber(10,1);
if (10 > number )
cout << "Thanks for entering: " << number << "\n\n";
else

number = askNumber(0);
cout << "Thats invilid to low, pick 1-10 \n\n";
system ("pause");
return 0;
}

int askNumber(int high, int low)
{
int num;
do
{
cout << "Please enter a number" << " (" << low << " - " << high << "): ";
cin >> num;
} while (num > high || num < low);

return num;
}
@mitchsoje20

Hope this helps. I changed the random number to be between 1 and 100. More enjoyable. Even better from 1 to 1000.

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
// Give Me a Number
// Demonstrates default function arguments

#include <iostream>
#include <windows.h> // for srand(GetTickCount());

using namespace std;

int askNumber(int high, int low, int computer_num );

int main()
{
	srand(GetTickCount());
	int low, high, number, picked_number, guesses = 1;
	picked_number = 1+rand()%99; // 1 to 100
	low = 1;
	high = 100;
	while (number != picked_number)
	{
		cout << "Guess #" << guesses << endl;
		number = askNumber(high,low, picked_number);
		guesses++;
	if (number < picked_number) // Update low and high variables
		low = number;
	if (number > picked_number)
		high = number;
	}; 
	cout << endl << "You took " << guesses << " guesses, to find my number.."<< endl;
return 0;
}

int askNumber(int high, int low, int computer_num)
{
int num;
do
{
cout << "Please enter a number" << " (" << low << " - " << high << "): ";
cin >> num;
if (num<computer_num && num > low)
	cout << "Your guess is to low.." << endl;

if ( num > computer_num && num < high)
	cout << "Your guess was to high.." << endl;
if ( num == computer_num)
	cout << "Yep.. That's my number.." << endl;
if(num < 0 || num > 100)
	cout << "Come on now. We're only using guesses from 0 to 100" << endl;
if(num < low || num > high)
	cout << "Please keep an eye on the previous guesses!!" << endl;
} while (num < low || num > high);

return num;
}
oh its the guessing, i tired it is fun . i also twek it a little bit but thank you very much for your help :)
Topic archived. No new replies allowed.