Help with my Questionaire Program

This is a discussion on Help with my Questionaire Program within the C++ Programming forums, part of the General Programming Boards category; hii guys, i am new to c++ and i wrote this basic program but i cant seem to make it ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    11

    Help with my Questionaire Program

    hii guys, i am new to c++ and i wrote this basic program but i cant seem to make it work right. Here is the code:-

    Code:
    // questionaire.cpp
    
    #include <iostream>
    #include <string.h>
    using namespace std;
    const int ANS = 80;
    
    
    int main()
           {
           
           char choice = 'x';
           char Answer[ANS];
           
           
           cout << "\nQ1. What is the capital of Turkey?";
           cout << "\n My Answer is:- ";
           cin >> Answer[ANS];
           if( Answer == "Ankara" || "ankara" )
           cout << "You got the right answer!";
           else 
           cout << "The answer is wrong.";          
                     
                     
           cout << "\nQ2. What is the capital of Egypt?";
           cout << "\n My Answer is:- ";
           cin >> Answer[ANS];
           if( Answer == "Cairo" || "cairo")
           cout << "You got the right answer!";
           else 
           cout << "The answer is wrong.";
           
           
           cout << "\nQ3. What is the capital of Iraq?";
           cout << "\n My Answer is:- ";
           cin >> Answer[ANS];
           if( Answer == "Baghdad" || "baghdad")
           cout << "You got the right answer!";
           else 
           cout << "The answer is wrong.";
    
    }

    What i want it to do is make it go to the next question if the user types in the right answer, for example, if the person types "Ankara", or "ankara" for the first question it goes directly to the next question, or if the question is wrong, it says "please try again" and goes back to the question again. But after 3 tries and the user cannot answer the question, then it finally goes to the next. you guys understand me?? thanks a lot

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Code:
           cout << "\nQ1. What is the capital of Turkey?";
           cout << "\n My Answer is:- ";
           cin.getline(Answer,ANS,'\n');
           if( Answer == "Ankara" || "ankara" )
           cout << "You got the right answer!";
           else 
           cout << "The answer is wrong.";
    Use cin.getline() for C-style strings.

    Prototype: cin.getline(<variable>,<size of buffer>,<delimeter>)

    ...and this marks the third time I've had to answer this question tonight.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    it still doesnt work. when i type in any word it still says "you got the right answer"

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Oh I'm sorry, I didn't even look at the if statement.
    Code:
    if( Answer == "Ankara" || "ankara" )  // This is always true
    
    if( Answer == "Ankara" || Answer == "ankara" ) // This is conditional
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    hey thanks for your help, it doesnt say "you got the right answer", now everytime i type something it keeps saying the answer is wrong. lol. could you please check it again :S

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Did you change all your if() statements?

    Post your latest code - we've no way of knowing what you've done to break it.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    here is the code now:-

    BTW i got a friend to help me fix it but i have a new problem..

    Code:
    // questionaire.cpp
    
    #include <iostream>
    #include <windows.h>
    #include <string.h>
    using namespace std;
    const int ANS = 80;
    
    void fullscreen()
    {
    keybd_event(VK_MENU,0x38,0,0);
    keybd_event(VK_RETURN,0x1c,0,0);
    keybd_event(VK_RETURN,0x1c,KEYEVENTF_KEYUP,0);
    keybd_event(VK_MENU,0x38,KEYEVENTF_KEYUP,0);
    }
    
    class Questionaire
    {
          public:
          void Showcreator()
          {
               cout << "         **********[QUESTIONAIRE 1.0 by aBz]**********                 Score";
          }
          void Myansweris()
          {
               cout << "\n My Answer is:- ";
          }
          void Right()
          {
               cout << "\nCONGRATULATIONS! You got the right answer!";
               cout << "\nPress any key to proceed.";
               cin.ignore();
               system("cls");
          }
          void Wrong()
          {
               cout << "\nWRONG!!!";
               cout << "\nPress any key to try again.";
               cin.ignore();
               system("cls");
          }
    };
    
    int main()
    
    {
           
           char choice = 'x';
           char Answer[ANS];
           
           Questionaire selection;
           
    q1:
           selection.Showcreator();
           cout << "\n\nQ1. What is the capital of Turkey?";
           selection.Myansweris();
           cin.getline(Answer,ANS);
           if( strcmp(Answer,"Ankara") == 0 || strcmp(Answer,"ankara") == 0 )
    	   {
               selection.Right(); 
           }
    	   else 
           {
    		   selection.Wrong();                   
               goto q1;
    	   }
    q2:
           selection.Showcreator();
           cout << "\n\nQ2. What is the capital of Egypt?";
           selection.Myansweris();
           cin.getline(Answer,ANS);
           if( strcmp(Answer,"Cairo") == 0 || strcmp(Answer,"cairo") == 0 )
    	   {
               selection.Right();
           }
    	   else 
           {
    		   selection.Wrong();          
    		   goto q2;
    	   }
    q3:
           selection.Showcreator();
           cout << "\n\nQ3. What is the capital of Iraq?";
           selection.Myansweris();
           cin.getline(Answer,ANS);
           if( strcmp(Answer,"Baghdad") == 0 || strcmp(Answer,"baghdad") == 0 )
           {
               selection.Right();
    	   }
           else 
           {
    		   selection.Wrong();         
               goto q3;
    	   }
    	   
    }
    I want to put in a basic score system. what code do i use to add integers to a variable?
    Last edited by danepporambo; 11-24-2005 at 06:57 AM.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Ah, sorry again. Didn't even notice you were using C style strings. I was a bit tired last night, me guesses.

    Anyway, the score system should be easy to add, just ask your friend. I got turkey to eat.
    Sent from my iPadŽ

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    If your friend said to use goto's, then you need a more knowledgeable friend.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    lol well he helped me fix the score system as well now. Now i got 1 final problem. is there a way to make it so the questions come randomly? He said he couldnt help me with that..

  11. #11
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by danepporambo
    lol well he helped me fix the score system as well now. Now i got 1 final problem. is there a way to make it so the questions come randomly? He said he couldnt help me with that..
    Wow, I hate to see goto's used like that.

    Since the purpose of using a class in your code is lost, I would.. Put the question and the answer strings in the class. Then make an array of instances and add the question/answer to each instance (well actually I wouldnt do that grunt work personally, I'd load them from a text file, but thats me). Then simply use rand() (seeded with srand) (thats your random part, you can find info on it in the FAQ) as the element of the array. You could then make a method in the class say called ask() and put the body of each question there, use the variable in the class for the question and answer, and in main() simply call selection[rand() % 3].ask(); or whatever amount of elements are in the array.

    Note: if you did do this then you'd have a real purpose to using a class, and could add variables to flag if the person already answered the question, how long it took, how many guesses, the id of the question (use a static variable to keep track of the amount of instances), etc. You turn it into a linked list, with nodes, to give it extra functionality (search by id, or question name, part of the question, blah blah blah).

    ----------------------------------------

    If thats not an option.. just call the rand() function and use that value in some if/if else statements that lead to the goto's. It may be the simplest way, but I wouldn't call it the best - at all.

    Code:
    int random = rand() % 3;
    if(random == 1)
      goto q1;
    else if(random == 2)
      goto q2;
    else if(random == 3)
      goto q3;
    else
      cout << "Sorry, question doesn't exist";
    Alternatively you could use a switch statement and use rand to go to one of the cases, and use a while statement around the switch statement to go back to ask more questions. Usually theres an equally good or better way to do something with if/switch/while statements than using goto.

    ---------------------------------------

    I would suggest to stop using c-style strings (char[]) and use the string object, and import <string> not <string.h>. Theres a good tutorial on the main site on how to use them, and its much easier than c-style strings.
    Last edited by Dae; 11-24-2005 at 06:26 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    rand() is in <cstdio>, and srand() is in <cstdlib>.

    Try the FAQ here: http://faq.cprogramming.com/cgi-bin/...arch&match=all. There's also a page on it at Prelude's site: http://www.eternallyconfuzzled.com/articles/rand.html.

    BTW, SlyMaelstrom, your avatar's down again. It must be hosted remotely.
    Last edited by dwks; 11-25-2005 at 02:42 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21