I'm trying to read a bunch of ints in one line until EOF, but the program doesnt end, it scans again.
why?Code:printf("input: "); while (scanf("%d", &temp)!= EOF) { printf("Values: %d", temp); }
I'm trying to read a bunch of ints in one line until EOF, but the program doesnt end, it scans again.
why?Code:printf("input: "); while (scanf("%d", &temp)!= EOF) { printf("Values: %d", temp); }
There is nothing like EOF char. There is no any special character to represent end of file. It is used to indicate a end-of-file in a file and at the end of file. Use fgetc to red char. Perhaps you can break the look as soon as the user enter a return key. Like
ssharishCode:int c; while( ( c = fgetc( stdin ) ) != '\n' ) { ... }
Life is like riding a bicycle. To keep your balance you must keep moving - Einstein
Well, that works if I was reading 1 char at a time.
I'm trying to read in a bunch of ints,
Example input:
1 4 9 52 63 29
And what I'm trying to get is
Largest value: 63
I have everything else I believe and it's just this input problem I am having.
How are you terminating your input, and on what operating system are you running?
for now, I'm terminating it by pressing ctrl+c. I'm using linux via putty
#include <stdio.h>
int static large;
int static count;
int largest(int *check)
{
large = check[0];
int i = 0;
for (i=0;i<count;i++)
{
if (large < check[i])
{
large = check[i];
}
}
return large;
}
int main()
{
int list[20];
int temp;
int ans;
printf("input: ");
while ((scanf("%d", &temp))!= '\n')
{
list[count] = temp;
count++;
}
ans = largest(list);
printf("Largest value: %d\n", ans);
return 0;
}
This is what I currently have. My biggest problem is the starting point.
Ctrl-C will stop your program cold, unless the point is to trap signals. Otherwise you can read in a string, and then read out of the string using strtol (look that up in your manual, it is probably exactly what you want).
Well your best bet would be to use read the input as a string convert that to int or long or whatever it is. Or your other options are just use for loop along the size of your array (20). Which could be saving as you don’t have boundary check with your current code. Which is very important to check you don’t cross your array boundary.
ssharish
Life is like riding a bicycle. To keep your balance you must keep moving - Einstein
I'd need to try it without strtol.
ssharish, but if I were to enter less than 20 inputs, wouldn't it loop until I have 20 inputs?
More specifically, CTRL C is not EOF. It's typically CTRL D or CTRL Z, depending on your OS.
See also: End-of-file - Wikipedia, the free encyclopedia
Quzah.
Hope is the first step on the road to disappointment.
Same result with the == 1
Same result as what? You need to type something that the computer will not recognize as a number -- any letter will do. If you only intend to just read one line of input, then this loop isn't for you.
Well this is getting no where, this is something which i propose. If this dosn't suite your requirnment, i would strongly suggest to give up with this idea and use char to terminate the loop.
ssharishCode:void flush_buffer( void ); int main ( void ) { char intV[10]; while( scanf( "%[0-9]", intV ) > 0 ) { printf("%s\n", intV ); /* convert read string to int using strtoul */ flush_buffer(); } return 0; } void flush_buffer( void ) { int ch; while( ( ch = getchar() ) != '\n' && ch != EOF ); }
Life is like riding a bicycle. To keep your balance you must keep moving - Einstein
Code:#define MAX 10 void flush_buffer( void ); int main ( void ) { int i = 0, count = MAX; int val[MAX]; while( i++ < count) { if (scanf( " %d", &val) == 0) break; printf"%d\n", val ); flush_buffer(); } return 0; } void flush_buffer( void ) { int ch; while( ( ch = getchar() ) != '\n' && ch != EOF ); }
Last edited by CommonTater; 06-16-2011 at 07:35 PM.