Page 1 of 1
regarding getchar()
Posted: Fri Dec 12, 2003 11:49 pm
by 40019JR
Hi,
I got compiler error for using getchar()
since I wanna read whitespace and new lines, what can I use?
Thanks.
Posted: Sat Dec 13, 2003 7:13 am
by Andrey Mokhov
You are free to use getchar(). I used it in many programs and got AC...
This is the way I usually use it:
[cpp]while((ch=getchar())!=EOF)
{
do_something_with(ch);
}
[/cpp]
Good luck!
Andrey.
about get char and question 272
Posted: Sun Dec 14, 2003 6:01 am
by 40019JR
[cpp]
I still get compiler errors as follows from the program below.
I use VC and it works fine.
Can anybody have suggestions please?
02143358_24.c: In function `int main ()':
02143358_24.c:8: `getchar' undeclared (first use this function)
02143358_24.c:8: (Each undeclared identifier is reported only once for
each function it appears in.)
#include <iostream>
using namespace std;
int main()
{
int counter = 0;
char ch;
while((ch=getchar())!=EOF)
{
if(ch == '"')
{
counter++;
if((counter % 2) == 0)
cout << "'" << "'";
else
cout << '`' << '`';
} //end of if
else
{
cout << ch;
} //end of else
} //end of while
return 0;
}[/cpp]
Posted: Sun Dec 14, 2003 6:28 am
by shamim
Yes, I understand your problem.
You see, getchar() requires the header file <stdio.h>.
But you do not include it in your program.
But it still compiles in VC, because <stdio.h> is automatically included with <iostream>.
I suggest, you seperately include <stdio.h>.
Another advice:
To prevent frequent compile error, you should compile your code in LINUX environment. UVA uses a similar compiler.
Thanks it worked.
Posted: Sun Dec 14, 2003 6:38 am
by 40019JR
Yes, after including stdio.h I got it accepted.
Thank you.