Questionnaire/Survey program with the use of if /else if/else statements

Oct 13, 2021 at 9:17pm
Hi I’m trying to figure how I would create a validation for the amount of no’s that are at least used more than 5 or more times and if the person does
Say no more than 5 or equal to 5 then the questionnaire/survey would end.

Here is the later half portion of my code(There’s 6 all together)
Char
choice = ‘N’;
choice = ‘Y’:)

cout << “ Do you at least work out more than 5 days a week?(Y/N) “;
cin >> choice;
cout << “ Do you jog every morning?(Y/N)” ;
cin >> choice;
cout << “ Do you weigh less than 200 lbs?(Y/N)” ;
cin >> choice;

cout << “ thank you so much for participating here are the results: \n “;
cout << “ 5 or more “no” answers: You might want to consider talking with a physician or a nutritionist \n “;
cout << “ 4-2 “no” answers: Not bad but make sure to keep track of your current progress \n “
cout << “ 1 no answer: Pretty good make sure to stay a float out there:)” << endI;

If (choice = ‘N’ >= 5 )
{
cout << “ Entered 5 or more “ No “ responses questionnaire will now be terminated: “ << endI;
}

Return 0

}



Oct 13, 2021 at 10:08pm
The clue to solving this proble is to be able to clearly describe what the problem is. Pseudocode is often the best step. If I understand you correctly the pseudocode might be ...

start the program running
setup choice variable
setup a counter variable to 0

loop:
ask a question
if answer is N then add 1 to the counter

if counter value is less than 5 then loop back
otherwise the end
Last edited on Oct 13, 2021 at 10:08pm
Oct 13, 2021 at 10:08pm
If you have some list of questions to ask the user, a natural way to store this would be with arrays (or vectors).

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
// Example program
#include <iostream>
#include <string>
using namespace std;

int main()
{
    const int NumQuestions = 8;
    
    const string questions[NumQuestions] = {
        "Is it bigger than a breadbox?",
        "Can you change the nature of a man?",
        "Do you love me?",
        "Will you love me forever?",
        "Do you need me?",
        "Will you never leave me?",
        "Will you make me so happy for the rest of my life?",
        "Will you take me away, and will you make me your wife?"
    };
    
    int num_noes = 0;
    
    for (int i = 0; i < NumQuestions; i++)
    {
        cout << questions[i] << " (Y/N): ";
        char response;
        cin >> response;
        
        if (response != 'Y')
        {
            num_noes++;
        }
        
        if (num_noes >= 5)
        {
            break; // stop early
        }
    }
    
    if (num_noes <= 1)
    {
        // pretty good   
    }
    else if (num_noes <= 4)
    {
        // not bad
    }
    else
    {
        // consider talking...   
    }
}
Last edited on Oct 13, 2021 at 10:14pm
Oct 14, 2021 at 12:36am
I see all great responses but let’s say I have to use everything I’ve learned from the intro of c++ to if statements so just a little before loops and arrays. But I will keep the array response in mind when I cover that due time .
Oct 14, 2021 at 1:34am
A good move because you will learn in due course how arrays (or other containers of similar data) and loops reduce duplication of your code and enable you to concentrate your efforts in developing and debugging your code.
Oct 14, 2021 at 3:15am
Thanks so much definitely taking all these replies in account huge help :) if I have any more questions I’ll be sure to ask :)
Topic archived. No new replies allowed.