A few problems i need help with...
1. I have to have the user clear scores before entering the first set of test scores because the test score number starts of in the millions.
2. I can't get the program to enter a second set of numbers after the first set has been cleared.
3. How to get the program to count the sets as i enter them in. ( I will be saving them in a file)
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
#include %26lt;math.h%26gt;
int main()
{
int eLoop=0,bsinp;
char menuResponse;
float score;
float average;
float x;
float max = 0;
float min = 1000;
float total = 0;
float sum;
int i = 0;
char buffer[BUFSIZ+1];
int scoresEntered;
int setsEntered;
do{
/*Print the Menu*/
printf ( "\n\n\**********************************...
printf ("*****Test Score Statisics v2.0*****\n");
printf ("***********************************\n"...
printf ("[C] Clear Stats\n");
printf ("[E] Enter Test Scores\n");
printf ("[D] Display Test Score Stats\n");
printf ("[Q] Quit\n\n");
printf ("Type the letter of your selection and press %26lt;ENTER%26gt;\n");
gets(buffer);
menuResponse = buffer[0];
switch (menuResponse)
{
/*Clear all scores and tally the total sets*/
case 'C':
case 'c':
printf("All Scores Cleared.\n");
setsEntered++;
max = 0;
min = 100;
int i = 0;
float total = 0;
break;
case 'E':
case 'e':
while ( score ) {
printf("Please Enter test Score %d: ", i+1);
score = atoi(gets(buffer));
i = i + 1;
if (buffer[0] == 0) break;
if( score %26gt; max ) { /* Find Max Score*/
max = score;
}else{
if (score %26lt; min) { /* Find Min Score*/
min = score;
}
}
sum = score + sum; /* Find Sum Score*/
}
average = sum / (i-1) ; /* Find Average Score*/
break;
case 'D':
case 'd':
printf( "\n\nThe average score is %.1f\n", average);
printf( "%d Test Scores\n", i-1 );
printf( "Maximun Test Score = %.1f\n", max);
printf( "Minimum Test Score = %.1f\n", min);
printf( "\n\nPlease Clear Scores before preceding.");
break;
case 'Q':
case 'q':
eLoop=1;
break;
default:
printf("Please select a letter.\n");
break;
}
}while (eLoop!=1);
printf("Press Any Key.\n");
gets(bsinp);
return 0;
}
Need help with c- programming?
sorry I'll just have to paste the full code here (commented where necessary). there were a few other flaws as well, also added support for writing sets to file. enjoy.
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
#include %26lt;math.h%26gt;
int main()
{
int eLoop=0;
char menuResponse;
float score;
float average=0;
float max = 0;
float min = 0;
float sum=0;
int i = 0;
char buffer[BUFSIZ+1];
int setsEntered;
FILE *fd;
// open file where you put sets
if (NULL == (fd = fopen("mysets.txt", "w")))
{
printf ("\n Sorry couldn't open mysets.txt, change path or give permissions.\n");
exit(1);
}
do
{
/*Print the Menu*/
printf ( "\n\n***********************************...
printf ( "*****Test Score Statisics v2.0*****\n");
printf ( "***********************************\n")...
printf ("[C] Clear Stats\n");
printf ("[E] Enter Test Scores\n");
printf ("[D] Display Test Score Stats\n");
printf ("[Q] Quit\n\n");
printf ("Type the letter of your selection and press %26lt;ENTER%26gt;\n");
gets(buffer);
menuResponse = buffer[0];
switch (menuResponse)
{
/*Clear all scores and tally the total sets*/
case 'C':
case 'c':
printf("All Scores Cleared.\n");
max = 0;
min = 0;
i = 0;
sum = 0;
average = 0;
break;
case 'E':
case 'e':
if (0 != i)
{
printf (" %26lt;%26lt;%26lt; YOU MUST CLEAR PREVIOUS SCORES FIRST %26gt;%26gt;%26gt;\n\n");
break;
}
score = 1; // needed for "while (score)" to start off properly
setsEntered++;
fprintf (fd, "Values for scores Set # %d\n", setsEntered);
while ( score )
{
printf("Please Enter test Score %d: ", i+1);
score = atof(gets(buffer)); // atof instead of atoi - you really need float values not integers
if (buffer[0] == 0) break;
// initialize min score here (= first score)
if (0 == i) min = score;
if( score %26gt; max )
{ /* Find Max Score*/
max = score;
}
// "else" should not be here
if (score %26lt; min)
{ /* Find Min Score*/
min = score;
}
sum = score + sum; /* Find Sum Score*/
i++; // increment counter here
// write entry to file
fprintf (fd, " Score %d: %.1f\n", i, score);
}
average = sum/i; /* Find Average Score*/
// write statistics to file
fprintf(fd, "The average score is %.1f\n", average);
fprintf(fd, "Maximun Test Score = %.1f\n", max);
fprintf(fd, "Minimum Test Score = %.1f\n", min);
fprintf(fd, "---------------------------------------...
break;
case 'D':
case 'd':
printf( "\n\nThe average score is %.1f\n", average);
printf( "%d Test Scores\n", i );
printf( "Maximun Test Score = %.1f\n", max);
printf( "Minimum Test Score = %.1f\n", min);
printf( "\n\nPlease Clear Scores before preceding.");
break;
case 'Q':
case 'q':
eLoop=1;
break;
default:
printf("Please select a letter.\n");
break;
}
} while (eLoop!=1);
// close file
fclose (fd);
printf("Press Any Key.\n");
gets(buffer);
return 0;
}
Reply:You need to initialise
int setsEntered=0
float average = 0; (This is incase you do 'D' before entering any scores!
float min = 0; and add a check in the while loop
if ((min == 0) || (score %26lt; min)) { /* Find Min Score*/
min = score;
}
scoresEntered is not used
You are defining total within the case block but it is NOT used.
Initialise the values before the while loop after the case 'e' so you don't have to manually clear them each time before entering a set of scores.
sum = 0;
min = 0;
max = 0;
i = 0;
Increment the setsEntered when you actually enter the sets
case 'e':
setsEntered++;
while (score) {
You need to change the while (score) to a
do {
}while(score);
As at the end for the first set score is set to 0 so it never enters the while loop again.
I think that is it.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment