-3

I have started a C programming class just a few weeks ago and I am having a hard time learning the how to make loops with unlimited inputs. The code is originally setup to find the average grade for 5 students with three test scores each.

The code I am working with is:

#include <stdio.h>
int main ()
{
  /* variable definition: */
  char StudentName[100];
  float ExamValue, Sum, Avg;
  int students,exams;
   // Loop through 5 Students
  for (students=0; students <5 ; students++) 
  {
     // 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);
  }
  return 0;
}

I need to allow unlimited input, I have tried changing "students <5" to both "student =!" and "student

Also can anyone help me understand this line in the code?

char StudentName[100];

My course doesn't even mention it, and I can't find it online. If anyone can point me to a great source for learning C please let me know.

Thanks in advance for your help.

Peter VARGA
  • 4,780
  • 3
  • 39
  • 75
KSchultz
  • 11
  • 1
  • 2
    C or C++, pick a language. Answers may be very different. `char StudentName[100];` preserves a char array of maximum 100 `char`'s on the stack. – πάντα ῥεῖ Oct 22 '16 at 03:22
  • `StudentName` is a buffer used to store student's names. It has a size of 100 characters, so your student's name shouldn't exceed this. Declaring things with square brackets is called an array. Now you can look it up! – Alex Oct 22 '16 at 03:49
  • [For C](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and [for C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – user4581301 Oct 22 '16 at 03:58
  • Purchase "The C Programming Language" by Brian W. Kernighan, Dennis M. Ritchie – atomSmasher Oct 22 '16 at 04:25

1 Answers1

0

If you really want unliminited input change the outer for loop to for(;;) for example:

#include <stdio.h>
int main ()
{
  /* variable definition: */
  char StudentName[100];
  float ExamValue, Sum, Avg;
  int students,exams;
   // Loop until ctrl-c or program terminates. 
  for (;;) 
  {
     // 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);
  }
  return 0;
}

Your next SO question should most likely be how do I clear an array or how do I handle a signal?

atomSmasher
  • 1,465
  • 2
  • 15
  • 37