Reference no: EM132183823
I have completed a program but the following piece of code is acting wonky, and I dont know how to fix it.
printf("Please enter the contact's postal code: ");
scanf(" %7[^n]%*c", conAdd->postalCode);
//clearKeyboard();
//if (*conAdd[fix].city != '') {
//conAdd->city = '';
//}
printf("Please enter the contact's city: ");
scanf(" %40[^n]%*c", conAdd->city);
Ignoring the //s, this code is meant to prompt the user for postal code, accept input, then do the same for city. It works except for one thing: if the user enters a postal code larger then what it can take, then it will print "Please enter the contact's city" and NOT accept user input, instantly moving on to the next line in the code. Then, when the program shows the saved contact information, the part of the postal code that was too long is now in the place of the city variable. So if you were to enter h4f 2jt 5ky as a postal code, then 5ky would instantly become the city.
The //s show my ways of attempting to fix the problem. clearkeyboard() clears the contact buffer and makes it work if the postal code is too long, but will also print out a blank line if the postal code is correct. I do not understand why or how but it is no fix here.:
The if statement was an attempt at going around the problem. I thought if I could always delete what is inside city between the scanf for the postal code and the prompt for the city, then that would fix the problem. But I do not know how to get that to work.
The problem appears to be how to clear the buffer without causing a blank line, but I would be glad to learn how to avoid the problem from happening in the first place or how to fix the if statement so that it works! Any help would be greatly appreciated.