scanf 和多个输入的错误

Error for scanf and multiple inputs

我是编程新手,请原谅我的无知。 我已经尝试查找如何使用 scanf 并尝试了各种不同的使用方法,但在尝试输入第一个输入后我仍然收到错误消息。当我尝试 运行 具有设定编号的程序时,它可以工作(直到我将要解决的第二个问题),所以我知道它是第一个 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;
}

根据我对您问题的理解,您希望用户输入字母等级 - 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。

还有||运算符是用于比较的逻辑或运算符,不应这样使用。

这里有一堆错误:

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

 scanf ("%i", TotalStudents);

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

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

scanf ("%f", TotalClasses);

这里,TotalClasses 是一个 int。因此,要么将其更改为浮点数,要么将格式说明符更改为 %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 link 完整代码: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] = '[=14=]';   // 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:if条件——如果字符串的最后一个字符是\n,那么...

line 5:将最后一个字符转换为 [=35=],因为字符串应该以这种方式终止。

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

您需要了解 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);