A nested if inside the switch

This is a discussion on A nested if inside the switch within the C Programming forums, part of the General Programming Boards category; Originally Posted by Syked4 Extro Just a couple of observations: You have declared ' pay ' as an int and ...

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Syked4
    Extro
    Just a couple of observations:

    You have declared 'pay' as an int and then go on to use it as a char
    That's the correct way to do it, since getchar actually returns an integer. Read the FAQ as to why you have to use an int to test for EOF when using characters.
    Quote Originally Posted by Syked4
    Second; the '\n' left in the buffer is causing the second prompt for input. There is a '\n' left in the buffer after the last call for input which is being immediately assigned to 'pay' in your while statement second time through satisfying the '\n' case.
    :reads his last post: Yep, that's already been covered quite clearly.
    Quote Originally Posted by Syked4
    While I know that it’s frowned on, using fflush(stdin) as the last line of your 'while' loop should take care of this.
    No it shouldn't. There's nothing at all in the standard that says this should ever work. In fact, it should never work. The only reason it "works" ever is because Microsoft decided to ........ up^H^H^H^H^H^H^Hextend the language and make the function do something it should never do. fflush is used for output streams only. :reads his last post: Wait a minute, I already covered that too.

    Quote Originally Posted by Syked4
    Thirdly getchar() only reads in one character, so using '-1' or 'EOF' to end your program wont work.getchar() will only load the '-' or the 'E'.
    :reads last post again: Holy Deja Vu, Batman! I covered this too!
    Quote Originally Posted by Syked4
    Maybe scanf is a better choice.
    I wouldn't go that far. Wait a second! I already showed a better way to do this too!
    Quote Originally Posted by Syked4
    EDIT: Missed your last post, so this refers to the code from post befor the do while change.
    Apparently you missed my last post too, because I showed a better way to do everything you mentioned.


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    n00b
    Join Date
    May 2005
    Location
    Lakefield
    Posts
    12
    Salem
    In doManager and main you declare a string of BUFSIZ elements. Can you explain the use of this for me.
    Also in *promptAndAnswer, can you explain the use of the '*' and is size_t a type.

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) BUFSIZ is a macro defined in stdio.h. It is defined by the C standard.

    2) The * denotes that you're using a pointer. A pointer is a variable that stores the address (memory location) of another variable. In this case:
    Code:
    char *promptAndAnswer ( char *buff, size_t size ) {
    promptAndAnswer returns a pointer to a character. (Read it from right to left to easier understand it:
    Code:
    promptAndAnswer   /* the function... */
    * /* returns a pointer to... */
    char /* a character... */
    )
    Also, it as an argument takes a pointer to a character:
    Code:
    promptAndAnswer ( char *buff,
    buff is a * pointer to a char

    3) size_t is a data type defined by the C standard. It is defined to be an unsigned integeral type. Usually defined as an unsigned long, but it doesn't have to be. It only has to be unsigned, integeral. It's guarinteed to be safe for indexing arrays, as you can't run off the lower boundry. (Nothing stops you from running off the top end though, except your careful attention.)


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    n00b
    Join Date
    May 2005
    Location
    Lakefield
    Posts
    12
    quzah
    Thanks for the slap and the fflushstdin() function. Both appreciated. In main should c be choice?

    OPPS Two pages now. Thanks also for the explanations.
    Last edited by Syked4; 08-14-2005 at 02:11 PM.

  5. #20
    Registered User
    Join Date
    May 2004
    Posts
    68
    Well I got it to work with fflush(stdin) and so I think it's best to leave well enough alone. Also, I dont wanna get too much into into c commands and functions we havent covered or he(my teacher) may not think it was me that did it, a risk I'd rather not run.
    Thanks for all the help Quzah and others.
    Regards,
    Extro

  6. #21
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    If your teacher thinks fflush(stdin) is a good idea, ask them what they think of gets().
    If they don't recognise it's faults, then you're not going to learn anything useful from them, because they do not know enough C themselves.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested array vs. tree
    By KONI in forum Tech Board
    Replies: 1
    Last Post: 06-07-2007, 05:43 AM
  2. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 11:57 AM
  3. unwanted number in switch
    By chrismax2 in forum C++ Programming
    Replies: 5
    Last Post: 04-24-2004, 06:43 AM
  4. can (switch) be apart of a loop?
    By matheo917 in forum C++ Programming
    Replies: 2
    Last Post: 09-20-2001, 07:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21