为什么我的循环没有在我预期的时候退出?

时间:2015-11-19 23:27:39

标签: c arrays

我目前正在处理一些C编程问题,而且我遇到了一个我不太了解如何完成的问题。它如下:

Write a C program that will prompt the user to enter data about student
marks. After all the marks are entered, your program must print a short 
report showing the minimum and maximum marks (along with the ID of the students 
receiving those marks) and the average mark. The data will include student 
numbers (integers) and marks (floating point). Here's an example of what an 
interaction with your program might look like. The program's output is in 
boldface and the user's input is not.

--------------------------

student number (0 to stop): 1234
mark: 72.5
student number (0 to stop): 2345
mark: 63.47
student number (0 to stop): 67298764
mark: 86
student number (0 to stop): 0

Lowest mark: 63.47 (student 2345)
Highest mark: 86 (student 67298764)
Average mark: 73.99

这是我到目前为止所提出的:

#include<stdio.h>
#include <stdlib.h>
int main()
{
int marks[5];
int i;

for(i=0;i<5;i++)
{
    printf("student number (0 to stop): ");
    scanf("%d\n", marks + i);
    if (marks[i] == 0) {
        break;
    }



}

printf("\nEntered values:\n");
for(i=0;i<5;i++)
{
    printf("%d\n",*marks);
}

return 0;

} 我只是试图让程序在用户输入无法正常工作的0后遵循退出循环的要求。我试图一步一步走,似乎我在第一次失败了......任何帮助都非常感谢,谢谢。

2 个答案:

答案 0 :(得分:2)

您正在使用单个等于=,这是一个赋值运算符,而不是==比较运算符。

答案 1 :(得分:1)

你错了。请删除scanf函数中的“\ n”:

scanf("%d", marks + i);

要打印所有成员,您必须更改数组的索引:

printf("%d\n",marks[i]);
相关问题