-2

如何提示用户输入国家名称,例如Malaysia将变量替换country1Malaysia

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
    writeRecipe (int fd)
   {
     readData ();
     char country1[LINE_MAX] = "China";
     static char* line1 = "Starting Country Search Client ...\n\n\n";
     static char* line2 = "**********************************************";
     static char* line3 = "Welcome to the Country Info Directory Service!";
     static char* line4 = "**********************************************\n\n";

     write (fd, line1, strlen (line1) + 1); /* Write first line */
     write (fd, line2, strlen (line2) + 1); /* Write second line */
     write (fd, line3, strlen (line3) + 1); /* Write third line */
     write (fd, line4, strlen (line4) + 1); /* Write fourth line */


     puts("Please enter country > ");
     fgets(country1, sizeof country1,stdin);
     printf ("\n%s Capital       : %s\n", country1, getCapital (country1));
     printf ("%s Currency Code : %s\n", country1, getCurrencyCode (country1));
   }
4

1 回答 1

3

如何提示用户输入国家名称,例如马来西亚

这是如何:

puts("Please enter a country name:");

现在,如果您想实际读取用户输入:

char buf[LINE_MAX] = "China";
fgets(buf, sizeof buf, stdin);

不要做任何其他事情。严重地。这是安全、简单和美好的。

于 2013-08-08T14:51:17.390 回答