|
|
View previous topic :: View next topic |
Author |
Message |
Aayush13
Joined: 12 Aug 2016 Posts: 14
|
Undefined Identifier EOF |
Posted: Tue Aug 16, 2016 9:40 am |
|
|
Hey guys, I am trying to put getc() in a array and convert the string to a floating point number. So far, I have this:
Code: |
void main() {
int c;
int count;
char arr[50];
c = getchar();
count = 0;
while ((count < 50) && (c != EOF)) {
arr[count] = c;
++count;
c = getchar()
}
for (c=0; c<count; c++) {
putchar(c);
}
putchar('\n');
} |
When I try to compile this program it says that EOF is an undefined identifier? I'm sure it's an easy fix but for some reason I cannot figure it out. Thanks for the help! |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Tue Aug 16, 2016 10:08 am |
|
|
This code comes from, or is based on something else, isn't it?
EOF normally means "End Of File", and suggests that the original code expects to deal with data stored in some sort of file system. When data comes from standard input, which in this case tends to mean from a serial port, then there if no end of file, the port is always open and the data, conceptually at least, is never ending.
With a file system, EOF would be defined by the filesystem code as a special character code that meant: there is no more data. It is there in the original code to take account of the situation where there is not enough data, i.e. less than fifty entires, to fill the array, oreventing the code from waiting forever for data that will never come.
In your code, I strongly suspect that EOF is meaningless and you should delete all references to it. On the other hand, you should also consider what should happen if there isn't enough data for some reason. Generally, with serial input, the concept of a delimiter, some character or characters that tells the code where a number ends. For example, something that reads in numbers will stop reading when it encounters anything that isn't a +/-, digit or full stop (i.e. the decimal point). A simple space is enough to end the number, as is any white space character. |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1907
|
Re: Undefined Identifier EOF |
Posted: Tue Aug 16, 2016 10:17 am |
|
|
Try this:
Aayush13 wrote: | Code: |
#include <ctype.h>
void main() {
int c;
int count;
char arr[50];
c = getchar();
count = 0;
while ((count < 50) && (isdigit(arr[count]))) {
arr[count] = c;
++count;
c = getchar()
}
for (c=0; c<count; c++) {
putchar(c);
}
putchar('\n');
} |
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|