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;
}
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
// Example program
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
constint 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
}
elseif (num_noes <= 4)
{
// not bad
}
else
{
// consider talking...
}
}
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 .
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.