Hello I have been thinking for a while on how to do this one program in c++. I am trying to make a quiz in c++ with 4 yes or no questions. After the quiz has been taken I want to have it ask another question depending on how many it answered yes to. I have been brainstorming on how I could do this for a while. Any help to get me started in the right direction would be appreciated.

Show me what you have for code and I'll help you develop it more. Remember that help is only given if you show that you actually want to do some work. ^_^

ok well I only did the first question in the code because i want to know how to take note throughout the quiz about how many questions were answered yes or no therefore at the end depending on the yes or no's answered I will be able to ask a whole different question. Heres what I got so far.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    std::string baby1;
    std::string baby1_yes = "yes";
    std::string baby1_no = "no";
    std::string baby2;
    std::string baby2_yes = "yes";
    std::string baby2_no = "no";
    std::string baby3;
    std::string baby3_yes = "yes";
    std::string baby3_no = "no";    
    std::string baby4;
    std::string baby4_yes = "yes";
    std::string baby4_no = "no";
    
    cout<<"Is the baby lethagic: ";
    cin>>baby1;//I want to be able to record how many times the user said yes or no, here I put only 1 question but after I figure out what to put there I will be all set
    
    if(baby1 == baby1_no){
    //Not sure how to take note right here that the answer is no.
}
    if(baby1 = baby1_yes){
    //need help here                 
} 
    system("pause");
    return 0;
}

Okay, a few things here.
1 What compiler and OS are you using.
2 You've over complicated this too much. You don't need the

std::string baby1_yes = "yes";
std::string baby1_no = "no";

Instead after the question is asked just write

if (baby1 == "yes")
{
     // We'll get to this part in a minute
}
if (baby1 == "no")
{
     //We'll also get to this part in a minute
}
/* You also should add an if statement incase the user doesn't input "yes" or "no". It would look some thing like this*/
if (baby1 != "yes" || baby1 != "no")
{
      /* this is were you would have a cout telling the user to input either yes or no and then you'd send them back to the beginning of question one*/
}

3 You should also try taking out the std:: in front of the strings and it should still work depending on you're compiler.

4 If you're running windows it should be fine to use

system("pause");

, but I wouldn't get use to it as it only works on windows and you might find yourself on a MAC or Linux someday.

Im using DEv C++ on Windows Vista. And my question is how to almost tally or keep records of the yes's or no's for each question. Have any idea?

Ok, so yeah the tallying is really easy.

#include <iostream>
#include <string>

using namespace std;

int main ()
{
     string baby1;
     string baby2;
     string baby3;
     string baby4;
     string baby5;
     int yes; // to keep track of yes answers
     int no; // to keep track of no answers
     bool run1 = true; // repeat the questions

while (run1 == true)
{
     cout << "Is the baby lethargic? ";
     cin >> baby1;
     if (baby1 == "yes")
     {
          yes = yes + 1;
          // or you could put yes += 1;
          run1 = false;
     }
     if (baby1 == "no")
     {
          no = no + 1;
          // or you could put no += 1;
          run1 = false;
     }
     if (baby1 != "yes" || baby1 != "no")
     {
          cout << "Please input yes or no.\n";
     }
}
     system("pause"); // ok for now
     return 0;
}

Do you get it? I can explain it more if you want, and I can show you a function to avoid the system("pasue"); call if you want!! ^_^

You're welcome, but you should take a look at my new thread about replacing system("pause");, trust me it's a bad habbit to pick up!! =]

#include <iostream>
#include <string>

using namespace std;

int main ()
{
     string ques = "\0";
     int yes; // to keep track of yes answers
     int no; // to keep track of no answers
     bool run1 = true; // repeat the questions

while (run1 == true)
{
     cout << "Is the baby lethargic? ";
     cin >> baby1;
     if (baby1 == "yes")
     {
          yes = yes + 1;
          // or you could put yes += 1;
          run1 = false;
     }
     if (baby1 == "no")
     {
          no = no + 1;
          // or you could put no += 1;
          run1 = false;
     }
     if (baby1 != "yes" || baby1 != "no")
     {
          cout << "Please input yes or no.\n";
     }
}
     ques = "\0"; //put this after every question
     system("pause"); // ok for now
     return 0;
}

Sorry, I was a little late on fixing the errors.

hi,

how can i make sure that when i ask a yes no question, i don't get rubbish for an answer.
i have tried some while codes, but i seem to do something wrong, because when i execute the program i can enter anything i want...
this is what i have til now

cout<<"Do you want to shutdown "<<ComputerName<<"?  Yes or No\n";
  char ans[4];
  cin>>ans;
  cin.ignore();
      
  while((ans!="Yes" && ans!="No"))
  {
    cout<<"I said: \"Yes or No\" so enter Yes or No please\n";
    cin.clear();
    cin>>ans;
    cin.ignore();
  }
  
  if (ans=="Yes")
  {
     cout<<"Ok\n";
  }
  
  if (ans=="No")
  {
     cout<<"Ok, the please enter the name of the computer you want to shut down\n";
  }

yes i am making my own shutdown timer :)
please help me

hi,

how can i make sure that when i ask a yes no question, i don't get rubbish for an answer.
i have tried some while codes, but i seem to do something wrong, because when i execute the program i can enter anything i want...
this is what i have til now

cout<<"Do you want to shutdown "<<ComputerName<<"?  Yes or No\n";
  char ans[4];
  cin>>ans;
  cin.ignore();
      
  while((ans!="Yes" && ans!="No"))
  {
    cout<<"I said: \"Yes or No\" so enter Yes or No please\n";
    cin.clear();
    cin>>ans;
    cin.ignore();
  }
  
  if (ans=="Yes")
  {
     cout<<"Ok\n";
  }
  
  if (ans=="No")
  {
     cout<<"Ok, the please enter the name of the computer you want to shut down\n";
  }

yes i am making my own shutdown timer :)
please help me

if (ans=="Yes")

Don't compare strings with the == and != operators as you did above. They compare addresses, not contents of strings. Instead use the strcmp function:

if (strcmp (ans, "Yes") == 0)

http://www.cplusplus.com/reference/clibrary/cstring/strcmp.html

ah
and I was thinking it was my logic...
thnx very much it works perfectly now

No it doesn't. Try answering the question with "Supercalifragilisticexpialidocious". Chances are the program will crash because you are stuffing 34 characters into a 4-character array.

Instead of char use the C++ string . Then you can compare using ==, and your input won't crash your program.

you're right thnx
the program didn't crash, but my time variable took a large number...
anyways now that is working, can enter any computername of a computer i want to shut down, but the debugger gives a segmentation error in the function initiatesystemshutdown whenever i enter any other computername...

how to make a modify option for user?
for example if user press a before game all questions should be display and after that if user want to modify any question there should be a option for user....please tell me how its code will be????????????

Comments
Necropost (4 year old thread) and to top it with a very stupid post..

The code will be like this

#include <iostream>

int main ()
{
    std::cout << "Nice _quality_ necro-post.\n";
    return 0;
}
This article has been dead for over six months. Start a new discussion instead.