循环基于用户输入 [英] Loop based on user input

查看:130
本文介绍了循环基于用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个输出学生成绩平均值的代码。用户输入一个名称,然后输入他们的三个考试分数,然后输出平均值。我似乎无法弄清楚如何让用户输入任意数量的学生姓名。我希望程序在用户没有输入其他名称时终止,但我似乎无法弄明白。我在没有程序导致无限循环的情况下获得的最大进步是使用if语句,但if语句在一个条目后终止。

I'm trying to create a code that outputs the average of a student's grade. The user inputs a name then three of their exam scores and then the average is outputted. I can't seem to figure out how to let the user input any number of student names. I want the program to terminate when the user does not put in another name but I can't seem to figure it out. The most progress I've gotten without the program resulting in an infinite loop is with an if statement, but the if statement terminates after one entry.

#include <stdio.h>
int main ()
{
  /* variable definition: */
  char StudentName[100];
  float ExamValue, Sum, Avg;
  int exams;
if (StudentName!=0) {
     // reset Sum to 0
     Sum =0.0;
     printf("Enter Student Name \n");
     scanf("%s", StudentName);
     // Nested Loop for Exams
    for (exams=0; exams < 3; exams++)
    {
        printf ("Enter exam grade: \n");
        scanf("%f", &ExamValue);
        Sum += ExamValue;
}
Avg = Sum/3.0;
printf( "Average for %s is %f\n",StudentName,Avg);
} else {
return 0; }
} 





我尝试过:



我试过使用while循环,这样当用户输入是新行时它会终止但是不起作用。我尝试过以学生= 0开头的for循环;学生<学生姓名;学生++;但那没用。我得到的最多就是这个if循环,但我不明白它为什么不起作用。我曾尝试使用while(StudentName> 0),但这导致无限循环。我只是希望程序在用户停止输入名称时停止。



What I have tried:

I've tried to use a while loop so that when the user input is new line it would terminate but that didn't work. I've tried a for loop that started with students = 0; students < StudentName; students++; but that didn't work. The most I've gotten was this if loop, but I'm not understanding why it isn't working. I've tried to use while(StudentName>0) but that results in an infinite loop. I just want the program to stop when the user stops inputting names.

推荐答案

因为你这样做:

Because you do this:
char StudentName[100];



此条件将始终通过:


This condition will always pass:

if (StudentName!=0) {

因为 StudentName 是一个数组,而在C中,数组的名称是指向第一个元素的指针。指针在有效时始终为非零,因此您的情况不会失败。



目前,您有一个循环 - 从用户获取三个项目平均他们。要为第二个,第三个或第四个学生重复此操作,您需要围绕获取学生姓名并计算平均值代码进行第二轮循环。也许,用适当的while循环替换上面提到的if行并添加你想要另一个学生吗?循环底部的问题和反应会做你想要的 - 但这是你的功课,而不是我的;所以我不会给你任何代码!

Because StudentName is an array, and in C the name of an array is a pointer to the first element. Pointers are always non-zero when they are valid, so your condition cannot fail.

At the moment, you have one loop - to get three items from the user and average them. To repeat this for a second, or third, or fourth student, you need a second loop around the "get a student name and work out the average" code. Probably, replacing the if line I mentioned above with an appropriate while loop and adding a "do you want another student?" question and response at the bottom of the loop would do what you want - but this is your homework, not mine; so I'll give you no code!


这篇关于循环基于用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆