Reputation: 57
I am writing a small program with a database organized with the following struct:
typedef struct {
char fname[20];
char lname[20];
char phone[12];
char address[50];
} database;
When I ask to input all the values of one entry, I do it as below (the var save is 0 at the beginning of the program):
if(option == 1)
{
printf("\nPlease input each of the following parameters separated by space: \n");
printf("Firstname Lastname Phone Address: \n");
scanf("%s %s %s %s", &list[save].fname, &list[save].lname, &list[save].phone, &list[save].address);
save++;
printf("\n\n!!!!! DONE !!!!!\n\n");
}
The problem is, I want the last string input (address) to be able to store a complete sentence with spaces. When I input something like for example Andres Guerra +15551234 55555 AB Ave., UT. It will only save in list[save].address the 55555, the rest of the things stays stored for the next input, causing storage discrepancies.
How can I save in the address struct element a complete sentence with spaces included?
NOTE: I also tried making an additional scanf of %s with the address alone, but it did not work either.
If I use Country instead of Address, the program works fine.
Upvotes: 0
Views: 87
Reputation: 712
Try using
scanf("%s %s %s %49[0-9a-zA-Z ]",
&list[save].fname,
&list[save].lname,
&list[save].phone,
&list[save].address
);
I hope this helps.
Upvotes: 3