scanf和多个输入错误

Error for scanf and multiple inputs

本文关键字:输入 错误 scanf      更新时间:2023-10-16

我对编程很陌生,所以请原谅我的无知。我已经试着查找如何使用scanf,并尝试了各种不同的方法来使用它,但在尝试输入第一个输入后,我仍然收到一个错误。当我试图用一个设定的数字运行程序时,它是有效的(直到我的第二个问题,我将要解决),所以我知道这是第一次扫描。我感谢你提供的任何帮助。以下是我正在努力的内容:

//C code
#include <stdio.h>
using namespace std;
int main() {
//Declare variables
char StudentName[100];
float Avg;
int Sum, Students, TotalStudents, TotalClasses, Classes, A, B, C, D, F;
A=4;
B=3;
C=2;
D=1;
F=0;
//This is where the problem begins.
//I want to allow the user to input the number of students
//being graded. "Enter the number of students being graded" 
//comes up fine.
printf ("Enter the number of students being graded.");
scanf ("%i", TotalStudents);
//First loop 
for (Students = 0; Students<1; Students++){
Avg =0.0;
printf ("Enter Student Name n");
scanf ("%s", StudentName);
printf ("Enter Number of Classes n");
scanf ("%f", TotalClasses);
for (Classes = 0; Classes < TotalClasses; Classes++){
printf ("Enter Letter Grade for Class n");
//The second problem starts here. I am trying to find a way to
//allow the user to input the letter grade and add all the grades 
//together. After that I want it to find the average for that  
//student before moving on to the next student.
//I know my code is completely wrong but I don't know how to correct
//it based off of the examples I have seen 
scanf ("%i", A || B || C|| D || F);
Sum = Sum +  A || B || C|| D || F;
}
Avg = Sum/TotalClasses;
printf ("%s's average is %f n", StudentName, Avg); 
}
return 0;
}

这里有很多错误:

首先,无论您在哪里使用scanf,例如:

scanf ("%i", TotalStudents);

将其更改为scanf("%i",&TotalStudents);

其次,scanf的第一部分是格式说明符,所以如果有%f,那么参数应该是浮动类型的。

scanf ("%f", TotalClasses);

这里,TotalClasses是一个int。因此,要么将其更改为float,要么将格式说明符更改为%i

字母等级:假设输入是一个整数,并且您希望使用它们的总和。

scanf ("%i %i %i %i %i", &A,&B,&C,&D,&F);
Sum = Sum +  A + B + C + D + F;

此外,还有一件事,您可能希望将一些变量(如Sum)视为float,因为语句Avg = Sum/TotalClasses;是整数除法,因此一些值(小数点后,如整数除法中的12/5=2.00)可能会丢失。

完整代码的Ideone链接:http://ideone.com/ur3M2v

完整代码:

#include <stdio.h>
using namespace std;
int main() {
//Declare variables
char StudentName[100];
float Avg,TotalClasses;
int Students, TotalStudents, Sum, Classes, A, B, C, D, F;
char c;
A=4;
B=3;
C=2;
D=1;
F=0;
//This is where the problem begins.
//I want to allow the user to input the number of students
//being graded. "Enter the number of students being graded"
//comes up fine.
printf ("Enter the number of students being graded.");
scanf ("%i", &TotalStudents);
//First loop
for (Students = 0; Students<TotalStudents; Students++){
Avg =0.0;
printf ("Enter Student Name n");
scanf ("%s", StudentName);
printf ("Enter Number of Classes n");
scanf ("%f", &TotalClasses);
for (Classes = 0; Classes < TotalClasses; Classes++){
printf ("Enter Letter Grade for Class n");
//The second problem starts here. I am trying to find a way to
//allow the user to input the letter grade and add all the grades
//together. After that I want it to find the average for that
//student before moving on to the next student.
//I know my code is completely wrong but I don't know how to correct
//it based off of the examples I have seen
scanf (" %c", &c);
switch(c)
{
case 'A': Sum+=A;
break;
case 'B': Sum+=B;
break;
case 'C': Sum+=C;
break;
case 'D': Sum+=D;
break;
case 'F': Sum+=F;
break;
}
}
Avg = Sum/TotalClasses;
printf ("%s's average is %f n", StudentName, Avg);
}
return 0;
}

为了检测StudentName的空输入,用替换scanf("%s,StudentName);

char c= getchar();        // line 1
fgets(StudentName,100,stdin);   // line 2
int i = strlen(StudentName)-1;   // line 3
if( StudentName[ i ] == 'n')   // line 4
StudentName[i] = '';   // line 5
if(i==0) {printf("exiting"); break;}  //  line 6

此代码的解释:

line 1:这个c只是用来存储缓冲区。在它的前一行中,我们有一个n,所以当我们使用fgets进行输入时,它存储这个n,并且不接受进一步的输入。

line 2:这里我们使用fgets来获取字符串的实际输入。为什么不scanf?因为它在遇到空白后停止接受输入,并且它的分隔符是换行符,即它一直在等待换行符完成扫描。

line 3:我们将字符串的长度存储在i中。

line 4:如果条件-如果字符串的最后一个字符是n,那么。。。

line 5:将最后一个字符转换为,因为字符串应该这样终止。

line 6:if condition-如果字符串长度为0,即用户只需按下回车键,打印消息并脱离循环。

根据我对您问题的理解,您希望用户输入字母等级-A、B、C、D、F.

现在,这里的代码没有意义,因为scanf接受输入%i-一个以10为基数的整数(更多信息:http://alvinalexander.com/programming/printf-format-cheat-sheet)并在命令之后分配。

scanf ("%i", A || B || C|| D || F);

A||B等等是一个条件,这就是为什么将用户的整数输入分配给一个条件。。。

要使用scanf,您需要传递两个参数,即您正在扫描的内容的格式和您正在分配的变量的地址。&运算符是如何获取变量的地址。它看起来是这样的:

scanf("%i", &someVariable);

整数格式为%i或%d,浮点格式为%f,字符格式为%c。

此外,||运算符是用于比较的逻辑OR运算符,不应像这样使用。

您需要了解scanf()是如何工作的。scanf至少需要2个参数。第一个参数将包含需要捕获的变量类型(%d是整数,%s是字符串,%c是字符,等等)。第二个参数包含变量的地址,该地址将包含用户输入的内容。

以下是您需要替换的代码部分:

scanf ("%i", TotalStudents);应为scanf ("%i", &TotalStudents);

for (Students = 0; Students<1; Students++){应为for (Students = 0; Students<TotalStudents; Students++){

scanf ("%i", A || B || C|| D || F);
Sum = Sum +  A || B || C|| D || F;

应该是

scanf ("%i %i %i %i %i", &A,&B,&C,&D,&F);
Sum +=  A + B + C + D + F;

此外。。。

scanf ("%f", TotalClasses);应为scanf ("%i", &TotalClasses);