Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

266
Vistas
How to use a switch case inside a for loop?

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() ;
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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 () ; 
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda