Doubt in C++ program entry command

Asked

Viewed 78 times

1

I am learning functions now in c++ and I went to try to run this program. It turns out that when I run it just doesn’t let me type in the value of cin>>p; it simply ignores the command and switches to the next one. I don’t know what’s going on. This is the program:

#include <iostream>
#include <conio.h>
using namespace std;

float p,mai=0;
int i;
char nome,pesado;

void topo(){
    system("cls");
    cout<<"------------------------------------"<<endl;
    cout<<" D E T E C T O R  DE  P E S A D O"<<endl;
    cout<<" Maior peso ate agora: "<<mai<<" kg"<<endl;
    cout<<"------------------------------------"<<endl;
}

int main (void) {
    topo ();
    do{
        i+=1;
        cout<<"Digite o nome e depois o peso: "<<endl;
        cin>>nome;
        cout<<"\n";
        cin>>p; 
        if (p>mai){
            mai =p;
            pesado =nome;
        }
    topo();
    }while(i<=5);
    topo();
    cout<<"A pessoa mais pesada foi "<<pesado<<"com "<<mai<<" kg"<<endl;


}
  • Is there any way you can put the input you are going through? I need it Verbatim, with neither left nor missing a space or enter

  • Look how Marconi put the input of the data in his doubt in this question: https://answall.com/q/211315/64969

  • @Jeffersonquesado this way ?

  • You left your code unreadable and didn’t put any input

  • When you talk about input, you talk about what exactly ? regarding the code, was to leave it the way it was ? I didn’t quite understand what you asked me to do, sorry, I’m starting to agr.

  • When speaking input is in the sense of what will be passed to the program by the default input (usually the keyboard). For example, look at this question http://br.spoj.com/problems/COFRE/, it has the input example. This other problem has 3 input examples: http://olimpiada.ic.unicamp.br/pratique/programacao/nivel2/2007f1p2_choc

  • and the "Cin>" commands are no longer sufficient for input ?

  • I didn’t ask how you did it to get the down payment, but what’s the down payment. cin >> nome; indicates how you received programmatically input, but I don’t know if you typed in p or Pedro or Pedro Alonso or umgrandenomedetestequeultrapassaacapacidadedeleituradaparadadastring

  • Friend, Before the Cout<<" n" put fflush(stdin);

  • nome is the type char so your name can only be a letter. And in that sense the program is working well. If you want to read a name change to the type string making the corresponding include #include <cstring>

  • Andersonhenrique I did here and it worked, in fact I didn’t even know what fflush(stdin) was, now I know, thanks. Isac I hadn’t noticed that either, I wanted to read a name myself, thanks for the tip.

  • Posso Colocar como Resposta então @Pedroafonso ?

Show 7 more comments

1 answer

0

You have to pass a pointer to a character, not simply a character, so it looks like this:

char *nome;

Browser other questions tagged

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