簡體   English   中英

為什么在我期望的時候我的循環沒有退出?

[英]Why is my loop not exiting when I expect?

我目前正在研究一些實踐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后退出循環的要求。 我正在嘗試逐步進行,似乎我一開始就失敗了...非常感謝您的幫助。

您使用的是單個equals = ,它是賦值運算符,而不是==比較運算符。

你錯了。 請在scanf函數中刪除“ \\ n”:

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

要打印所有成員,必須更改數組的索引:

printf("%d\n",marks[i]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM