Home > Enterprise >  How to use a switch case inside a for loop?
How to use a switch case inside a for loop?

Time:01-03

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() ;
}

CodePudding user response:

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 () ; 
}
  • Related