Using a 'while' loop to guess a persons age

So the assignment is this: "Write a C++ program that uses a while loop to guess a persons age, ask the user if the want to try again if not end the loop. Of course if they guess it the loop also ends"

This is what I have so far;

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
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <ctime>

using namespace std;

int main ()





{

srand(time(NULL));

char input = 'N';

while(input == 'N')
{

int guess = (rand() % 100) + 1; //guess from range of 0-100

printf("Since you can use a computer i'd wager your %d years old\n", guess);

printf("If I got your age wrong I can try again (y/n): ");

input = getchar();

input = toupper(input);



}
}


After I input "N" it asks the question again, but doesn't continue the loop. If I could get some help on this I would be forever grateful. This is an accelerated online class and I haven't been able to get the book yet so I am struggling to get caught up.
I had to add #include <cstdio> to get the code you have to compile for me. I tweaked it just a little to use functions from <iostream> and it's looping until Y is input. To get the output below, I entered N four times, then Y.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main ()
{
	srand(time(NULL));

	char input = 'N';

	while(input == 'N')
	{
		int guess = (rand() % 100) + 1; //guess from range of 0-100
		cout << "Since you can use a computer I'd wager you're " << guess << " years old\n";
		cout << "If I got your age wrong I can try again (y/n): " << endl;
		cin >> input;
		input = toupper(input);
	}
	return 0;
}


Since you can use a computer I'd wager you're 69 years old
If I got your age wrong I can try again (y/n): 
Since you can use a computer I'd wager you're 72 years old
If I got your age wrong I can try again (y/n): 
Since you can use a computer I'd wager you're 99 years old
If I got your age wrong I can try again (y/n): 
Since you can use a computer I'd wager you're 36 years old
If I got your age wrong I can try again (y/n): 
Since you can use a computer I'd wager you're 83 years old
If I got your age wrong I can try again (y/n): 
Last edited on
Topic archived. No new replies allowed.