When I run this code it buga , I type 1 and it enters the function register but not pause to read the data (it keeps printing things without stopping) when I change cin
by scanf
it works). Can you make it work with the cin command?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;
typedef struct {
char nome[100];
}pessoa;
pessoa a[100];
int c=0;
void cadastro();
void imprimi();
void cadastro(){
std::cout<<"Digite o nome:"<<endl;
cin.get(a[c].nome,100);
c++;
}
void imprimi(){
cout<<"\n\n\n";
for(int i=0;i<c;i++){
std::cout<<"Nome:"<<a[i].nome<<endl;
std::cout<<"------------"<<endl;
}
}
main(){
int op=0;
while(op!=3){
std::cout<<"\n\t---IMC---"<<endl;
std::cout<<"1-Cadastrar usuario:"<<endl;
std::cout<<"2-Listar usuarios:"<<endl;
scanf("%d",&op);
fflush(stdin);
switch(op){
case 1:
cadastro();
break;
case 2:
imprimi();
break;
case 3:
exit(1);
break;
default:
std::cout<<"digite um numero valido"<<endl;
break;
}
}
}