0

I'm doing a quiz program in C++ programming language. I have used a for loop to go through each switch case but when I run the program, it's just keep looping the case 0 and can't stop the loop after I answering my quiz. How should I solve it ?

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

void quiz_count () ;
void display_question () ;
void question (string question , string a , string b , string c , string d , char correct_answer) ;
void result () ;

int question_num = 0 ;
int correct = 0 ;
int wrong = 0 ;


int main ()
{
    display_question() ;
    return 0 ;
}

void quiz_count ()
{
    system("cls") ;
    cout << "Question Number: " << question_num << "\t\t Correct Answer:" << correct << "\t\t Wrong Answer:" << wrong << endl << endl ;
    display_question () ;
}

void display_question()
{
    for (int i=0; i<10 ; i++)
    {
        switch (i)
        {
            case 0 :
                question ( "1) What is recycling?" , "Buying new clothes" , "Collecting and using materials to make something new" , "Throwing things in garbage can" , "Selling items" , 'b' ) ;
                break ;
            case 1 :
                question ( "2) What are the 3R's of the recycling?" , "Redirect, Rude, Round" , "Respectful, Responsible, Right" , "Reduce, Reuse, Recycle" , "Rewrite, Rewind, Respond" , 'c') ;
                break ;
            case 2 :
                question ( "3) What goes into the green bin?" , "plastic" , "glass" , "cans" , "paper" , 'b' ) ;
                break ;
        }
    }
    result () ; 
}

void result ()
{
    system("cls") ; 
    cout << "The total of question is :" << question_num << endl ;
    cout << "The correct answer from you is :" << correct << endl ;
    cout << "The wrong answer from you is :" << wrong << endl ; 
}

void question (string question , string a , string b , string c , string d , char correct_answer)
{
    cout << question << endl ;
    cout << "A. \t" << a << endl ;
    cout << "B. \t" << b << endl ;
    cout << "C. \t" << c << endl ;
    cout << "D. \t" << d << endl ;
    char answer ;
    cout << "Please enter your answer here :" ;
    cin>>answer ;
    if (answer == correct_answer)
    {
        correct ++;
    }
    else 
        wrong ++ ;
    question_num ++ ;
    quiz_count() ;
}
Filburt
  • 17,626
  • 12
  • 64
  • 115
Code Nauh
  • 23
  • 1
  • 1
    You'll be glad to hear you don't need anyone's help to figure this out, just a tool you already have: your debugger! This is exactly what a debugger is for. It [runs your program, one line at a time, and shows you what's happening](https://stackoverflow.com/questions/25385173/), this is something that's every C++ developer must know how to do. With your debugger's help you'll able to quickly find all problems in this and all future programs you write, without having to ask anyone for help. Have you tried using your debugger, already? If not, why not? What did your debugger show you? – Sam Varshavchik Jan 02 '22 at 15:06
  • 1
    `display_question` is calling itself recursively: `display_question` calls `question` that calls `quiz_count` that calls `display_question`. Each nested call to `display_question` starts looping anew. Most likely, you want to just drop `display_question` call from `quiz_count` – Igor Tandetnik Jan 02 '22 at 15:09

1 Answers1

1

The issue isn't the loop + switch, but the infinite recursion you're using:

  1. display_question() calls question()
  2. question() calls quiz_count()
  3. quiz_count() calls display_question(), so you're back at step 1.

The values of i you're observing are simply the values for different calls to the display_question function.

You'll probably get the desired outcome by removing the display_question() call from quiz_count. However the combination of loop and switch isn't a good choice here. the following implementation yields the same results in addition to being easier to understand:

void display_question()
{
    question ( "1) What is recycling?" , "Buying new clothes" , "Collecting and using materials to make something new" , "Throwing things in garbage can" , "Selling items" , 'b' ) ;
    question ( "2) What are the 3R's of the recycling?" , "Redirect, Rude, Round" , "Respectful, Responsible, Right" , "Reduce, Reuse, Recycle" , "Rewrite, Rewind, Respond" , 'c') ;
    question ( "3) What goes into the green bin?" , "plastic" , "glass" , "cans" , "paper" , 'b' ) ;

    result () ; 
}
fabian
  • 80,457
  • 12
  • 86
  • 114