no output in compiler.....why?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gfanning23
    New Member
    • Mar 2016
    • 12

    no output in compiler.....why?

    I have been tasked with making this code allow the user to enter an unspecified amount of student names and avg here 3 test grades. Once that is done I set it to end the loop when a negative number is entered. (atleast that's the plan) When the code is put into the compiler (ideone.com) it runs successfully but there is not output....why?

    Code:
    #include <stdio.h>
    int main ()
    {
    /* variable definition: */
    char StudentName[100];
    float ExamValue, Sum, Avg, value;
    int students,exams,studentname;
    
    // reset Sum to 0
    Sum =0.0;
    // Loop through Students
    while (students > 0)
    {
    printf("Enter Student Name \n");
    scanf("%s", StudentName);
        scanf("%f", &value);
        {studentname = studentname + 1; }
    	if (studentname < 0){
    	break;
    		printf("End of Student list");
    	}		
    // 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;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The students and studentname variables are not being initialized.

    Your results will be indeterminate.

    Also, the studentname array is being used for all students. You will need an array for each student. That is, an array of strings.

    Comment

    • gfanning23
      New Member
      • Mar 2016
      • 12

      #3
      Do you have any examples? of what that would look like?

      Comment

      • gfanning23
        New Member
        • Mar 2016
        • 12

        #4
        the Array of strings that is..

        Comment

        • gfanning23
          New Member
          • Mar 2016
          • 12

          #5
          my code.

          Code:
          #include <stdio.h>
          int main ()
          {
          /* variable definition: */
          char StudentName[100];
          float ExamValue, Sum, Avg, value;
          int students, exams, studentname;
           
          // Loop through Students
          while (students > 0)
          {
          // Initialize
          Sum = 0.0;
          students = 0;
          studentname =0;
          printf("Enter Student Name \n");
          scanf("%s", StudentName);
                  studentname++; 
          	if (studentname < 0){
          	break;
          		printf("End of Student list \n");
          	}		
          // 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;
          }

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Read this: https://bytes.com/topic/c/insights/7...rrays-revealed

            Post me again after you have read this and I'll provide more info.

            Also scanf stops on the first whitespace character. So if your student name is Tom Cat all you will get is Tom. The Cat will show up on the next scanf wherever it is in the code. You might look up sscanf.

            Comment

            Working...