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

101
Vistas
Using switch statements, with terminating char, in this case '='

I'm working on a program that takes in values and operands and calculates the total of said values. This program will loop until the user enters '='.

I believe I am close but am missing something to make it function correctly. Most likely I don't need the while loop but I am unsure.

Example of user input:

'6'
'+'
'5'
'-'
'3'
'-'
'7'
'='

Total is 1.

int main()
{
    float num1, num2;
    char change;
    float total;

    cout << "Welcome to your friendly neighborhood accumulator! Please input your expression, starting with an operand and type in  '='  when completed." << endl;
    

    while(change != '='){       
        cin >> num1;
        cin >> change;
        cin >> num2;

        switch(change){
            case '+':
                total = num1 + num2;
                break;
            case '-':
                total = num1 - num2;
                break;
            case '=':
                cout << total;
                break;
            default:
                cout << "Incorrect operator";
                break;
        }
    }

    cout << "Thank you for using the accumulator!" << endl;
   
   return 0;
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

while(change != '='){   
    cin >> change;
    if(change != '='){
        cin >> num2;
    }
    switch(change){
        case '+':
            total = total + num2;
            break;
        case '-':
            total = total - num2;
            break;
        case '=':
            cout << total << endl;
            break;
        default:
            cout << "Incorrect operator";
            break;
    }
}
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