When the input is greater than 9 the program reads only the first digit

Asked

Viewed 78 times

0

When the user enters with a larger number, such as 13 or 28, 50. the compiler understands that it is being typed 1.2 and 5 respectively. how do I fix this problem ?

/*EXERCÍCIOS UTILIZANDO O CAMANDO WHILE
1 - Em uma eleição presidencial existem quatro candidados. Os votos são informados por código. Os dados utilizados para a votação obedecem à seguinte codificação:
    > 1,2,3 e 4 = voto para os respectivos candidatos;
    > 5 = voto nulo;
    > 6 = voto em branco;
    > 0 = Encerrar votação;
Elabore um algoritmo que calcule e escreva:
    > o total de votos para cada candidato e o seu percentual sobre o total;
    > o total de votos nulos e seu percentual sobre o total;
    > o total de votos em branco e o seu percentual sobre o total;*/ 

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>

using namespace std;

main (){

int cand1=0, cand2=0, cand3=0, cand4=0,votnulo=0, votbranco=0, totalvot;
bool continua=true;
char cand;

        while(continua==true){
        cout << "Eleicao Presidencial\n" << endl;
        cout << "1 = Candidato 1\n2 = Candidato 2\n3 = Canditado 3\n4 = Canditato 4\n5 = Voto nulo\n6 = Voto em branco\n0 = Encerrar votacao\n" << "Digite o Numero do seu candidato:";
        cin >>cand;
        system("cls");

        if (cand =='0')
        {
            continua=false;
            cout << "Votacao Encerrada" << endl;
        }   
        else if(cand =='1'){

        cout << "Voce votou no canditado 1!" << endl;
        Sleep(4000);
        system("cls");
        cand1 +=1;
        }

        else if (cand =='2'){

        cand2+=1;
        cout << "Voce votou no canditado 2!" << endl;
        Sleep(2000);
        system("cls");
        }
        else if(cand =='3'){
            cand3+=1;
        cout << "Voce votou no canditado 3!" << endl;
        Sleep(2000);
        system("cls");
        }
        else if(cand =='4'){
        cand4+=1;
        cout << "Voce votou no canditado 4!" << endl;
        Sleep(2000);
        system("cls");      
        }
        else if (cand =='5'){
            votnulo+=1;
        cout << "Votou Nulo!" << endl;
        Sleep(2000);
        system("cls");
        }
        else if (cand == '6'){
        votbranco+=1;
        cout << "Voce votou em Branco!" << endl;
        Sleep(2000);
        system("cls");
        }

        else {
            cout << "Comando invalido!" << endl;
            Sleep(2000);
        system("cls");

        }
}
    totalvot = cand1+cand2+cand3+cand4+votnulo+votbranco;


    cout << "Votos Canditado 1: " << cand1 << " - Percentual de votos - " << (cand1*totalvot)/100.00 <<  "%" << endl;
    cout << "Votos Canditado 2: " << cand2 << " - Percentual de votos - " << (cand2*totalvot)/100.00 << "%" << endl;
    cout << "Votos Canditado 3: " << cand3 << " - Percentual de votos - " << (cand3*totalvot)/100.00 << "%" << endl;
    cout << "Votos Canditado 4: " << cand4 << " - Percentual de votos - " << (cand4*totalvot)/100.00 << "%" << endl;
    cout << "Votos Brancos: " << votbranco << " - Percentual de votos - " << (votnulo*totalvot)/100.00 << "%" << endl;
    cout << "Votos Nulos:  " << votnulo << " - Percentual de votos - " << (votbranco*totalvot)/100.00 << "%" << endl;





    return 0;
}
  • By chance you are not storing the user input in a char, which accepts only one character?

  • Yes, I understand that it is a logical error, the compiler recognizes only the first character, but so far I have not found a way to "treat" it. I tried to make switch case , but in case the user enters with some letter or other character type other than the valid numbers, the program does not recognize.

  • 2

    And why cand is not the type int if the input is an integer number?

  • I did this and the program recognized the votes, the problem and that when it enters with a letter, it closes the program, and as if a letter has the value 0. and 0 closes the program. how can I treat this?

  • If you want to allow anything to be typed and accept only valid characters then read a string and treat what has been read by checking if it contains any non-valid characters. So decide what to do in such a situation.

1 answer

0


Use integers and make data entry consistency, as in the example below.

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

int main()
{
  int opt;

  for (;;)
  {
    cout << "* digite um numero e tecle enter: ";
    if (cin >> opt)
    {
      // ok, leu numero
      cout << "* opcao digitada: " << opt << endl;
    }
    else
    {
      // erro, nao leu numero, ocorreu erro de digitacao
      cout << "* erro, digite apenas numeros\n";
      cin.clear(); // limpa erro de formatacao
    }

    // ignora restante da linha (ate' 1000 caracteres)
    cin.ignore(1000, '\n');
  } // for

} // main
  • I entered if before Cin as in the example above, but when typed a letter, the program enters Else, but is "locked" as if the program ends, but the results do not appear, only the courses are locked. IF(Cin >>cand;)

  • put the updated code in your question...without the code it is difficult to say anything

  • Okay, I’ve edited the code. It looks like you’re entering an infinite loop

  • you are not treating the error, as shown above...when error occurs you need to do the "Cin.clear()", otherwise it will loop even...put also the "Cin.ignore" as shown in my example

  • Thank you so much for your attention, I put Cin.clear() and Cin.ignore. it worked!! but I still don’t quite understand how the check works but it worked hahah

Browser other questions tagged

You are not signed in. Login or sign up in order to post.