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"
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <ctime>
usingnamespace 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.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace 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):