[英]printing out user's input and next-line is included in the input
struct{
int year;
int unit;
int NumberOfSubject;
float gpa;
char semester[5];
char NameOfSubject[60][50];
char grade;
char name[40];
}student;
char CreatAccount(void)
{
int i;
char ans;
database= fopen("database.rtf", "w");
printf("\tThis is registration page\n");
printf("Please enter your name\n");
fgets(student.name,40,stdin);
printf("Is your name %s?, Y/N\n", student.name);
scanf("%c", &ans);
if (ans == 'y' || ans == 'Y')
{
printf("Registration is successfully completed!. Name on your account is %s",student.name);
fopen("database.txt", "w");
for (i=0; student.name[i] != '\n'; i++)
{
fputc(student.name[i], database);
}
fclose(database);
int i;
int j;
printf("Please enter year\n");
scanf("%d",&(student.year));
fflush(stdin);
printf("Please enter semester\n");
fgets(student.semester,10,stdin);
printf("Please enter units you have taken\n");
scanf("%d",&(student.unit));
printf("Please enter number of subjects you are taking at this moment\n");
scanf("%d",&(student.NumberOfSubject));
fflush(stdin);
for (i=0; i<student.NumberOfSubject; i++)
{
printf("Please enter the name of subjects you are taking\n");
fgets(student.NameOfSubject[i],50,stdin);
printf("Your %dth Subject is %s\n",i+1,student.NameOfSubject[i]);
}
printf("Registration is completed! Here is your information\n\n");
printf("Your year:%d\n",student.year);
printf("Semester:%s\n",student.semester);
printf("Units:%d\n",student.unit);
for (j=0; j<student.NumberOfSubject; j++)
{
printf("Name of subjects:%s",student.NameOfSubject[j]);
}
printf("Number of subjects:%d\n",student.NumberOfSubject);
}
我只是找不到這個問題的原因。 當輸出被打印出來時,有些東西不應該在那里。 我想找出這件事的原因。 謝謝返回例如,如果我輸入以下輸入
2022年
秋季學期
12個單元
3-科目數
數學、英語、物理
輸出將是這個,
您的年份:2022
學期:秋季
英語 // 不應該被打印
//shouldn't be next-line space here
單位:12
科目名稱:英文
科目名稱:數學
學科名稱:物理
科目數:3
fflush(stdin);
是未定義的行為。
在fgets()
中,換行符出現在length - 1
位置。
此外,永遠不要將scanf()
與fgets()
混合使用。
size_t len = strlen(str); // str is your string
if(str[len - 1] == '\n')
str[len - 1] = 0;
編輯:更好的一個
str[strcspn(str, "\n")] = '\0'
您的代碼中有幾個問題:
scanf
與fget
混合。 它們的工作方式不同,所以你不應該完全使用它們(除非你非常小心)。fflush(stdin)
是未定義的行為。 刷新stdin
的唯一方法是閱讀其中的內容。scanf
功能(顯然您是),它可能是一個危險的功能。 您需要檢查它的返回值,並在它失敗時執行一些操作。fgets
還讀取換行符(按Enter時的字符)。 您必須用空終止符替換它。我只是找不到這個問題的原因。
上面的最后一點解釋了原因。
scanf
不讀取用戶輸入。 它解析它。 這就是它的名字所說的:掃描格式。 並且掃描可能會失敗,當它失敗時,它會保持輸入緩沖區完好無損。 因此,您可以“刷新”(即清除或讀取並丟棄其中的所有內容)。
void fflush_stdin(void)
{
scanf("%*[^\n]");
}
通過檢查其返回值來解決scanf
故障。 從cppreference :
返回值
成功分配的接收參數的數量(如果在分配第一個接收參數之前發生匹配失敗,則可能為零),如果在分配第一個接收參數之前發生輸入失敗,則為 EOF。
bool read_char(const char *msg, char *c)
{
do {
printf("%s", msg);
int ret = scanf(" %c", c); // Note the extra space before %c. It means match all space characters before a given character is met.
switch (ret) {
case EOF: return false;
case 1: return true;
default:
flush_stdin();
printf("Input error.\n");
break;
}
} while (true);
return true; // This won't run.
}
對於int
s、 float
s 等也是如此。對於掃描字符串,您必須提供char
緩沖區的長度:
char buffer[100];
scanf("%99[^\n]", buffer);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.