scanf和多个输入错误
Error for scanf and multiple inputs
我对编程很陌生,所以请原谅我的无知。我已经试着查找如何使用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
:将最后一个字符转换为