我对编程非常陌生,所以请原谅我的无知。 我已经尝试查找如何使用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;
}
答案 0 :(得分:0)
根据我对您的问题的理解,您希望用户输入字母等级 - A,B,C,D,F。
现在这段代码没有意义,因为scanf接受输入%i - 整数基数10(更多infor:http://alvinalexander.com/programming/printf-format-cheat-sheet)并在comman之后分配它。
scanf ("%i", A || B || C|| D || F);
A || B等......这是一个条件,这就是为什么将用户的整数输入分配给条件...没有任何意义...
答案 1 :(得分:0)
要使用scanf,您需要传递两个参数,即扫描内容的格式以及要分配的变量的地址。 &amp; operator是如何获取变量的地址的。它看起来像这样:
scanf("%i", &someVariable);
格式为%i或%d表示整数,%f表示浮点数,%c表示字符。
也是|| operator是用于比较的逻辑OR运算符,不应该像这样使用。
答案 2 :(得分:0)
这里有一堆错误:
首先,无论您在何处使用scanf
,例如:
scanf ("%i", TotalStudents);
将其更改为scanf(“%i”,&amp; 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] = '\0'; // 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 condition - 如果字符串的最后一个字符是\n
,那么......
line 5
:将最后一个字符转换为\0
,因为这就是字符串的终止方式。
line 6
:if condition - 如果字符串长度为0
,即用户只需按Enter键,打印消息并从循环中断。
答案 3 :(得分: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);