Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

108
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda