Bug when using cin command inside a loop

Question:

When I run this code it bugs , I type 1 and it enters the register function but it doesn't pause to read the data (it keeps printing things over and over) when I change cin to scanf it works). Is it possible to 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;

                }

            }


            }

Answer:

To work, just put cin.ignore() before cin.get(a[c].nome,100) . This is because when you choose option 1 you press ENTER and cin catches this ENTER, so when you do cin.ignore() you will be "ignoring" this ENTER.

        #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.ignore();
            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;

            }

        }


        }
Scroll to Top
AllEscort