Unable to input with spaces into char array from struct.

closed account (4ENRDjzh)
Hey guys. I am currently attempting an assignment that requires the user to input data into a structure.However, I can't manage to properly insert the inputs with space.(name,address etc). cin.getline spewed an error and I am out of ideas.. Here is the code..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  struct customer
    {
       char nameC[20];
       char addressD[35];
       char telePhone[12];
       int accountB;
    };

void getInput(customer& );//declarations

for(x=0;x<10;x++)
getInput(account[x]);//In main function,skipped others not important


//The following function the problem.
void getInput(customer &account)
{
    
    cout<<"What is the name of the customer?";
   cin>>(account.nameC);
    cout<<"What is the customer's address ?"<<endl;
    cin>>account.addressD;
    cout<<"What is the customer's phone number?"<<endl;
    cin>>account.telePhone;
    cout<<"What is the customer's account balance?"<<endl;
    cin>>account.accountB;

    return;

}//When entering the name or address with space, the loop ignores  the subsequent prompt  and considers I have inputted both name and address.

//cin.getline spewed |error: no matching function for call to 'std::basic_istream<char>::getline(char [20])'|


Thanks in advance.
//When entering the name or address with space, the loop ignores the subsequent prompt and considers I have inputted both name and address.

That's becuase a cin operation stops at whitespace.
http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/



Last edited on
getline is the easiest way to go, so look at the documentation/ask google to find how to use it.
A pointer: with cin.geline you have got it backwards (which is why no matching function could be found).
1.
1
2
3
4
5
6
7
struct customer
    {
       char nameC[20];
       char addressD[35];
       char telePhone[12];
       int accountB;
    };


Should be :
1
2
3
4
5
6
7
struct customer
    {
       char nameC[40];
       char addressD[70];
       char telePhone[25];
       int accountB;
    };


2.
1
2
3
4
5
6
7
8
cout<<"What is the name of the customer?";
cin>>(account.nameC);
cout<<"What is the customer's address ?"<<endl;
cin>>account.addressD;
cout<<"What is the customer's phone number?"<<endl;
cin>>account.telePhone;
cout<<"What is the customer's account balance?"<<endl;
 cin>>account.accountB;


Should be :
1
2
3
4
5
6
7
8
cout << "What is the name of the customer? : ";
cin.getline(account.nameC, sizeof(account.nameC));
cout << "What is the customer's address ? : ";
cin.getline(account.addressD, sizeof(account.addressD));
cout << "What is the customer's phone number? : ";
cin.getline(account.telePhone, sizeof(account.telePhone));
cout << "What is the customer's account balance? : ";
cin >> account.accountB; 
Last edited on
Though I don't see the point of changing the data size within structure??

Well, with a decent amount of space, the user can input longer name and address (it is up to you).
Topic archived. No new replies allowed.