Bug when using loop control

3

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;

                }

            }


            }
    
asked by anonymous 18.06.2016 / 02:02

1 answer

1

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 picks up this ENTER, so when you do cin.ignore() you will be "ignoring" that 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;

            }

        }


        }
    
22.06.2016 / 21:35