By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,557 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,557 IT Pros & Developers. It's quick & easy.

no output in compiler.....why?

P: 12
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?

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int main ()
  3. {
  4. /* variable definition: */
  5. char StudentName[100];
  6. float ExamValue, Sum, Avg, value;
  7. int students,exams,studentname;
  8.  
  9. // reset Sum to 0
  10. Sum =0.0;
  11. // Loop through Students
  12. while (students > 0)
  13. {
  14. printf("Enter Student Name \n");
  15. scanf("%s", StudentName);
  16.     scanf("%f", &value);
  17.     {studentname = studentname + 1; }
  18.     if (studentname < 0){
  19.     break;
  20.         printf("End of Student list");
  21.     }        
  22. // Nested Loop for Exams
  23. for (exams=0; exams < 3; exams++)
  24. {
  25. printf ("Enter exam grade: \n");
  26. scanf("%f", &ExamValue);
  27. Sum += ExamValue;
  28. }
  29. Avg = Sum/3.0;
  30. printf( "Average for %s is %f\n",StudentName,Avg);
  31. }
  32. return 0;
  33. }
Mar 31 '16 #1
Share this Question
Share on Google+
5 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Mar 31 '16 #2

P: 12
Do you have any examples? of what that would look like?
Apr 1 '16 #3

P: 12
the Array of strings that is..
Apr 1 '16 #4

P: 12
my code.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int main ()
  3. {
  4. /* variable definition: */
  5. char StudentName[100];
  6. float ExamValue, Sum, Avg, value;
  7. int students, exams, studentname;
  8.  
  9. // Loop through Students
  10. while (students > 0)
  11. {
  12. // Initialize
  13. Sum = 0.0;
  14. students = 0;
  15. studentname =0;
  16. printf("Enter Student Name \n");
  17. scanf("%s", StudentName);
  18.         studentname++; 
  19.     if (studentname < 0){
  20.     break;
  21.         printf("End of Student list \n");
  22.     }        
  23. // Nested Loop for Exams
  24. for (exams=0; exams < 3; exams++)
  25. {
  26. printf ("Enter exam grade: \n");
  27. scanf("%f", &ExamValue);
  28. Sum += ExamValue;
  29. }
  30. Avg = Sum/3.0;
  31. printf( "Average for %s is %f\n",StudentName,Avg);
  32. }
  33. return 0;
  34. }
Apr 1 '16 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Apr 2 '16 #6

Post your reply

Sign in to post your reply or Sign up for a free account.