English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

How do i modify using gets?

PROGRAM DESCRIPTION:

Write a program that will find the lowest, highest, and average score for a set of test scores. The test scores may include fractional values (i.e. 99.5 is possible).

The program then asks the user for a test score, and continues to do so until he/she types only in response to the prompt. At this point, the program prints the information listed below and quits
This is the part where i get confused....so I had to use scanf...Please help to change to gets...
This is what I got so far...

#include
#include
#include
#include
main()
{
float score;
float average;
float x;
float max = 0;
float min = 1000; /*just in case there is extra credit*/
float total = 0;
float sum;
int i = 0;


{

printf ("***********************************\n");
printf ("*****Test Score Statisics *****\n");
printf ("***********************************\n");

printf("How many test scores? "); /*I couldn't stop the loop with gets. this is why I am asking.*/
scanf ("%f", &total); /* I don't know how to use gets*/

for( i = 0; i < total; i = i + 1 )
{
printf("Please Enter test Score %d: ", i+1);
scanf ("%f", &score); /*strlen and gets were confusing to me*/
/*It was hard for me to find references for this*/


if( score > max ) { /* Find Max Score*/
max = score;
}else{
if (score < min) { /* Find Min Score*/
min = score;
}
}
sum = score + sum; /* Find Sum Score*/
}

average = sum / i; /* Find Average Score*/

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);

}
system("PAUSE");
return;
}

2007-02-26 09:17:51 · 2 answers · asked by importchef 2 in Computers & Internet Programming & Design

2 answers

The difference is that gets (get-string) -ONLY- reads strings, whereas you can tell scanf to read different data types.

For example, in your code you do:
scanf(%f", &total);

Whereas you would need to do:
char TempBuffer[1024];
gets(TempBuffer);
total = atof(TempBuffer); //atof = Ascii-To-Float

gets(Variable) is equivalent to doing: scanf("%s", Variable).

2007-02-26 09:30:37 · answer #1 · answered by Anonymous · 0 0

scanf: retrieves data from the stream (stdin) according to the format first specified...
The return is the number of fields converted...

gets: returns a string pointer to the line retrieved. It returns a NULL pointer when end-of-file (EOF) is reached...

Your programming mistake is in not setting a return value in the function call... You should always set a variable = function when it is not a void function for status testing... That is why you were having difficulty getting the end of your input!

Example:
. . . .
char *inp1;
if ( (inp1=gets(inp1)) == NULL) break;
. . .

This will break out of the loop when the last input is received!

You can then use sscanf() to parse the string retrieved!

2007-02-26 09:23:43 · answer #2 · answered by N2FC 6 · 0 0

fedest.com, questions and answers