generalt Posted May 7, 2009 Share Posted May 7, 2009 (edited) #include <stdio.h> #define TESTS 5 int main(int argc, char *argv[]){ float grades[TESTS], sum; int average, i; for (i = 0; i < TESTS; i++) { float tmp; printf("Test %d Score: ", i+1); grades[i] = scanf("%f", &tmp); } sum = 0; for (i = 0; i < TESTS; i++) sum += grades[i]; average = (int) (sum / (TESTS + 0.5)); printf("Average: %d\n", average); for (i = 0; i < TESTS; i++) printf("Test %d: %f\n", i, grades[i]); return 0; } i is set to 1 no matter what the input. What am I doing wrong? Edited May 7, 2009 by generalt Link to comment Share on other sites More sharing options...
0 generalt Posted May 8, 2009 Author Share Posted May 8, 2009 I have revised it somewhat but now I get a "Bus error," which is essentially described as a memory access error. #include <stdio.h> #define TESTS 5 int main(int argc, char *argv[]){ char *grades[TESTS]; int average, i; float sum = 0; for (i = 0; i < TESTS; i++) { printf("Test %d Score: ", i+1); int tmp = (int) gets(grades[i]); sum += tmp; } average = (int) (sum / (TESTS + 0.5)); printf("Average: %d\n", average); for (i = 0; i < TESTS; i++) printf("Test %d: %s\n", i, grades[i]); return 0; } Link to comment Share on other sites More sharing options...
0 ViZioN Posted May 8, 2009 Share Posted May 8, 2009 I've fixed your program, although I'm not sure why doing what I did fixed the second problem (not quite awake yet) #include <stdio.h> #include <stdlib.h> #define TESTS 5 int main(int argc, char *argv[]){ float grades[TESTS+1], sum; int average, i; for (i = 0; i < TESTS; i++) { float tmp; printf("Test %d Score: ", i+1); scanf("%f", &grades[i]); } sum = 0; for (i = 0; i < TESTS; i++){ sum += grades[i]; } average = (int) (sum / (TESTS + 0.5)); printf("Average: %d\n", average); for (i = 0; i < TESTS; i++) printf("Test %d: %f\n", i, grades[i]); return 0; } You were storing the input wrong with this line: grades[i] = scanf("%f", &tmp); This sets grades to the return value of scanf, and stores the float number in the variable temp, which you never use otherwise. I'm assuming scanf returns 1 on success which explains why your output was all ones. For the second error I made your array bigger by one element than TESTS although I'm not sure why that fixed it. grades[TESTS] --> grades[0]->[4] which from looking at your code is how you have used it. Hope this helps! Link to comment Share on other sites More sharing options...
0 generalt Posted May 8, 2009 Author Share Posted May 8, 2009 Ah, that explains the 1s! It makes sense now that I think about it. And I removed the "+1" to make the array the size of the input, and it seems to work fine (after I revised the computation of average -> ((sum / TESTS) + .5); Thanks for your help! The scanf thing clears up some issues I have with some other programs as well. Link to comment Share on other sites More sharing options...
0 ViZioN Posted May 8, 2009 Share Posted May 8, 2009 You're welcome, glad it has helped. In addition, you might want to use fgets() rather than scanf, as fgets() allows you to specify the number of characters that are read from the stream. scanf() won't care and will just try and store it all in your variable even if there is insufficient room. Helps you to avoid memory corruption :p Link to comment Share on other sites More sharing options...
0 generalt Posted May 8, 2009 Author Share Posted May 8, 2009 So if I use fgets() and specify the number of characters to read in as the length of the string, then will it automatically allocate enough memory to handle the string (no matter how large) and therefore eliminate the chance of a buffer overflow? Link to comment Share on other sites More sharing options...
0 ViZioN Posted May 8, 2009 Share Posted May 8, 2009 No it won't allocate memory at all. It will only ensure that only the number of characters which can be stored, are read from the stream and are stored in a variable you've already declared. So in your case when you define your array as grades[TESTS] you would then do something along the lines of fgets(grades, TESTS, stdin). This ensures you wont overflow. If more data is input than can be stored, the rest will still be stored in the stream, waiting to be read in so you will probably want to do a fflush(stdin) to clear the input stream, before you read in another value. More information on fgets() here Link to comment Share on other sites More sharing options...
Question
generalt
#include <stdio.h> #define TESTS 5 int main(int argc, char *argv[]){ float grades[TESTS], sum; int average, i; for (i = 0; i < TESTS; i++) { float tmp; printf("Test %d Score: ", i+1); grades[i] = scanf("%f", &tmp); } sum = 0; for (i = 0; i < TESTS; i++) sum += grades[i]; average = (int) (sum / (TESTS + 0.5)); printf("Average: %d\n", average); for (i = 0; i < TESTS; i++) printf("Test %d: %f\n", i, grades[i]); return 0; }i is set to 1 no matter what the input. What am I doing wrong?
Edited by generaltLink to comment
Share on other sites
6 answers to this question
Recommended Posts