Help please!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

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
void option()
{
    bool verify2 = false;

    while(verify2 == false)
    {
       string response;
       bool verify = false;
       cout << "Which field would you like to inquire about?" << "\n";
       cout << "Action, Strength, or Diagnostic?" <<  "\n";
       cout << "If you would like to quit please enter Q or q" << endl;
       getline(cin, response);
       response = lowercase(response);//Converts response to lowercase letters

        if(response == "strength")
        {
            verify = true; //Changed to true if it enter this if statement
            strength();
       
        }

        if(response == "action")
        {
            verify = true;
            action();
          
        }

        if(response == "diagnostic")
        {
            verify = true;
            diagnostic();
        
        }
        if(response == "q")
        {
            verify2 = true;
            cout << "Goodbye." << endl;
            
        }
        else if(verify == false || response.empty()) //If it hasnt entered either of the statements this will print
        {
            cout << "Your input was not recognized. Please check spelling." << "\n" << endl;
            option();
        }
    }


}


Hey guys/gals I need some serious help before I go insane. I've written this function with the intent to give users the choice and once the choice is selected to go to the other function and do its thing. My problem is when it runs and I choose action, it completes its purpose and it repeats the action function. The same for the other two (strength and diagnosis). I've tried placing option() again after action is called in the same if statement to see what would happen. After I did so the menu came up and no matter what I select the action function keeps repeating. I've tried choosing to quit after action or any of the other functions are called and it wouldn't quit. The only way the program quits is if quit is my first option.

I know one solution is just to put exit(), but I don't want to do that. I want the variables and statements to do what they are supposed to.

I would hate to find out it's some minor foolishness.
Use else if's instead of all ifs. And your line 41's second condition is doing nothing because as long as the user has not entered anything, getline will still be waiting for input
long as the user has not entered anything, getline will still be waiting for input
Nope. getline() will store all characters it encounter into response until delimeter (default '\n') is found. Then it discards it and stops. If user just presses enter, getline will store an empty string.
Last edited on
Anyone?
You might have some issues on the other functions maybe.
try looking at the ones that loop and are not supposed to...

what if you just do this: and eliminate the bool verify variable
1
2
3
4
5
else if(response.empty()) //If it hasnt entered either of the statements this will print
        {
            cout << "Your input was not recognized. Please check spelling." << "\n" << endl;
            option();
        }


this is just for better code writing
Last edited on
Thou I don't know exactly if this will fix it, but worth a try, I guess.
A: Make the entire if section an "if else" section instead (ie, add the word "else " to the beginning of line 22, 29 and 35)
B: Remove the if statement on line 41
C: Remove line 44

Don't know if that will work, but hey, why not give it a whirl?
Topic archived. No new replies allowed.