Getc or Getchar is not reading data

M

mailursubbu

HI,
Below is my program. I compiled it through g++. Now strange thing is,
getc is not reading the data instead its printing the previously read
data ! Please some one let me know whats wrong.

#include<stdio.h>
int main()
{

int a;
char c;
scanf("%d",&a);
printf("%d",a);
c = getc(stdin);
printf("%c",c);
}

Thanks In advance.
Subra
 
R

Richard Heathfield

(e-mail address removed) said:
HI,
Below is my program. I compiled it through g++. Now strange thing is,
getc is not reading the data instead its printing the previously read
data ! Please some one let me know whats wrong.

#include<stdio.h>
int main()
{

int a;
char c;
scanf("%d",&a);

Read an int's value from the text representation given in stdin. Assuming
nothing goes wrong, that will be read correctly, and the /next/ thing in
the stream - which scanf will take a quick peek at and then quietly stick
back onto the input stream - will be a character that can't be interpreted
as part of the int. For example, it might be a newline character pressed by
the user to indicate that he's finished entering a line of information.
printf("%d",a);
c = getc(stdin);

Ah, let's go and collect that newline character.
printf("%c",c);

And now let's display it.
}

Thanks In advance.

Yes. That's basically the problem here. :)

By the way, don't forget that main returns an int, so it's best to return an
int from main. In the absence of anything better to return, I suggest 0.
 
M

mailursubbu

Then how can I read the Integer and the Character ?? Any solution ???
Its happening consistantly on windows as well as on Linux...
 
M

M.B

use one more getchar/getc (to read newline) after reading integer and
before reading the character
or
use scanf to read character also

Thanks
M.B
 
E

Emmanuel Delahaye

(e-mail address removed) a écrit :
Below is my program. I compiled it through g++.

If you are not using a C compiler, all bets are off. Please use gcc or
the like.
Now strange thing is,
getc is not reading the data instead its printing the previously read
data ! Please some one let me know whats wrong.

#include<stdio.h>
int main()
{

int a;
char c;
scanf("%d",&a);

scanf() is a tricky function that you are using badly:

- missing test of the returned value
- missing purge of pending characters.
printf("%d",a);
c = getc(stdin);

Due to the missing purge, getc() reads without waiting. Note that getc()
returns an int and not a char.
printf("%c",c);
}

Better to use fgets() unless you spend a long time to learn how to use
scanf() correctly.
 
P

pemo

Then how can I read the Integer and the Character ?? Any solution ???
Its happening consistantly on windows as well as on Linux...

scanf(...)

while(getchar() != '\n')
;

c = getchar();
 
R

Richard Heathfield

(e-mail address removed) said:
Then how can I read the Integer and the Character ?? Any solution ???
Its happening consistantly on windows as well as on Linux...

Yes, it would. Firstly, I forgot to observe last time that you are using
g++, which is a C++ compiler rather than a C compiler. I suggest you use
gcc if you wish to program in C. If you prefer to program in C++, please
ask your C++ questions in comp.lang.c++. Thank you.

Secondly, I would suggest that, to deal with text input of any kind, you
read it as a string with fgets, and then do any necessary conversions of
the data yourself using, for example, strtol or sscanf. Otherwise you'll
just litter your code with getchar calls, at the expense of clarity.
 
P

pemo

Richard Heathfield said:
(e-mail address removed) said:


Yes, it would. Firstly, I forgot to observe last time that you are using
g++, which is a C++ compiler rather than a C compiler. I suggest you use
gcc if you wish to program in C. If you prefer to program in C++, please
ask your C++ questions in comp.lang.c++. Thank you.

Secondly, I would suggest that, to deal with text input of any kind, you
read it as a string with fgets, and then do any necessary conversions of
the data yourself using, for example, strtol or sscanf. Otherwise you'll
just litter your code with getchar calls, at the expense of clarity.

Wouldn't something like this work ...

/* could perhaps pass in the buffer size too */
int stdinGet(const char * format, void ** dest)
{
char buffer[100];

if(fgets(buffer, sizeof(buffer), stdin))
{
return sscanf(buffer, format, &(*dest));
}

return 0;
}

int main(void)
{
int n;
char buffer[100];

stdinGet("%d", (void *)&n);

printf("%d\n", n);

stdinGet("%s", (void *)buffer);

printf("%s\n", buffer);

return 0;
}
 
P

pemo

pemo said:
Richard Heathfield said:
(e-mail address removed) said:


Yes, it would. Firstly, I forgot to observe last time that you are using
g++, which is a C++ compiler rather than a C compiler. I suggest you use
gcc if you wish to program in C. If you prefer to program in C++, please
ask your C++ questions in comp.lang.c++. Thank you.

Secondly, I would suggest that, to deal with text input of any kind, you
read it as a string with fgets, and then do any necessary conversions of
the data yourself using, for example, strtol or sscanf. Otherwise you'll
just litter your code with getchar calls, at the expense of clarity.

Wouldn't something like this work ...

/* could perhaps pass in the buffer size too */
int stdinGet(const char * format, void ** dest)
{
char buffer[100];

if(fgets(buffer, sizeof(buffer), stdin))
{
return sscanf(buffer, format, &(*dest));
}

return 0;
}


int stdinGet(const char * format, void ** dest)
{
char buffer[100];

if(fgets(buffer, sizeof(buffer), stdin))
{
return sscanf(buffer, format, &(*dest));
}

return 0;
}

What's the type of the *dest above?

When used like this ...

int n;

stdinGet("%d", (void *)&n);

The routine's fine, but, if dest's type is changed to void *, gcc complains
about a void * dereference. This seems like a right hack to me - my code
above - but *as ever*, I'm confused: About the type of *dest, why the
compiler doesn't complain about the code 'as is', and why the call to the
routine with 'just' a void * cast isn't complained about - the expression
casts the int * to a void *, yet when this address is passed to stdinGet as
a void **, there's no complaint.
 
E

Emmanuel Delahaye

pemo a écrit :
Wouldn't something like this work ...

Hardly... (BTW, <stdio.h> is missing)

123456
123456

æ╣└w◄
/* could perhaps pass in the buffer size too */
int stdinGet(const char * format, void ** dest)

void ** ? Scary !
{
char buffer[100];

if(fgets(buffer, sizeof(buffer), stdin))
{
return sscanf(buffer, format, &(*dest));

This '&(*' thingy sounds weird...
}

return 0;
}

int main(void)
{
int n;
char buffer[100];

stdinGet("%d", (void *)&n);

Don't use cast to hide bad pratices... It's evil and will bite you some
day...
printf("%d\n", n);
stdinGet("%s", (void *)buffer);

Are you a member of the random programming cult ?
printf("%s\n", buffer);
return 0;
}

Devillish... Far enough to burn in Hell...

Why do you return the value of sscanf() if you never test it ?
 
E

Emmanuel Delahaye

pemo a écrit :
Wouldn't something like this work ...

Hardly... (BTW, <stdio.h> is missing)

123456
123456

æ╣└w◄
/* could perhaps pass in the buffer size too */
int stdinGet(const char * format, void ** dest)

void ** ? Scary !
{
char buffer[100];

if(fgets(buffer, sizeof(buffer), stdin))
{
return sscanf(buffer, format, &(*dest));

This '&(*' thingy sounds weird...
}

return 0;
}

int main(void)
{
int n;
char buffer[100];

stdinGet("%d", (void *)&n);

Don't use cast to hide bad pratices... It's evil and will bite you some
day...
printf("%d\n", n);
stdinGet("%s", (void *)buffer);

Are you a member of the random programming cult ?
printf("%s\n", buffer);
return 0;
}

Devillish... Far enough to burn in Hell...

Why do you return the value of sscanf() if you never test it ?

Try this

#include <stdio.h>

int stdinGet (int* p_dest)
{
int ret = 0;

if (p_dest != NULL)
{
char buffer[32];

if (fgets(buffer, sizeof buffer, stdin) != NULL)
{
ret = sscanf (buffer, "%d", p_dest);
}
}
return ret;
}

int main(void)
{
int n;

int ret = stdinGet(&n);

if (ret == 1)
{
printf("%d\n", n);
}

return 0;
}
 
P

pemo

Emmanuel Delahaye said:
pemo a écrit :

Hardly... (BTW, <stdio.h> is missing)

123456
123456

æ??w?

What's the test data entered?
void ** ? Scary !

See my other post on this.
{
char buffer[100];

if(fgets(buffer, sizeof(buffer), stdin))
{
return sscanf(buffer, format, &(*dest));

This '&(*' thingy sounds weird...
}

return 0;
}

int main(void)
{
int n;
char buffer[100];

stdinGet("%d", (void *)&n);

Don't use cast to hide bad pratices... It's evil and will bite you some
day...
printf("%d\n", n);
stdinGet("%s", (void *)buffer);

Are you a member of the random programming cult ?
??
printf("%s\n", buffer);
return 0;
}

Devillish... Far enough to burn in Hell...

Why do you return the value of sscanf() if you never test it ?

Do you test printf's return value?
 
K

Keith Thompson

pemo said:
scanf(...)

while(getchar() != '\n')
;

c = getchar();

If getchar() returns EOF before returning '\n', this is an infinite loop.

What is the purpose of that last line? It reads the first character
on the line following the one your just read; was that what you
intended?
 
P

Pradyut

--
Pradyut
http://pradyut.tk
http://groups.yahoo.com/group/d_dom/
http://groups-beta.google.com/group/oop_programming
India
HI,
Below is my program. I compiled it through g++. Now strange thing is,
getc is not reading the data instead its printing the previously read
data ! Please some one let me know whats wrong.

This is not a good solution but a works well

#include<stdio.h>
int main()
{

int a;
char c;
scanf("%d",&a);
printf("%d",a);
fflush(stdin);
c = getc(stdin);
printf("\nThis is the charachter %c",c);
return 0;
}

Although it's a very typical solution to such problems
 
E

Emmanuel Delahaye

Pradyut a écrit :

<nothing>

Because you have ansewerd after the signature separator, none of your
prose has been quoted (Thunderbird). Please don't do that.
 
E

Emmanuel Delahaye

Pradyut a écrit :
This is not a good solution but a works well

No, it doesn't work at all.
> fflush(stdin);

This statement invokes an undefined behaviour. It has been explained
before in the thread that you should have read before posting...
 
E

Emmanuel Delahaye

Pradyut a écrit :

<nothing>

Because you have answered after the signature separator, none of your
prose has been quoted (Thunderbird). Please don't do that.
 
F

Flash Gordon

<snip>

Your signature (i.e. the bit I've quoted part of above) belongs *below*
the post, not above it. A number of news readers automatically remove
signatures when replying (or even when displaying) so putting the
signature above is a real pain for some people. Search for quotefix if
you want to get OE to behave a bit better.
This is not a good solution but a works well

#include<stdio.h>
int main()
{

int a;
char c;
scanf("%d",&a);
printf("%d",a);
fflush(stdin);

No, no, a thousand times no. We've just had a few posts saying the
fflush is defined for output streams only, so *why* are you suggesting
using it on an input stream?
c = getc(stdin);
printf("\nThis is the charachter %c",c);
return 0;
}

Although it's a very typical solution to such problems

<snip>

It may be typical amongst those who have learnt from bad books, but it
is not typical amongst those who have a good understanding of C.
 
P

pemo

Keith Thompson said:
If getchar() returns EOF before returning '\n', this is an infinite loop.

What is the purpose of that last line? It reads the first character
on the line following the one your just read; was that what you
intended?

Oops - yes, it was intended, but it shouldn't have been copied into my post.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top