Help, getline bugging out when looped?

So I'm trying to get into the swing of things and I've been trying to make a form that asks for a persons name, age, gender, and then asks them to confirm it. I put in a few loops so that the form would only go through when the person enters "yes".

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;    
}


However when it loops, it seems that the function that has the user input their name bugs out. It will go through fine twice but on the third time it will output the "Can you tell me your name?" and then skip straight to ageFunc.

I'm really confused as to why this is happening, and why on the third loop around :x can anybody help?
You need to rethink this function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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; /// this statment exits the function and therefore the while loop also. Note isAnswer is not reset to false (doesn't matter).
         }
         else if (yesno == "N") {
              cout<< "Huh? Ah, alright, let's give that another go, then. \n"; 
              isAnswer = true;
              return yesno; /// this statement exits the function and therefore the while loop also. Note that isAnswer is not reset to false (does matter).
         }
         else {
              cout<< "Sorry, " << yesno << " doesn't seem like a yes or no answer. Try again. \n";
         }
    }
    isAnswer = false;    
    /// You don't return anything if isAnswer is true (don't know if it matters) but a non void function should always return something.
}
Last edited on
Oh DX woops, thanks a ton!
Topic archived. No new replies allowed.