C++ PROGRAM JUMPING INPUT

Asked

Viewed 64 times

-2

I started studying C++ in college and the teacher asked to make a small script to register 3 people and at the end print the chosen person. I made the script with simple inputs and when I run it just starts the first one and skips all the others and even at the end the result of the variable Name only takes the first letter typed.

#include <iostream>

using namespace std;

int main(int argc, char** argv) {

    char nome1,nome2,nome3;

    char sexo1,sexo2,sexo3;

    int altura1,altura2,altura3;

    int cpf1,cpf2,cpf3;

    int peso1,peso2,peso3;

    cout<<"DIGITE O PRIMEIRO NOME: "<<endl;

    cin>>nome1;

    cout<<"DIGITE O CPF : "<<endl;

    cin>>cpf1;

    cout<<"DIGITE O SEXO [M/F] : "<<endl;

    cin>>sexo1;

    cout<<"DIGITE O PESO: "<<endl;

    cin>>peso1;

    cout<<"DIGITE A ALTURA EM CENTIMETROS : "<<endl;

    cin>>altura1;

    cout<<"QUAL PESSOA DESEJA MEDIR O IMC: "<<endl;

    cout<<"1 - "<<nome1<<" CPF - "<<cpf1<<endl;

    return 0;
}
  • Utilize string in place of char in the declaration of name variables.

  • Note also that you only treat name1, cpf1, sex1, weight1 and height1. Other variables, with suffix 2 and 3, are declared but ignored.

1 answer

0

The primitive type char accepts only one character. If you type above one, the input buffer waits for the next cin and feeds it to the next character. I recommend using the object type string or an array of characters defined as char[100] nome;.

Browser other questions tagged

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