CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2010
    Posts
    5

    multiple choice test using C++

    Hello, for a final project in a class I am making a multiple choice test ABOUT C++, using C++. What I'm doing is using while loops and switch structures. The while loops are supposed to check whether the integer "error" is changed from it's default value (0), and the switch structures have 4 cases, A/B/C/D, which are of course the answers. If error is equal to 1 after the question is asked (the switch structure ends), the while loop will repeat the question. 2 problems thus far, 1.) the switch structure isn't recognizing my input as a valid case (it always defaults and sets error = 1), and 2.) when error is set to 1, the while loop isn't repeating the question! It is quitting the program.

    There will be 20 questions, but I need to get the code right, then I can add the rest of the questions. Because the code is already in excess of 300 lines, I've attached the .CPP file. Thanks in advance for any assistance you can give me!
    Attached Files Attached Files

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: multiple choice test using C++

    Just with a really quick glance your code could be simplified immensely using functions and loops.

    Why not just create functions that ask the question and get the answer. Set up two arrays, one with the questions, one with the answers. Basically, you'd have

    Code:
    for(int i = 0; i < 20, i++)
    {
        AskQuestion(i);
        GetAnswer(i);
    }
    That would be your main function pretty much.
    Your GetAnswer function wouldn't need switch statements. It would just loop until the user entered the correct answer. Your whole program could be done in about 50 lines.

    Even if you don't do that, and I strongly recommend you do, there's no need for switch statements. Just us an if.
    Code:
    if(answer != 'd' && answer != 'D')
        cout << "wrong";
    else
       cout << "right";
    Wrap that up in a while loop.
    Last edited by GCDEF; February 25th, 2010 at 10:25 PM.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: multiple choice test using C++

    Why is letter choice a 2 char array?

    letterChoice[2] is invalid memory. Valid indexes for an array of length 2 are 0 and 1.

  4. #4
    Join Date
    Feb 2010
    Posts
    5

    Re: multiple choice test using C++

    Unfortunately we didn't do functions in my class - it is an introductory course. The other problem is that the final project's minimal requirements are switch, if/then/else, loop and counter..and I can't use anything we haven't gone over in the class.

    Personally I fully agree on using a simple if statement wrapped in a while loop. But it doesn't seem to fit the requirements I'm bound to -- I have to use switch in SOME way.

  5. #5
    Join Date
    Feb 2010
    Posts
    5

    Re: multiple choice test using C++

    Also, I set letterChoice to 2, because from what I've learned, if you need 1 letter stored, you need a place for 1.) the letter and 2.) the null terminator ( \0 ). Is this wrong?

  6. #6
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: multiple choice test using C++

    Quote Originally Posted by Belinrahs View Post
    Also, I set letterChoice to 2, because from what I've learned, if you need 1 letter stored, you need a place for 1.) the letter and 2.) the null terminator ( \0 ). Is this wrong?
    For C-Style char arrays, yes that would be generally correct, but not all char* is necessarily null terminated.
    Unfortunately we didn't do functions in my class - it is an introductory course. The other problem is that the final project's minimal requirements are switch, if/then/else, loop and counter..and I can't use anything we haven't gone over in the class.
    So the requirement is that you use only while/if-else/switch and each has to be used at least once, correct?
    One way would be to use while to loop as many as times there are questions, use switch to print questions, and if-else to determine the answer. It's still verbose but probably 2/3 less the lines allowed (300 lines)

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: multiple choice test using C++

    Quote Originally Posted by potatoCode View Post
    For C-Style char arrays, yes that would be generally correct, but not all char* is necessarily null terminated.

    So the requirement is that you use only while/if-else/switch and each has to be used at least once, correct?
    One way would be to use while to loop as many as times there are questions, use switch to print questions, and if-else to determine the answer. It's still verbose but probably 2/3 less the lines allowed (300 lines)
    That sounds like a better approach. At least you should consolidate your answer checking to
    Code:
                       case 'A':
                       case 'a':
                      case 'C':
                       case 'c':
                       case 'D':
                       case 'd':
                             cout << "Sorry, that's the wrong answer. The correct answer is B!" << endl;
                            error = 0;
                            break;
                       case 'B':
                       case 'b':
                            cout << "Great! That's the right answer!" << endl;
                            correctAnswers++;
                            error = 0;
                            break;
                       default:
                            cout << "That isn't a valid answer. Please try the question again." << endl;
                            error = 1;
                            break;
    but again, I think an if statement is more appropriate here.

  8. #8
    Join Date
    Feb 2010
    Posts
    5

    Re: multiple choice test using C++

    I redid a lot of the program using the tips you guys gave and I'm having another problem. Seems the loops are looping..repeatedly, extremely fast. Compile this for yourself and see the mess that happens.

    Unless you guys have any tips I'm likely rewriting the whole thing using if loops, and perhaps, to fit the requirement, a case loop that prints different messages based on the score the user got. The thing's due on Monday, unfortunately..

    Thanks for the tips - hoping I can get a few more pointers here to get me in the right direction.
    Attached Files Attached Files

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: multiple choice test using C++

    Same problem I mentioned before. Don't make letterchoice an array.

    The greatest allowable index for an array is 1 less than its size. Since you declared letterChoice as char[1], which doesn't really make sense to do, the ONLY allowable index for it is 0. letterChoice[1] references memory your app doesn't own. Just make letterChoice a char and stop trying to use it as an array.

    I don't use the streams much so I'm not sure what cin.get does but cin >> letterChoice ought to be sufficient.
    Last edited by GCDEF; February 26th, 2010 at 01:27 PM.

  10. #10
    Join Date
    Feb 2010
    Posts
    5

    Re: multiple choice test using C++

    Kudos GCDEF! I simply did char letterChoice, used cin >> letterChoice, and I also needed to set error to 1 before each while loop. (I could change that but it works, and that's what counts)

    I'm indebted - I'll be able to turn in this final project and get a good score!

    I'm sure I'll be back for help (besides just random posts and surfing the forums), but hopefully not too soon

  11. #11
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: multiple choice test using C++

    Quote Originally Posted by GCDEF View Post
    That sounds like a better approach. At least you should consolidate your answer checking to
    Code:
                       case 'A':
                       case 'a':
                      case 'C':
                       case 'c':
                       case 'D':
                       case 'd':
                             cout << "Sorry, that's the wrong answer. The correct answer is B!" << endl;
                            error = 0;
                            break;
                       case 'B':
                       case 'b':
                            cout << "Great! That's the right answer!" << endl;
                            correctAnswers++;
                            error = 0;
                            break;
                       default:
                            cout << "That isn't a valid answer. Please try the question again." << endl;
                            error = 1;
                            break;
    but again, I think an if statement is more appropriate here.
    That surely is one way.
    I was thinking of using the char* as the "answer sheet" in a way such that
    Code:
     // each letter represents the correct choice for the questions
        // answer for Q1 is 'b', Q2 is 'a', Q3 is 'd'
        const char* answerSheet = "bad";
    would assign a single task to while/if-else/switch in
    Code:
    while( /* while there are questions */ )
        {
            switch(currentQuestion)
            {
                case 1:
                    cout << "Question 1: What command prints something to the screen?" << endl;
                    cout << "A: cin" << endl << "B: cout" << endl << "C: char" << endl << "D: print" << endl;
                    break;
                case 2:
                    cout << "Question 2: Which of the following is the default output device?" << endl;
                    cout << "A: screen" << endl << "B: printer" << endl << "C: mouse" << endl << "D: keyboard" << endl;
                    break;
            }
            cin >> letterChoice;
    
            // modify to make it case insensitive
            if(letterChoice == answerSheet[currentQuestion - 1])
            {
                cout << "Great! That's the right answer!" << endl;
                // ...
            }
            else
            {
                cout << "Sorry, that's the wrong answer. The correct answer is ";
                // print the correct answer
                // do whatever else is necessary
            }
             ++currentQuestion;
        }

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: multiple choice test using C++

    I know what you meant. I thought your approach made more sense than his. I was just showing him how to simplify is switch statements if he chose to stick with what he had.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured