Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Help. Where is my error?

Reply
Thread Tools

Help. Where is my error?

 
 
Default User
Guest
Posts: n/a
 
      10-21-2005
Red Dragon wrote:

>
> 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
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      10-21-2005
"Red Dragon" <(E-Mail Removed)> writes:
[...]
> 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.


>> 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.


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

"John Doe" <(E-Mail Removed)> writes:

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

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      10-21-2005
"Default User" <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> "Red Dragon" <(E-Mail Removed)> writes:
>> > Sorry. What is Google Groups? I am using Outlook Express and
>> > Visual C++ platform.

>>
>> 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


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.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
David Resnick
Guest
Posts: n/a
 
      10-21-2005
Keith Thompson wrote:
>
> 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

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      10-21-2005
Keith Thompson wrote:

> "Default User" <(E-Mail Removed)> writes:


> > 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



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

 
Reply With Quote
 
David Resnick
Guest
Posts: n/a
 
      10-21-2005

David Resnick wrote:
> Keith Thompson wrote:
> >
> > 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


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

 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      10-24-2005
On Wed, 19 Oct 2005 07:02:44 +0800, "Red Dragon"
<(E-Mail Removed)> wrote:

<snip sample of HTML from OE's multipart/alternative>
> 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
 
Reply With Quote
 
Red Dragon
Guest
Posts: n/a
 
      10-24-2005
>
> 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.


 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      10-30-2005
On 21 Oct 2005 11:10:18 -0700, "David Resnick" <(E-Mail Removed)>
wrote:

> Keith Thompson wrote:

<snips>
> > getchar("%c",&b);


> > 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.


> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments