A very small problem

This is a discussion on A very small problem within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> main() { int a,b,c; char lit; printf("A number: "); scanf("%d", &a); printf("Another number: "); scanf("%d", &b); printf("Alege!"); ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    A very small problem

    Code:
    #include <stdio.h>
    
    main()
    {
              int a,b,c;
              char lit;
              printf("A number:  ");
              scanf("%d", &a);
              printf("Another number: ");
              scanf("%d", &b);
              printf("Alege!");
              printf("\n a ) Sum");
              printf("\n b ) Multiply\n");
              scanf("%s", &lit);
    
              switch(lit)
             {
                 case 'a':
                        c  = a + b;
                        printf("Answer: %d", c);
                        break;
                 case 'b':
                        c = a * b;
                        printf("Answer: %d", c);
                        break;
                 default:
                        break;
             }
    return 0;
    }
    When I run the code, the second variable ( b ) takes always the value 0, despite the fact that I choose another value then 0.
    What do you think?

  2. #2
    Registered User khpuce's Avatar
    Join Date
    May 2003
    Posts
    165
    I can't see anything wrong in the code. It should work...

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Flush the input buffer after each call to scanf().

    Read the faq for information on flushing the input buffer.

    Also change the "%s" to "%c" in the line : scanf("%s", &lit);

  4. #4
    Registered User khpuce's Avatar
    Join Date
    May 2003
    Posts
    165
    Flush the input buffer after each call to scanf().
    Also change the "%s" to "%c" in the line : scanf("%s", &lit);
    I've just tried Iahim's code in my machine without any modification and it works fine.

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Originally posted by khpuce
    I've just tried Iahim's code in my machine without any modification and it works fine.
    That doesn't necessarily mean that his code is right.

  6. #6
    Registered User khpuce's Avatar
    Join Date
    May 2003
    Posts
    165
    Originally posted by The Dog
    That doesn't necessarily mean that his code is right.
    I am not arguing about the proper way of coding. All I am saying is that his code worked on my machine so I cannot understand why its not working on his.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > I cannot understand why its not working on his.
    Because this assumes a string
    scanf("%s", &lit);

    In all likelyhood, the \0 which %s stores at the end of the string actually ends up overwriting the value of b.

    This of course depends on
    - how much you type in
    - the details of how your compiler arranges local variables
    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.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    4
    Originally posted by The Dog

    Also change the "%s" to "%c" in the line : scanf("%s", &lit);
    I've put %s because %c doesn't work... If I do make the change, it exits the program after it prints on the screen "b) Multiply"(at least, this happens on my machine).

  9. #9
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    probably because the \n is getting left on the input buffer, from the previous reads, scanf does that, then when it hits the switch, it reaches default and breaks; Use fgets instead.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  10. #10
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Might as well add this
    Code:
    int main(void){
    if you don't put the void it assumes it accepts unknown amount of arguments. the void insures that it takes NO arguments. and also, though a function by default is an int if not implicitly stated it should be stated for use with older compilers and standards.

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    38
    %c is not working bcoz u r not flushing
    so either use fflush(sdtin); after scanf("Another Number: ",&b); and use %c instead of %s
    OR
    (as SALEM says /0 is being overwritten)
    declare a pointer to int int *in; and point it to b in=&b keeping the rest of the code same

    my compiler is behaveving in a fashion similar to urs and i have tried the above two methods sucessfull so i hope they'll work 4 u 2
    use any one of the above methods

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by surdy
    %c is not working bcoz u r not flushing
    so either use fflush(sdtin);
    Close, but no cigar. Don't flush input streams. It is undefined behaviour.

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

  13. #13
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Originally posted by linuxdude
    ...and also, though a function by default is an int if not implicitly stated it should be stated for use with older compilers and standards.
    It should be noted that implicit int is deprecated in C99

    ~/

  14. #14
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161

    Re: A very small problem

    Originally posted by Iahim
    Code:
    char lit;
    ...
    printf("\n a ) Sum");
    printf("\n b ) Multiply\n");
    scanf("%s", &lit);
    When I run the code, the second variable ( b ) takes always the value 0, despite the fact that I choose another value then 0.
    What do you think?
    Since lit is a char and you are reading a string, you are undoubtedly overwriting memory. A string has a minimum of 2 characters.

    Since all you want is 1 character, use getchar()

    But that leaves any characters after the 'a' or 'b' in the input stream so you still have to flush the stream. As quzah mentioned elsewhere, use:
    Code:
    char lit;
    ...
    printf("\n a ) Sum");
    printf("\n b ) Multiply\n");
    lit = getchar();
    while (getchar() != '\n');
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  15. #15
    Registered User
    Join Date
    Mar 2004
    Posts
    4
    Code:
    lit = getchar();
    If I use this line, the program ends when it gets to it

    Anyway, I've search the help for some hints( I hate doing the Help search, I'm surprised that it helped me this time), and the only modifications I had to do to this code was on line

    Code:
    scanf("%s", &lit);
    which I've replaced with

    Code:
    lit = getch();
    and, ofcourse, at the begining of the code

    Code:
    #include <conio.h>
    Now the program runs smootly (at least on my computer...)
    Last edited by Iahim; 03-26-2004 at 03:51 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small Problem with double and integer adding?
    By Nathan the noob in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2009, 04:16 PM
  2. Visual C++ small problem
    By gadu in forum C++ Programming
    Replies: 0
    Last Post: 03-10-2009, 10:45 PM
  3. Small problem with this array...
    By Merholtz in forum C Programming
    Replies: 7
    Last Post: 11-03-2008, 03:16 PM
  4. Help with a small problem (beginner)
    By piffo in forum C Programming
    Replies: 13
    Last Post: 09-29-2008, 04:37 PM
  5. Need Big Solution For Small Problem
    By GrNxxDaY in forum C++ Programming
    Replies: 8
    Last Post: 08-01-2002, 03:23 AM

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