Advertisement

Looping problem with scanf - Help!!!

Started by April 07, 2002 08:10 AM
6 comments, last by rhino2876 22 years, 11 months ago
With the following code (it prints the max and min from 10 inputed numbers) , I get the output with my input of course: My output and my question to you: Why does it take two values for the first scanf? " Enter a number: 4 3 Enter a number: 1 Enter a number: 2 Enter a number: 4 Enter a number: 5 Enter a number: 6 Enter a number: 23 Enter a number: 2 Enter a number: 1 Enter a number: 2 The maximum is: 23.000000 The minimum is: 1.000000 " My code " #include <stdio.h> int main() { int i = 0; float numbers[10] = {0}, max = 0, min = 0; printf("Enter a number: "); scanf("%f\n", &numbers); max = numbers[0]; min = numbers[0]; for(i = 1; i < 10; i++) { printf("Enter a number: "); scanf("%f\n", &numbers); if (numbers > max) max = numbers; if (numbers < min) min = numbers; } printf("\nThe maximum is: %f", max); printf("\nThe minimum is: %f", min); return 0; } " </i>
Rhino2876
scanf() is an abomination. Do not use it. Unless, that is, you *want* to write programs susceptible to buffer overflow attack.
Advertisement
Please just answer the question. I know thier are better methods for doing this with C++, but I am trying to learn C right now. I actually already know C++, but I am trying to get familiar with C in order to understand C code examples also.
Rhino2876
Remove the "\n" from the scanf format string. That makes it wait for a newline. And do the software industry a favour by never using scanf() in your code.
Well, I figured out how to fix it, but still not sure why it didn''t work before. I took out all the \n''s in the scanf functions. I don''t know why that the \n was only a problem the first time scanf is called and not the others. The interesting thing is that when I took the \n off the 1st call to scanf, then the same problem happened on the 2nd call. See below. But anyways, after I took them all out, everything worked normal, so problem solved. I wish I understood why there was a problem in the first place though.

"
Enter a number: 4
Enter a number: 1
5
Enter a number: 2
Enter a number: 4
Enter a number: 5
Enter a number: 6
Enter a number: 23
Enter a number: 2
Enter a number: 1
Enter a number: 2

The maximum is: 23.000000
The minimum is: 1.000000
"




My code
"
#include <stdio.h>

int main()
{
int i = 0;
float numbers[10] = {0}, max = 0, min = 0;

printf("Enter a number: ");
scanf("%f", &numbers);

max = numbers[0];
min = numbers[0];

for(i = 1; i < 10; i++)
{
printf("Enter a number: ");
scanf("%f\n", &numbers);
if (numbers > max) max = numbers;<br>if (numbers < min) min = numbers;<br>}<br><br>printf("\nThe maximum is: %f", max);<br>printf("\nThe minimum is: %f", min);<br><br><br>return 0;<br>}<br>" </i>
Rhino2876
Good call SabreMan on taking out the \n''s. You posted it before I finished posting my post that I figured it out.
Rhino2876
Advertisement
quote:
Original post by SabreMan
scanf() is an abomination. Do not use it. Unless, that is, you *want* to write programs susceptible to buffer overflow attack.

It''s quite possible to use c i/o functions without causing buffer overflows. You know what circumstances cause buffer overflows? Then avoid them.

I agree that the c++ way is easier; I just believe that the biggest causes of buffer overflows are incompetence and lack of sleep.
Agreed. If you know how to use C, you shouldn''t have any problems with C. C++''s cout << is not any "better" than C''s printf(). In some cases, printf() is better because it''s more technical. Most, times, if you''re good at C, it''s usually some problem with your mind, whether it''s lack of sleep, dumbness, etc.

--------------------------
->Take it to the Xtreme!<-
--------------------------
--------------------------->Take it to the Xtreme!<---------------------------

This topic is closed to new replies.

Advertisement