Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / CodeWarrior / March 2004



Tip: Looking for answers? Try searching our database.

GCC

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
KaJe - 02 Mar 2004 02:08 GMT
Argh..ok this is the third time i've posted this and none of them have
ended up in here... How do I get codewarrior to compile with GCC? I'll
compile something on my mac and it won't run right (such as not
noticing fflush(stdin);), but i'll compile it on gcc in Unix and it'll
work fine. Anyone know how I can get this working? Or am I not
creating the right kinds of projects? What projects work for either
Mac OS X and Unix? This is just simple programs for a class, not
graphical stuff.
Gregory Weston - 02 Mar 2004 02:36 GMT
> Argh..ok this is the third time i've posted this and none of them have
> ended up in here... How do I get codewarrior to compile with GCC? I'll
[quoted text clipped - 4 lines]
> Mac OS X and Unix? This is just simple programs for a class, not
> graphical stuff.

I can't help you with getting CW to use another compiler, but I'd think
if fflush() isn't clearing the buffer on a stream opened for reading
it's a fairly obvious bug to report.

G
Eric Albert - 02 Mar 2004 06:11 GMT
> > Argh..ok this is the third time i've posted this and none of them have
> > ended up in here... How do I get codewarrior to compile with GCC? I'll
[quoted text clipped - 8 lines]
> if fflush() isn't clearing the buffer on a stream opened for reading
> it's a fairly obvious bug to report.

It isn't quite as obviously a bug if it's on stdin, as in this case.  See
<http://www.eskimo.com/~scs/C-faq/q12.26.html>.

-Eric

Signature

Eric Albert         ejalbert@cs.stanford.edu
http://rescomp.stanford.edu/~ejalbert/

KaJe - 02 Mar 2004 14:31 GMT
> > > Argh..ok this is the third time i've posted this and none of them have
> > > ended up in here... How do I get codewarrior to compile with GCC? I'll
[quoted text clipped - 13 lines]
>
> -Eric

It's not just that. It also wouldn't let me compile a program once
saying one of my functions didn't have a prototype, but it also
compiled fine on Unix.

KaJe
MW Ron - 02 Mar 2004 15:09 GMT

>It's not just that. It also wouldn't let me compile a program once
>saying one of my functions didn't have a prototype, but it also
>compiled fine on Unix.

You can turn off the require prototypes in the C/C++ langauage settings
but be very careful if you do as this is one of the big shoot yourself
in the foot items in C.  

In C++ you are required to have prototypes.

It could be you are using a function not available in MSL so you'd need  
to use a POSIX header.

Ron

Signature

Announcing  CodeWarrior Development Tools for Windows v9.2
http://www.metrowerks.com/MW/Develop/Desktop/Windows/Professional/Default.htm
Ron Liechty - MWRon@metrowerks.com - <http://www.metrowerks.com>

Gregory Weston - 03 Mar 2004 22:21 GMT
> > > Argh..ok this is the third time i've posted this and none of them have
> > > ended up in here... How do I get codewarrior to compile with GCC? I'll
[quoted text clipped - 11 lines]
> It isn't quite as obviously a bug if it's on stdin, as in this case.  See
> <http://www.eskimo.com/~scs/C-faq/q12.26.html>.

I stand corrected. When I responded I didn't have a copy of the spec at
hand. My first instinct was that flushing a read stream wasn't
necessarily sensible, but instead of going with the instinct I googled
and found several references that implied that it was legit.

G
MW Ron - 04 Mar 2004 03:22 GMT
>> It isn't quite as obviously a bug if it's on stdin, as in this case.  See
>> <http://www.eskimo.com/~scs/C-faq/q12.26.html>.
[quoted text clipped - 3 lines]
>necessarily sensible, but instead of going with the instinct I googled
>and found several references that implied that it was legit.

proof one to me,  then read the standarad which specifically states that
fflush(stdin) or any input buffer is implementation defined  or in other
words it may work and it may not.

Ron

Signature

Announcing  CodeWarrior Development Tools for Windows v9.2
http://www.metrowerks.com/MW/Develop/Desktop/Windows/Professional/Default.htm
Ron Liechty - MWRon@metrowerks.com - <http://www.metrowerks.com>

Gregory Weston - 04 Mar 2004 12:07 GMT
> >> It isn't quite as obviously a bug if it's on stdin, as in this case.  See
> >> <http://www.eskimo.com/~scs/C-faq/q12.26.html>.
[quoted text clipped - 7 lines]
> fflush(stdin) or any input buffer is implementation defined  or in other
> words it may work and it may not.

Already done. As stated, I didn't have a copy of the standard in reach
when I made my first response and second-guessed my instinct that it
wasn't a sensible use of fflush(). I've since learned that I should have
gone with that instinct and acknowledged my error for posterity.
MW Ron - 04 Mar 2004 16:20 GMT
>proof one to me,  then read the standarad which specifically states that
>fflush(stdin) or any input buffer is implementation defined  or in other
>words it may work and it may not.

Actually I was wrong,  it is not implementation defined, (which means it
works but how it works is up to the library vendor) but it is undefined
beahvior which means basically it can do anything including crash your
computer for some behavior.

Here is the description of fflush from the standard, clearly it is not
to be used for input streams at all, only an output stream or an
input/output stream that last did output.  

If stream points to an output stream or an update stream in which the
most recent operation was not input, the fflush function causes any
unwritten data for that stream to be delivered to the host environment
to be written to the file; otherwise, the behavior is undefined.

If stream is a null pointer, the fflush function performs this flushing
action on all streams for which the behavior is defined above.

GCC's Man says the same thing write only...

The function fflush() forces a write of all buffered data for the given
output or update stream via the stream's underlying write function.  

Ron

Signature

Announcing  CodeWarrior Development Tools for Windows v9.2
http://www.metrowerks.com/MW/Develop/Desktop/Windows/Professional/Default.htm
Ron Liechty - MWRon@metrowerks.com - <http://www.metrowerks.com>

MW Ron - 02 Mar 2004 15:18 GMT
>Argh..ok this is the third time i've posted this and none of them have
>ended up in here... How do I get codewarrior to compile with GCC?

Why would you ever want to do that?

> I'll
>compile something on my mac and it won't run right (such as not
>noticing fflush(stdin);),

Using gcc to compile it wouldn't matter this is a library functionality
You want to use the BSD library instead of MSL..  not gcc instead of
mwcc.

> but i'll compile it on gcc in Unix and it'll work fine.

It doesn't work fine, it is a big mistake on your part to assume it is
correct.  The standard specifically warns that this behavior is
undefined.  It is not portable code and you should not use it.  You can
set up a loop to flush an input buffer and that would be portable.

> Anyone know how I can get this working?

In CW 9 in the CodeWarrior Examples is BSD Stationery projects.  Copy
them to the (Project Stationery) folder .   use these BSD project
stationery for a new project and it probably will work the way you want
(but shouldn't expect) it to work.

> Or am I not
>creating the right kinds of projects? What projects work for either
>Mac OS X and Unix? This is just simple programs for a class, not
>graphical stuff.

If your teacher is telling you to use fflush(stdin);  find a more
competent teacher. :)  this is one of the things that is always taught
in basic C programming :)  

Finally  why do you need fflush(stdin)  it is almost always because a
beginner is mixing formatted  (scanf) and unformatted (gets) input
together.  i.e  enter your age and enter your name.

When you enter your age and scanf it you leave the newline so when you
read your name it reads the newline instead and you don't get a name.  
The better way to do this is
enter your age (gets then atoi or atof) enter your name (gets).  this
way you don't have that extra return character mucking up the input.

Ron

Signature

Announcing  CodeWarrior Development Tools for Windows v9.2
http://www.metrowerks.com/MW/Develop/Desktop/Windows/Professional/Default.htm
Ron Liechty - MWRon@metrowerks.com - <http://www.metrowerks.com>

KaJe - 03 Mar 2004 23:20 GMT
> >Argh..ok this is the third time i've posted this and none of them have
> >ended up in here... How do I get codewarrior to compile with GCC?
[quoted text clipped - 43 lines]
>
> Ron

I'm using scanf's but not any gets(*shrugs*). And that's why i'm using
the fflush..to get rid of the newline that comes up due to hitting
enter. It doesn't effect numbers, because int, etc won't notice the
newline..but other data types will be effected by the newline, so I
have to flush before the new data type is entered. This is in a loop
BTW.

Here's a sample of my code:

               printf("Math Menu Program \n");
        printf("A. Add up numbers \n");
        printf("B. Subtract numbers \n");
        printf("C. Multiply numbers \n");
        printf("D. Divide numbers \n");
        printf("E. Exit \n");
        printf("Please enter your choice: ");
        scanf("%c", &letter);
       
        /* Switch/Case statement to read which letter the user entered. */
       
        switch (letter)
        {
            case 'A':
                   
            case 'a':
                printf("Please enter first number to add: ");
                scanf("%d", &num1);
                printf("Please enter second number to add: ");
                scanf("%d", &num2);
                total = num1 + num2;
                printf("The total is %d \n\n", total);
                fflush(stdin);
                break;
David Phillip Oster - 02 Mar 2004 15:27 GMT
> Argh..ok this is the third time i've posted this and none of them have
> ended up in here... How do I get codewarrior to compile with GCC? I'll
[quoted text clipped - 4 lines]
> Mac OS X and Unix? This is just simple programs for a class, not
> graphical stuff.

You aren't creating the right kind of projects.

For simple programs for class, use Mach-O, standard console projects
like C-Console Mach-O.
KaJe - 03 Mar 2004 03:22 GMT
> > Argh..ok this is the third time i've posted this and none of them have
> > ended up in here... How do I get codewarrior to compile with GCC? I'll
[quoted text clipped - 9 lines]
> For simple programs for class, use Mach-O, standard console projects
> like C-Console Mach-O.

Thanks David i'll try that next time, instead of changing EVERYTHING
that is REQUIRED for my class and changing my TEACHER like some ppl
seem to think is good advice....
Thorrsten Froehlich - 03 Mar 2004 09:57 GMT
> > > Argh..ok this is the third time i've posted this and none of them have
> > > ended up in here... How do I get codewarrior to compile with GCC? I'll
[quoted text clipped - 13 lines]
> that is REQUIRED for my class and changing my TEACHER like some ppl
> seem to think is good advice....

Well, your teacher is wrong, so be careful that you might actual learn
other bad practices as well. Anyway, at least telling your teacher
that what he is suggesting is wrong would be a good idea. Most
teachers are humans, too - and they do usually understand that they
can make mistakes, but if nobody tells them... :-)

   Thorsten
MW Ron - 03 Mar 2004 18:45 GMT
>> > Argh..ok this is the third time i've posted this and none of them have
>> > ended up in here... How do I get codewarrior to compile with GCC? I'll
[quoted text clipped - 13 lines]
>that is REQUIRED for my class and changing my TEACHER like some ppl
>seem to think is good advice....

Sorry but that is a pet peeve for me.  But if you had read the message I
did tell you how exactly to do it (incorrectly like you wanted).

Again I am saying (if this was what you were doing) you should not mix
formatted input and unformtted input together, it leads to trouble.  

Ron

Signature

Announcing  CodeWarrior Development Tools for Windows v9.2
http://www.metrowerks.com/MW/Develop/Desktop/Windows/Professional/Default.htm
Ron Liechty - MWRon@metrowerks.com - <http://www.metrowerks.com>

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.