Ich bin auf einem Teil einer Aufgabe fest, die ich schreibe.Brauchen Sie Hilfe über die Eingabe von Zeichenfolgen und Drucken
Als ich in der postal tippe (die A8A 4J4
ist) es zeigt
Es soll sein:
Postal code: A8A 4J4
City: Toronto
Es ist Überspringen der Option in die Stadt kommen.
Ich habe versucht %[^\n]
und es überspringt immer noch die Option, die Stadt eingeben.
Mein aktueller Code ist:
if (option == 'y' || option == 'Y') {
printf("Please enter the contact's apartment number: ");
scanf("%u", &address.apartmentNumber);
printf("Please enter the contact's postal code: ");
scanf("%s", &address.postalCode);
}
if (option == 'n' || option == 'N') {
printf("Please enter the contact's postal code: ");
scanf("%s", &address.postalCode);
}
printf("Please enter the contact's city: ");
scanf("%40s", address.city);
printf("Postal code: %s\n", address.postalCode);
printf("City: %s\n", address.city);
sah ich einen Beitrag über das schon aber die Antworten dort nicht helfen. Ich habe bereits versucht, die [^ \ n] in meinem scanf
.
Mögliche doppelte findet: https://stackoverflow.com/questions/ 28197006/the-scanf-function-the-specifier-s-und-die-neue-line oder https://stackoverflow.com/questions/29905009/reading-newline-from-previous-input-when-reading-from- keyboard-with-scanf –
Mögliches Duplikat: https://stackoverflow.com/questions/45394221/n ewline-character-n-in-scanf oder https://stackoverflow.com/questions/15740024/scanf-asking-twice-for-input-while-i-expect-it-to-ask-only-once –
Sie könnten Probieren Sie 'scanf ("% s ", & address.postalCode);' um den nachgestellten Whitespace oder 'scanf ("% 40s ", address.city) zu essen;' um den führenden Whitespace zu essen. –