Thread: Need Help Allowing x Number of Inputs in a Loop

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    1

    Need Help Allowing x Number of Inputs in a Loop

    Hello everyone, I have only been coding for only a few weeks and I am still trying to learn all the basics. I have been tasked with a code that is currently set to average 3 tests for 5 students. I need to allow the code to accept x number of students. Is there away I can modify the "For" loop to complete this or do I need to try a different loop? Attached is the entire code and below it are a few things I've tried change.
    ---------------------------------------------------------------



    Code:
    #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;
    }


    -------------------------------------------------------
    So far I have tried changing:
    for (students=0; students <5 ; students++)

    into

    for (students=0; students <=0 ; students++)


    And

    int students,exams, MAX;
    // Loop through all Students
    for (students=0; students <MAX ; students++)

    In both scenarios the compiler will run, but not provide averages for any imputed students or test scores.

    Any help would be greatly appreciated, Thank you.


    Sorry if I'm not very clear or using correct terminology.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I need to allow the code to accept x number of students.
    Do you know x before the loop starts?
    In which case, you just read in the value of x, then loop up to x.

    If you don't know, then the obvious loop becomes a while loop, which terminates when the user inputs some 'end of input' condition.
    This could either be by signalling EOF to the program or entering a blank name.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting the number of inputs
    By skrowten_hermit in forum C Programming
    Replies: 5
    Last Post: 09-26-2012, 11:57 AM
  2. Reading an Unknown Number of Inputs
    By Aeias in forum C++ Programming
    Replies: 13
    Last Post: 02-25-2012, 08:27 AM
  3. Loop, Inputs
    By meandmyipod in forum C Programming
    Replies: 3
    Last Post: 09-24-2011, 05:49 AM
  4. converting two keypad inputs into one decimal number
    By volkvanmyn25 in forum C Programming
    Replies: 1
    Last Post: 09-16-2010, 09:09 AM
  5. trouble with creating a loop to read certain number of inputs
    By import tuner650 in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2008, 07:28 PM

Tags for this Thread