Hi all. I'm a novice with C++ and I'd like to make a program that will present the user with a series of exam questions, most of which are numbers but some of which are descriptions. I want each question to repeat until the user enters the correct response (but I also want to give them the option of quiting the program so it is not an endless loop). After the user enters the correct response the next question appears. And so on. At the end of the questions, I want a summary of the questions and answers to appear.
Most of the questions will be letter symbols like variables which correspond to a numerical answer, or shore verbal questions. For example: many questions will simply be... "Lm=", "Lrr=", where "Lm" refers to, say, maximum length, etc. Lm, Lrr, and the other variables will have a constant number, e.g. 15'3 3/4", to check the users answer against. Some variables will have answers like "frame 1 to frame 5", etc. so they should be char or strings.

I was thinking of declaring one answer variable for the user for every question within a large nested conditionals, but I don't quite know how to go about it.
I was thinking something along the lines of...

if(usersAnswer=Lm), then ask the next question (which is "Lrr="); else ask Lm again and again until they get the correct answer.
if(usersAnswer=Lrr), then ask the next question (which is ...); else ask Lrr again and again until they get the correct answer.
...And so on...

Also, I would like to user to be able to enter answers like: 5'3 3/4" , which reads 5 feet and 3 and 3-quarter inches. How can you include spaces as well as single and double quotations as part of the variable and input by the user?

Can someone please help me with this? I don't know how the conditionals are supposed to look to get this done. If there is a more efficient way not involving conditionals, please let me know.

Recommended Answers

All 6 Replies

Conditionals and loops will probably be important to your program, as they are in just about any program. Remember that if the response is more than one character long that isn't a digit, then you have to use a string variable (there are several versions of string variables you could use). Single characters, including digits, could be of type char. Numerical variables longer than 1 digit could be either numerical types or strings. You can't compare C style strings with ==, you can STL strings. You can compare char and numerical types with ==.

Start your program by writing out how you want it to work with paper. Then try to convert that to code. Here's a very high overview of how you might start:

list of variables needed:
user input
stored question(s)
stored answer(s)
flags to control flow

process used:
while user wants to continue
  while incorrect answer
     ask this question 
     get input
     compare known answer to this question to input
     if correct answer
       change flag to stop question loop.
     ask if user wants to do more questions 
        if not then change flag to stop outer loop

Thank you. I've done the pseudo code but am stuck and can't figure out how to implement it. For example, if I use a while loop at the beginning of the entire conditional is it to look like...

while (usersAnswer != correctAnswer)
{
cout<<"Question 1:"; //this should be the CURRENT question
cin>>usersAnswer;
}
if(usersAnswer=correctAnswerForQuestion1)
{
cout<<"Question 2:";
cin>>usersAnswer;
}
if(usersAnswer=correctAnswerForQuestion2)
{
cout<<"Question 3:";
cin>>usersAnswer;
}
...etc. ?
This does not seem right. How can the variable correctAnswer in the while loop be overarching such that if the user enters the wrong answer, then the CURRENT question will be asked again and again until they get it right.
I was thinking of using an array but that's a whole other can of worm.

If you have any, would you mind providing some source code for programs like these?

If I see actual code instead of trying over and over again from pseudo code, I am more likely to get it...plus I'm on a time-constraint.

I think Lerner gave you a very nice explanation.

What you need is another loop. You can't do this with a single loop. You need a nested loop inside your loop.
In my opinion there are two solutions for you:

The first one is: you make a loop for each question, and you keep asking it, as long as you don't have the correct answer. You can put an if statment in the block, so the user will be able to quit. Or you can use a do-while loop, it depends on you.

The second one is: you store your questions and answers in a sequence container. An array/map/list/vector whatever, and you walk through the questions/elements one by one. You keep asking the same question as long as the answer is not good. If the answer is OK you move to the next question. You will need at least 2 loops in order to do that. One that switches the questions, and another one wich keeps asking them. But if you choose this method you have to initialize the container(s) with your questions and answers. Also you have to make a condition for the user to be able to break the loop and quit the program.

if you it is not a must to do it in c++ you can do it by flash and load the swf in your c++ APPLICATION
I think it is more easier

commented: That's the most silly solution I've ever heard, I think it will be MORE difficult than just using two nested loops ... +0
array of questions
array of correct answers
bool askAnotherQuestion = true
bool incorrectAnswer = true
int i = 0; 
int totalQuestions = x;
while askAnotherQueistion and i < totalQuestions
{
    while incorrectAnswer
     {
         display question i
         input anwer
         if answer equals answer i
             increment i
             change flag
             output statement
         else
              output statement 
              ask if another answer attempt desired
               if no
                   change flag
     }
      ask if another question desired
      if no
         change flag
}

Keep in mind that when you have a 'string' as an answer -- for example 5' 2 3/4" -- each of these answers will be wrong:

5', 2 & 3/4"
5'  2 3/4"
 5' 2 3/4"
 5 ft, 2 3/4 in

IOW, a string input may not be a good option. Multi-choice might be the best solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.