Hi,
I got compiler error for using getchar()
since I wanna read whitespace and new lines, what can I use?
Thanks.
regarding getchar()
Moderator: Board moderators
-
- Experienced poster
- Posts: 128
- Joined: Fri Nov 15, 2002 7:45 am
- Location: Kyrgyzstan
about get char and question 272
[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]
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]
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.
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.
Yes, after including stdio.h I got it accepted.
Thank you.

Thank you.