Ich habe diesen einfachen C-Code in Microsoft Visual C++ 2010.Warum generiert dieser c-Code keine erwartete Ausgabe?
#include<stdio.h>
#include<conio.h>
void main()
{
char title[20], artist[30];
int numtrack, price;
char type;
printf("Enter the title of CD \n");
scanf("%s",title);
printf("\nName of the artist \n");
scanf("%s",artist);
printf("\nEnter the type of CD(enter a for album and s for single)\n");
scanf("%c",&type);
printf("\n Enter the number of tracks \n");
scanf("%d", &numtrack);
printf("\n Enter the price of the cd \n");
scanf("%d", &price);
printf("%s\n%s\n%c\n%d\n%d\n",title, artist, type, numtrack, price);
getch();
}
Es ist außer Betrieb gesetzt ist für Typ-Variable für die Eingabe
Enter the title of CD
ranjit
Name of the artist
mahanti
Enter the type of CD(enter a for album and s for single)
Enter the number of tracks
4
Enter the price of the cd
4
ranjit
mahanti
4
4
Ich kann nicht verstehen, warum es nicht wartet geschrieben? Kann mir das bitte jemand erklären? Danke im Voraus.
für Typen verarbeitet werden, aber können Sie mir die Logik bitte erklären? wenn ich das Extra \ n entfernt habe, warum wurde das Problem nicht gelöst? – narayanpatra
Das führende Leerzeichen in der scanf-Musterzeichenfolge verursacht, dass scanf alle Leerzeichen (Leerzeichen/Zeilenumbrüche/Tabs/etc) verbraucht, bevor das Zeichen gelesen wird, das in 'type' gespeichert werden soll. Das '% s'-Mustertoken überspringt Whitespace und liest eine Zeichenkette, dann * stop * bei Whitespace (eine Newline), ** ** verbraucht jedoch nicht den Whitespace ** und belässt es im Eingabepuffer. Sie müssen daher dieses Leerzeichen überspringen; das Leerzeichen vor '% c' wird das tun. – cdhowie
Danke für die Hilfe. Ich habe es verstanden. – narayanpatra