Hi All,
I'm having trouble with my program I wrote for school. Our assignment was to build a program that takes inputted scores until a negative number is entered and then returns a report including the average values, minimum score, maximum score, number of assignments, etc. After working several bugs out of my program I have one remaining that I don't understand. When I enter assignment values in the console and enter a negative number to stop data collection, I have to enter one more integer and press enter before my report will print. It most be a simple problem, but I don't understand it. We have not covered the basis of using if..else or do..while in class yet, so I might be misunderstanding their use. Please give advice, this is a graded assignment, but I really need some explanation on what is wrong in my approach. Thanks.
This is an example of the console:
Enter assigment scores relative to a one hundred point system.
Only use integer values (e.g. Do NOT enter 95.5) <0-100>.
Enter a negative number to end input.
34
65
87
98
67
89
45
-99
1
The summary of the assignments is as follows:
Total number of points earned: 485
Total number of assignments graded: 7
Minimum score from the assignments: 34
Maximum score from the assignments: 98
Mean score on the assignments: 69.29
This Report is completed.
What I am trying to eliminate is the need to enter "1" after "-99" to get the program to print the report.
Any comments or suggestions would be great. Thanks Again.
John
I'm having trouble with my program I wrote for school. Our assignment was to build a program that takes inputted scores until a negative number is entered and then returns a report including the average values, minimum score, maximum score, number of assignments, etc. After working several bugs out of my program I have one remaining that I don't understand. When I enter assignment values in the console and enter a negative number to stop data collection, I have to enter one more integer and press enter before my report will print. It most be a simple problem, but I don't understand it. We have not covered the basis of using if..else or do..while in class yet, so I might be misunderstanding their use. Please give advice, this is a graded assignment, but I really need some explanation on what is wrong in my approach. Thanks.
Code:
#include <stdio.h>
int main (int argc, const char * argv[]) {
// ints and float used are assigned below
int lastScore ;
int minScore = 100;
int maxScore = 0 ;
int numberAssign = 0;
int pointTotal = 0;
float assignAverage = 0.00;
printf("Enter assigment scores relative to a one hundred point system.\nOnly use integer values (e.g. Do NOT enter 95.5) <0-100>.\nEnter a negative number to end input.\n");
//Last line prints program use instrutions
do {
scanf("%d\n", &lastScore); //scans for input scores
if ((lastScore >= 0) && (lastScore <= 100)) { //checks entered score to confirm it is between 0 and 100 then stores the data
pointTotal = pointTotal + lastScore; //keeps a running total of all scores entered
numberAssign++; //keeps a running total of the number of assignments
}
if ((lastScore <= minScore) && (lastScore >= 0)) { //should store the lastScore just entered in minScore if it is the smallest value entered so far
minScore = lastScore;
}
if ((lastScore >= maxScore) && (lastScore <= 100)) { //should store the lastScore just entered in maxScore if it is the largest value entered so far
maxScore = lastScore;
}
} while (lastScore >= 0);
assignAverage = (float)pointTotal/numberAssign; //calculates the average score on the assignments entered
/*PRINTS THE REPORT AND ENDS THE PROGRAM*/
printf("The summary of the assignments is as follows:\n");
printf(" Total number of points earned: %d\n", pointTotal);
printf(" Total number of assignments graded: %d\n", numberAssign);
printf(" Minimum score from the assignments: %d\n", minScore);
printf(" Maximum score from the assignments: %d\n", maxScore);
printf(" Mean score on the assignments: %5.2f\n\n", assignAverage);
printf("This Report is completed.");
return 0;
}
This is an example of the console:
Enter assigment scores relative to a one hundred point system.
Only use integer values (e.g. Do NOT enter 95.5) <0-100>.
Enter a negative number to end input.
34
65
87
98
67
89
45
-99
1
The summary of the assignments is as follows:
Total number of points earned: 485
Total number of assignments graded: 7
Minimum score from the assignments: 34
Maximum score from the assignments: 98
Mean score on the assignments: 69.29
This Report is completed.
What I am trying to eliminate is the need to enter "1" after "-99" to get the program to print the report.
Any comments or suggestions would be great. Thanks Again.
John