Help. Where is my error?

K

Kevin Bagust

Red said:
2 keys. One for 1 and One for Enter.
Can you kindly tell me is using 2 scanf() the best solution to the problem?

No, What would happen if the user entered more than one character on the
line before entering Enter?
What is a better method?

Have a look at the getchar() function. Use that to write a bit of code
that will keep getting characters until it receives a new line.

Kevin Bagust.
 
M

Mark McIntyre

2 keys. One for 1 and One for Enter.

.... so you see now where the newline comes from.
Can you kindly tell me is using 2 scanf() the best solution to the problem?
What is a better method?

Personally I prefer to fgets() into a char array of known size, then
parse the array via eg sscanf. I can then decide if there's still
input waiting to be read, and if so I can empty it via repeated calls
to say getchar.

This is probably a FAQ (12.18, 12.20, 12.26 all seem relevant to this
discussion, plus a few others in that area).
 
R

Red Dragon

I tried getchar() and found it has same effect as scanf()
Anyway thanks at lot.
Rgds,
Khoon.
 
K

Keith Thompson

Red Dragon said:
I tried getchar() and found it has same effect as scanf()
Anyway thanks at lot.
Rgds,
Khoon.

Surely you've been here long enough to know how to post a proper
followup using Google Groups. I've even seen you do it in this
thread.

Incidentally, saying that getchar() has the same effect as scanf() is
meaningless without context. They're obviously two different
functions, doing different things. If you'll show us some code, we
can suggest improvements.
 
R

Red Dragon

Keith Thompson said:
Surely you've been here long enough to know how to post a proper
followup using Google Groups. I've even seen you do it in this
thread.

Sorry. What is Google Groups? I am using Outlook Express and Visual C++ platform.
Incidentally, saying that getchar() has the same effect as scanf() is
meaningless without context. They're obviously two different
functions, doing different things. If you'll show us some code, we
can suggest improvements.

Thank you. Here is a program to illustrate my point.

/*Program to Demonstrate getchar() and scanf()*/
#include <stdio.h>

int main (void)
{
char a,b;

/*Section 1*/
printf("Enter a character > ");
scanf("%c",&a);
printf("a=%c\n",a);
printf("Enter another character > ");
getchar("%c",&b);
printf("b=%c\n",b);

/*section 2*/
printf("\nEnter a character > ");
scanf("%c",&a);
printf("a=%c\n",a);
printf("Enter another character > ");
scanf("%c",&b);
printf("b=%c\n",b);

/*section 3*/
printf("\nEnter a character > ");
scanf("%c",&a);
printf("a=%c\n",a);
printf("Enter another character > ");
scanf("%c",&b); /* Dummy */
scanf("%c",&b);
printf("b=%c\n",b);

return 0;
}
/*PRINT RESULT
Enter a character > a
a=a
Enter another character > b=¦

Enter a character > a
a=a
Enter another character > b=

Enter a character > a
a=a
Enter another character > b
b=b
Press any key to continue
*/

You can see that I have divided the program into 3 sections.
1st section with getchar()
2nd section with single scanf()
3rd section with double scanf()
In the Printout Result, only 3rd Section with a Dummy performed perfectly.
BTW, Do you have HTML problem with my program above?
Regards and thank you very much.
Khoon.
 
K

Keith Thompson

Red Dragon said:
Sorry. What is Google Groups? I am using Outlook Express and Visual C++
platform.

Sorry, my mistake.

groups.google.com has a broken interface that encourages users to post
followups with no attributions or quoted text. I don't know why I
didn't check your article's headers before assuming you were using
Google Groups.

In general, posting a followup that doesn't quote any of the parent
article is considered rude.
Thank you. Here is a program to illustrate my point.

/*Program to Demonstrate getchar() and scanf()*/
#include <stdio.h>

int main (void)
{
char a,b;

/*Section 1*/
printf("Enter a character > ");
scanf("%c",&a);
printf("a=%c\n",a);
printf("Enter another character > ");
getchar("%c",&b);
printf("b=%c\n",b);

Ok, I was starting to write an explanation of what this code does when
I noticed this call:

getchar("%c",&b);

getchar() takes no arguments and returns an int value representing
the value of the input character or EOF. You have the required
"#include <stdio.h>" at the top of the program, so the compiler
knows this. Any working C compiler should give you an error message
on that line, or at least a warning.

Either you're running the compiler in a mode that causes it not to
display the error message (don't do that), or you're getting a warning
and ignoring it (don't do that), or the code you posted isn't the same
as the code you compiled (once again, don't do that).

If you're going to post code, you need to copy-and-paste the *exact*
code that you fed to the compiler. If you try to re-type it, you'll
make mistakes (I know I would), and there's no way we can guess which
errors are in the original code and which you introduced by re-typing
it.

If that really was the code you compiled, including the getchar() call
with too many arguments, you need to compile without turning off
diagnostic messages, and you need to fix any errors flagged by the
compiler.

[...]
BTW, Do you have HTML problem with my program above?

Not that I can see, but I think my newsreader sometimes quietly
renders HTML as plain text.
 
R

Red Dragon

In general, posting a followup that doesn't quote any of the parent
article is considered rude.

Yes. I agree with that.
I was responding to the suggestion by Kevin Bagust to try using getchar() to solve the problem of scanf() being unable to read input character. As I learnt from Mark McIntyre, it could not read my character input because it was reading "newline' and jumped. This problem was demonstrated in my exhibit program as Section 1 using getchar() and section 2 using single scanf(), both failed to execute. Only when I used double scanf() was the problem solved.
Ok, I was starting to write an explanation of what this code does when
I noticed this call:

getchar("%c",&b);

getchar() takes no arguments and returns an int value representing
the value of the input character or EOF. You have the required
"#include <stdio.h>" at the top of the program, so the compiler
knows this. Any working C compiler should give you an error message
on that line, or at least a warning.

I started C programing a month ago, self study on a book " A Structured Programming Approach Using C by Forouzan and Gilberg. I think it is very good.
Now I am on into Looping in Chapter 6, and I see getchar() is a topic in Chapter 7.
Either you're running the compiler in a mode that causes it not to
display the error message (don't do that), or you're getting a warning
and ignoring it (don't do that), or the code you posted isn't the same
as the code you compiled (once again, don't do that).

When I compiled my code as shown in my previous program, I got 0 errors and 0 warnings.
If you're going to post code, you need to copy-and-paste the *exact*
code that you fed to the compiler. If you try to re-type it, you'll
make mistakes (I know I would), and there's no way we can guess which
errors are in the original code and which you introduced by re-typing
it.
When Tim Rentsch posted back to me what he had received, I was horrified by the load of gibberish HTML codes I had inadvertently created.
What I had earlier done was to copy from my Visual C++ platform and paste into Outlook Express. What I now do is to cut and paste into Notepad, and then cut from Notepad and paste into Outlook. In this way, I reckon I have killed all the HTML code. I did not do any retyping.
Not that I can see, but I think my newsreader sometimes quietly
renders HTML as plain text.

Thank you very much.
Regards,
Khoon.
 
N

Netocrat

On Fri, 21 Oct 2005 17:19:09 +0800, Red Dragon wrote:
[in reply to Keith Thompson]
I was responding to the suggestion by Kevin Bagust to try using getchar()
to solve the problem of scanf() being unable to read input character.
As I learnt from Mark McIntyre, it could not read my character input
because it was reading "newline' and jumped.

Actually the point is that it was _not_ reading newline.
This problem was
demonstrated in my exhibit program as Section 1 using getchar() and
section 2 using single scanf(), both failed to execute. Only when I
used double scanf() was the problem solved.

The dummy scanf is reading the newline (generated when you press
enter/return) that was not read by the first scanf.

[...]
I see getchar() is a topic in Chapter 7.

getchar() doesn't take arguments as scanf does, it returns a value.

Your code getchar("%c",&b); should be rewritten b = getchar();

[...]
When I compiled my code as shown in my previous program, I got 0 errors
and 0 warnings.

Find out how to invoke your Visual C++ compiler in ANSI or ISO C mode
(these may be abbreviated as C89/C90/C99). In such a mode it is required
to issue a diagnostic for your code.

[...]
When Tim Rentsch posted back to me what he had received, I was
horrified by the load of gibberish HTML codes I had inadvertently
created. What I had earlier done was to copy from my Visual C++ platform
and paste into Outlook Express. What I now do is to cut and paste into
Notepad, and then cut from Notepad and paste into Outlook. In this
way, I reckon I have killed all the HTML code.

Some of your posts - even those without source code - still contain HTML,
including the one to which I'm replying and its predecessor. It is
likely that Outlook is generating it - check that Plain text is still
selected for News sending on the Send tab.
 
D

Default User

Keith said:
Sorry, my mistake.


If you can get your newsreader to display custom message headers
(XanaNews does) then adding the User-Agent will help differentiate
Google from other posts.

Example:
User-Agent: G2/0.2




Brian
 
D

Default User

Red said:
BTW, Do you have HTML problem with my program above?
Regards and thank you very much.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

Keith may not see it, but I do. Weren't you given instructions on how
to set OE so it doesn't do that?



Brian
 
K

Keith Thompson

Red Dragon said:
I was responding to the suggestion by Kevin Bagust to try using getchar()
to solve the problem of scanf() being unable to read input character.
As I learnt from Mark McIntyre, it could not read my character input
because it was reading "newline' and jumped. This problem was
demonstrated in my exhibit program as Section 1 using getchar() and
section 2 using single scanf(), both failed to execute. Only when I
used double scanf() was the problem solved.

I think this is a major misconception that you need to correct. The
getchar() and scanf() calls are *not* failing to execute. The calls
are being executed, and they're almost certainly doing exactly what
they're supposed to do. They're just not doing what you expect them
to do.

I started C programing a month ago, self study on a book " A Structured
Programming Approach Using C by Forouzan and Gilberg. I think it is
very good.
Now I am on into Looping in Chapter 6, and I see getchar() is a topic in
Chapter 7.

When I compiled my code as shown in my previous program, I got 0 errors
and 0 warnings.

Ok, that's bizarre. What compiler are you using, and with what
command-line arguments?

Try compiling the following:

#include <stdio.h>
int main (void)
{
char b;
getchar("%c",&b);
return 0;
}

Remember, getchar() takes no arguments and returns an int. A typical
compiler probably won't complain about ignoring the returned result,
but *any* compiler should complain about the incorrect arguments.

Does your compiler have an option that enable additional warnings?
If so, use it. Once you get your compiler to complain about the
getchar() call, fix that and any other errors it flags, and re-post it.

BTW, you seem to be snipping attributions on quoted text, the lines
that say something like


You need to leave those in so readers can tell who wrote what.
 
K

Keith Thompson

Default User said:
If you can get your newsreader to display custom message headers
(XanaNews does) then adding the User-Agent will help differentiate
Google from other posts.

Example:
User-Agent: G2/0.2

The header was right in front of me. I use
Organization: http://groups.google.com
to tell me that something was posted through Google. The article in
question had
Organization: TMnet Malaysia
My brain just decided to take a little nap at that moment.
 
D

David Resnick

Keith said:
Ok, that's bizarre. What compiler are you using, and with what
command-line arguments?

Try compiling the following:

#include <stdio.h>
int main (void)
{
char b;
getchar("%c",&b);
return 0;
}

Remember, getchar() takes no arguments and returns an int. A typical
compiler probably won't complain about ignoring the returned result,
but *any* compiler should complain about the incorrect arguments.

Does your compiler have an option that enable additional warnings?
If so, use it. Once you get your compiler to complain about the
getchar() call, fix that and any other errors it flags, and re-post it.

Just curious, if it were a really old library (or old headers)
-- pre-ansi -- would stdio.h have

int getchar();

instead of

int getchar(void);

Would that then not compile and work just fine, and furthermore
quite likely give no warnings of any sort? As in the following
example:

temp(1625)$ cat foo.c
#include <stdio.h>
int foo()
{
puts("got foo!");
return 0;
}
int main (void)
{
foo("a", 1, 2);
return 0;
}
temp(1626)$ gcc -ansi -pedantic -Wall -o foo foo.c
temp(1627)$

You get no warnings. Just wondering if OP somehow
has headers without the void...

-David
 
D

Default User

Keith said:
The header was right in front of me. I use
Organization: http://groups.google.com
to tell me that something was posted through Google. The article in
question had
Organization: TMnet Malaysia


That'll work too.
My brain just decided to take a little nap at that moment.

It IS a Friday (at least where I am). You can't much blame the brain.


Brian
 
D

David Resnick

David said:
Just curious, if it were a really old library (or old headers)
-- pre-ansi -- would stdio.h have

int getchar();

instead of

int getchar(void);

Would that then not compile and work just fine, and furthermore
quite likely give no warnings of any sort? As in the following
example:

temp(1625)$ cat foo.c
#include <stdio.h>
int foo()
{
puts("got foo!");
return 0;
}
int main (void)
{
foo("a", 1, 2);
return 0;
}
temp(1626)$ gcc -ansi -pedantic -Wall -o foo foo.c
temp(1627)$

You get no warnings. Just wondering if OP somehow
has headers without the void...

-David

In answer to my own question, you can get this
behavior with getchar on solaris/gcc with
-traditional argument:

temp(593)$ cat foo.c
#include <stdio.h>
#undef getchar
int main (void)
{
int a = getchar("a", 1, 2);
printf("a = %c\n", a);
return 0;
}
temp(594)$ gcc -o foo -traditional -Wall foo.c
temp(595)$

Need to undef getchar, because macro version of it DOES care
about number of arguments...

-David
 
D

Dave Thompson

On Wed, 19 Oct 2005 07:02:44 +0800, "Red Dragon"

Thank you Tim,
I absolutely have no idea of the problem until I saw your post to me. Not
even I dont like to read it, I am unable to read it.
I had purposely sent myself a returned copy of the mail and it was not like
this. The returned copy had not a single line of HTML code.

You probably mean you didn't _see_ any HTML code. It was almost
certainly there. OE when it finds (correctly labelled) HTML in email
or netnews will silently _render_ (that is, execute) it. There used to
be a menu operation "View Source" which shows the actual "raw"
message, although the last time I tried to use it on a relatively
recent version (about 2002 or 2003) it had either been removed or
hidden too well for me to find it. This is (was?) similar to the (more
sensible) operation of the web browser Internet Explorer which by
default when it fetches an HTML page executes it and displays the
formatted result, but you can use View Source to see the actual code.
Alternatively I think you can File / Save As to a file in .txt format
and then open in notepad or any "not overly clever" editor.
I suppose why this problem arises is because only readers with Outlook
Express get the mail in its perfect state. Others with different platform
will get it all in HTML.

There are some other newsreaders that do render HTML, at least
optionally. But not all, which is why in many newsgroups including
here it is preferred to use plain text. As you seem to be doing
correctly at least some times.
Thanks for enlightening me.
Regards,
Khoon.

- David.Thompson1 at worldnet.att.net
 
R

Red Dragon

There are some other newsreaders that do render HTML, at least
optionally. But not all, which is why in many newsgroups including
here it is preferred to use plain text. As you seem to be doing
correctly at least some times.

- David.Thompson1 at worldnet.att.net

Thanks for the information.
Regards,
Khoon.
 
D

Dave Thompson

Keith Thompson wrote:
Just curious, if it were a really old library (or old headers)
-- pre-ansi -- would stdio.h have

int getchar();

instead of

int getchar(void);

Would that then not compile and work just fine, and furthermore
quite likely give no warnings of any sort? As in the following
example: <snip>
It is quite likely and certainly permitted to go undiagnosed.

It's less likely and certainly not guaranteed to work; that depends on
the implementation and in particular the calling convention(s), which
in turn usually depends on the platform. For some systems including
x86 Windows with its de-facto standard cdecl, it does work; on other
systems including some I've used it crashes or destroys data.

- David.Thompson1 at worldnet.att.net
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top