Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#include <stdio.h> 
#include <limits.h> 
int main () 
{ 
  /* variable definition: */ 
  char StudentName[100];
  char end;
  float ExamValue, Sum, Avg; 
  int students,exams; 
   // Loop through Students 
  for (students=1; students <INT_MAX ; 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);
	if (StudentName == 'end')
	break;
}
  return 0; 
}


What I have tried:

I have tried various versions of this and still nothing.
Posted
Updated 28-Sep-18 22:15pm
v2

That will not even compile because the if statement at the end is incorrect.
C++
if (StudentName == 'end')
break;

You cannot compare strings in this way, and you are trying to compare a character array, not a single character. Also the string end must be in double quotes. Replace it with the following:
C++
if (strcmp(StudentName, "end") == 0)
break;
 
Share this answer
 
Start by indenting your code correctly so it doesn't look like it ends with a spurious close curly bracket:
C++
#include <stdio.h> 
#include <limits.h> 
int main () 
{ 
  /* variable definition: */ 
  char StudentName[100];
  char end;
  float ExamValue, Sum, Avg; 
  int students,exams; 
  // Loop through Students 
  for (students=1; students <INT_MAX ; 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);
    if (StudentName == 'end')
    {
      break;
    }
  }
  return 0; 
}
Now what I would look at is moving the "exit test" higher in the function, so that it's the first thing you do:
C++
#include <stdio.h> 
#include <limits.h> 
int main () 
{ 
  /* variable definition: */ 
  char StudentName[100];
  char end;
  float ExamValue, Sum, Avg; 
  int students,exams; 
  // Loop through Students 
  for (students=1; students <INT_MAX ; students++)
  { 
    printf("Enter Student Name \n"); 
    scanf("%s", StudentName);    
    if (strcmp(StudentName, "end") == 0)
    {
      break;
    }
    Sum =0.0;   
    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; 
}
If that doesn't fix your problem, then you need to explain in a lot more detail exactly what it is doing that you didn't expect, or not doing that you did. And what you have done to find out why - including what the debugger showed you while your code was running.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900