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 96 97 98 99 100 101 102 103 104 105 106 107 108
|
#include <iostream>
#include <istream>
#include <string>
#include <limits>
using namespace std;
void answer(string gender);
string nameFunc(string name);
int ageFunc(int age);
string genderFunc(string gender);
string yesnoFunc(string yesno);
bool formComplete = false;
string nameConst;
int ageConst;
string genderConst;
string yesnoConst;
bool isAnswer = false;
int main()
{
while (formComplete == false) {
nameConst = nameFunc(nameConst);
ageConst = ageFunc(ageConst);
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
genderConst = genderFunc(genderConst);
cin.clear();
cin.ignore();
yesnoConst = yesnoFunc(yesnoConst);
cin.clear();
cin.ignore();
}
}
string nameFunc(string name) {
cout<< "Can you tell me your name? \n";
getline(cin,name);
name[0] = toupper(name[0]);
int spaceInName;
spaceInName = name.find(" ");
if (spaceInName > 0) {
spaceInName++;
name[spaceInName] = toupper(name[spaceInName]);
}
return name;
}
int ageFunc(int age) {
cout<< "How about your age? \n";
while (1) {
if (cin>>age) {
break;
}
cout<< "Sorry, that isn't a number. Please input your age in all numbers.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
return age;
}
string genderFunc(string gender) {
cout<< "What's your gender? \n";
bool isGender = false;
while (isGender == false) {
cin>>gender;
gender[0] = toupper(gender[0]);
switch (gender[0]) {
case 'M':
cout<< "So, your name is " << nameConst << ", you are " << ageConst << " years old, and are a male? Yes or no? \n";
isGender = true;
break;
case 'F':
cout<< "So, your name is " << nameConst << ", you are " << ageConst << " years old, and are a female? Yes or no? \n";
isGender = true;
break;
default:
cout<< "Sorry, " << gender << " isn't a gender. Try male or female. \n";
break;
}
}
isGender = false;
return gender;
}
string yesnoFunc(string yesno) {
while (isAnswer == false) {
cin>> yesno;
yesno = toupper(yesno[0]);
if (yesno == "Y") {
cout<< "Excellent! Let's get you started then, " << nameConst << "\n";
isAnswer = true;
formComplete = true;
return yesno;
}
else if (yesno == "N") {
cout<< "Huh? Ah, alright, let's give that another go, then. \n";
isAnswer = true;
return yesno;
}
else {
cout<< "Sorry, " << yesno << " doesn't seem like a yes or no answer. Try again. \n";
}
}
isAnswer = false;
}
|