regarding getchar()

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
40019JR
New poster
Posts: 9
Joined: Fri Dec 12, 2003 11:41 pm

regarding getchar()

Post by 40019JR »

Hi,
I got compiler error for using getchar()
since I wanna read whitespace and new lines, what can I use?
Thanks.
Andrey Mokhov
Experienced poster
Posts: 128
Joined: Fri Nov 15, 2002 7:45 am
Location: Kyrgyzstan

Post 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.
40019JR
New poster
Posts: 9
Joined: Fri Dec 12, 2003 11:41 pm

about get char and question 272

Post 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]
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post 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.
40019JR
New poster
Posts: 9
Joined: Fri Dec 12, 2003 11:41 pm

Thanks it worked.

Post by 40019JR »

Yes, after including stdio.h I got it accepted. :D
Thank you.
Post Reply

Return to “C++”