[英]Read a single user input using scanf and convert it into appropriate data type
[英]How to read and store data which includes space bar that user input using **scanf** in C
我無法輸入包含空格鍵的數據,使用C
克里昂(C99)
輸入收貨人姓名,最終目的地和包裹狀態
char name_location_status[90];
char recipient[30];
char final_destination[50];
char status[10];
printf("Please enter , 1> Recipient-, 2> Final Destination- and 3>Delivery status :\n");
scanf("%s", name_location_status);
const char upper[2] = "-";
char *token;
token = strtok(name_location_status, upper);
int i=0;
while( i!=3,token != NULL )
{
(i==0) ? strcpy(recipient, token) :
(i==1) ? strcpy(final_destination, token) :
strcpy(status, token) ;
i++;
token = strtok(NULL, upper);
}
如果輸入(Rat-House-Arrived)輸出(Rat House Arrived),則程序運行良好,但是如果輸入包含空格(L'-Rat-Kitchen House-Not到達),則輸出無效(L )
那么有沒有辦法使用scanf來輸入這樣的數據?
排成一行的 Blockquote(L Rat-Kitchen House-未到)
如果否,您能告訴我一種方法嗎?要在一行中輸入此類數據
而不是scanf使用似乎更合適的getline()函數
char *line = NULL;
getline(&line);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.