It's a college project, all it has to do is ask the user for info and then output it. That part is done, so the project is done (I'm not asking for help with school work).
I decided to add a bit more to it just for fun. I'm trying to make it know when nothing was entered by the user (just hitting enter) and tell the user to do so and start over. After it starts over more than once, it outputs the last "cout" however many times the user failed to answer something (by just hitting enter). I can't figure out why.
Output:
Answer every question in order for this word game to work properly.
What is your name? 12
How old are you? 12
Where are you from? 12
What's the name of your college? 12
What is your profession? 12
What type of animal is your pet? 12
and what is her/his name? 12
There once was a person named 12 who lived in 12. At the age of 12, 12 went to college at 12. 12 graduated and went to work as a 12. Then, 12 adopted a(n) 12 named 12. They both lived happily ever after!
There once was a person named 12 who lived in . At the age of 12, 12 went to college at 12. 12 graduated and went to work as a . Then, 12 adopted a(n) 12 named 12. They both lived happily ever after!
There once was a person named 12 who lived in 12. At the age of 12, 12 went to college at 12. 12 graduated and went to work as a 12. Then, 12 adopted a(n) named 12. They both lived happily ever after!
Press any key to continue . . .
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name, city, college, prof, animal, petn;
int age;
// If no value is given for a question, start over.
struct NoValue
{
static void check(string a, int cls = 100)
{
if (a.empty())
{
cout << "\n\nAnswers cannot be blank, let's try this again..." << endl;
cin.get(); // Pause
for (int i = 0; i <= cls; i++) //
cout << "\n" << endl; // Clear screen
main();
}
}
};
cout << "Answer every question in order for this word game to work properly.\n\n";
// Questions
cout << "What is your name? ";
getline(cin, name);
cout << "How old are you? ";
cin >> age;
cin.ignore(); // To prevent the program from printing out the next 2 questions in the same line.
cout << "Where are you from? ";
getline(cin, city);
cout << "What's the name of your college? ";
getline(cin, college);
cout << "What is your profession? ";
getline(cin, prof);
cout << "What type of animal is your pet? ";
getline(cin, animal);
cout << "and what is her/his name? ";
getline(cin, petn);
NoValue::check(name);
NoValue::check(city);
NoValue::check(college);
NoValue::check(prof);
NoValue::check(animal);
NoValue::check(petn);
cout << "\nThere once was a person named " << name << " who lived in " << city
<< ". At the age of " << age << ", " << name << " went to college at " << college
<< ". " << name << " graduated and went to work as a " << prof << ". Then, " << name
<< " adopted a(n) " << animal << " named " << petn << ". They both lived happily ever after!\n\n" << endl;
return 0;
}