Hi,

I have a problem with a the imput of some data on a program Im making (FYI new in c++, homework).

So basicly the progrma I have is to imput diferent type of data, this is what im doing. (Im using codeblock 8.02 in Ubuntu 10.04 )

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <cmath>
using namespace std;
struct person
{
    int cod;
    char name[40], cid[15];
}client[100];
main()
{
    int opc,exit serch, c=0, i, e;
    while(exit!=1)
    {
        cout<<"1. Imput data"<<endl;
        cout<<"2. Out data"<<endl;
        cout<<"3. Exit"<<endl;
        cout<<"Enter an opcion: "
        cin>opc;
        system("clear");
        swith(opc)
        {
            case 1:       
                cout<<"Enter code: ";
                cin>>client[c].cod;
                cout<<"Enter name: ";
                gets(client[c].name);
                cout<<"Enter cid: ";
                cin<<client[c].cid;
                cout<<"Press enter to continue"<<endl;                
                getchar();
                c++
                system("clear");
                break;
            case 2:
                cout<<"Enter code of the client: "
                cin>>serch;
                for(i=0;i<100;i++)
                {
                    if(serch=client[i].cod)
                    {
                        cout<<client[i].cod;
                        cout<<client[i].name;
                        cout<<client[i].cid;
                    }
                    else 
                    {
                        e=1;
                    }
                }
                if(e=1)
                {
                    cout<<"Client does not exist"<<endl;
                }
                cout<<"Press enter to continue"<<endl;
                getchar();
                system("clear");
                break;
            case 3:
                cout<<"Bye"<<endl;
                exit=1
                break;
            default:
                cout<<"Wrong option"<<endl;                
                break;
        }
   }
return 0;
}

Ok now the problem:
After I enter the code of the client (client[c].cod) it skips right away to ask for the client[c].cid skeeping the client[c].name imput, I was told to use gets so it will read spaces in a string, the work around I have done before is ask twice for the name:

cout<<"Enter name: ";
gets(client[c].name);
gets(client[c].name);

but I think that just a quick fix just so get an ok on the program, but is not what I should do, please help Im not sure what to do.

thanks for the quick reply, I'm sorry I did read somewhere about the fgets I forgot to apply it, so I change:

cout<<"Enter code: ";
cin>>client[c].cod;
cout<<"Enter name: ";
fgets(client[c].name,40,stdin);
cout<<"Enter cid: ";
cin>>client[c].cid;

but I still have the problem I describe before. Let me give an example, lets say I want to enter 2 for the code, Isaac Jun for the name, and k3k8 for the cid, the output I get is:

Enter code: 2
Enter name: Enter cid:

It skips right away to ask for the cid and doesnt let me enter the name, I think It may a be a dumb problem but I dont know what else to do a part from the "fix" I did before, (asking twice).

Beware when you mix input methods. >> doesn't remove the terminating newline char from the input buffer leaving them around for gets() or fgets() or getline() to see. gets() terminates input into the desired variable when a newline char or EOF is found. That means if >> is called before gets() the first thing gets() sees in the input buffer is the left over newline char from >> which means it thinks it isn't supposed to put anything into the desired variable, which is what you want it to do. So, either don't mix input methods in the same program or be sure the input buffer is clear before calling input methods that terminate on whitespace. To clear the input buffer call the ignore() method of input streams using the default arguments of 1 and newline char or some reasonably large value for the first argument or the maximum number of characters the input buffer can hold if you want to do things to the nines.

Thank you so much for the explanation, that fix my problem, I was most confused because borland dint saw a problem there and codeblocks did, I guess I still have a long way to go, the code I put in here is small compared to what I have to do,it was just to give a main idea of what im doing, so Im going to use the ignore() method for now but in the future Im going to avoid mixing imput methods. Thanks for the help.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.