Thread: Next question

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    25

    Next question

    The program ends fine when i enter "Q". but if I enter "1" first and then enter my name, once it loops back to the menu "Q" wont end it anymore.

    help please

    Code:
    int main(int argc, char * argv[])
    {
     // Mainline Variable Declarations
    FILE * output = stdout;
    FILE * input = stdin;
    
    int error=0;
    
    char selection[2], name[20];
    
    while (strcmp(selection,"Q")!=0)
            {
            fprintf(output,"\n1. First name\n2. ISO setting\n3. Aperture setting\n\nPlease enter a menu number or '?' for help\n"
            );
            fscanf(input,"%s",&selection);
            if (strcmp(selection,"1")==0)
                    {
                    fprintf(output,"\nPlease enter your first name\n");
                    fscanf(input,"%s",&name);
                    error=1;
                    }
            }
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    sorry i see

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    wait no i dont.. help still please
    haha

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you're comparing before inputting anything.

    Also, fold your long printf lines like this
    Code:
    printf( "This is a long line\n"
            "And this is another line" );
    Also, you don't need the & when using %s to scan into a char array (it's the exception to the rule).
    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.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    wat am i comparing before inputting.. could u be a bit more precise.. i havnt got much experience

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > while (strcmp(selection,"Q")!=0)
    What does selection contain the first time around?
    - an empty string
    - your input
    - random garbage
    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.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    where does the "random garbage" come from?? so how do i fix it?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kiz View Post
    where does the "random garbage" come from?? so how do i fix it?
    Non-global variables (those local to a function) will contain "garbage" if the variable isn't set to something in particular.

    Code:
       char selection[2] =  {0 };
    As an example.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    random garbage is wahtever is contained in the memory location of an uninitialized variable. You should set 'selection' to something before you enter that while loop.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Location
    South Africa
    Posts
    20
    when you declare:
    char selection[2]

    selection consists of two bytes. They each could have any value. You should initialize them first before checking:

    while (strcmp(selection,"Q")!=0)

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    well I initialized it to just "aa" but i still have the same problem.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Whilst setting your array to "aa" would essentially fix the problem of "random garbage", it is also invalid, becuse "aa" is three bytes long, your array is only two bytes. Remember, C strings have a "end marker" which is zero, so "aa" is 'a', 'a', 0 - three characters.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    i tried intialising it as 'a' aswell

    let try and explain wat isnt working again if it didnt seem clear

    these things happen in the program

    case 1

    1. I run program
    2. I enter 'Q' at menu selection
    3. Program ends

    case 2

    1. I run program
    2. I enter '1' at menu selection
    3. I enter my name
    4. program loops back to menu selection
    5. i enter 'Q'
    6. program loops back to menu selection rather than end

    why wont it end in case 2???

  14. #14
    Registered User
    Join Date
    Sep 2007
    Location
    South Africa
    Posts
    20
    First you should read up on how strings are handled in C.
    eg:
    http://www.cs.cf.ac.uk/Dave/C/node19.html
    http://www.exforsys.com/tutorials/c-...ings-in-c.html
    once you understand strings then:

    Next I recommend that you read up on fscanf() and scanf() before you try to continue.
    eg:
    http://www.cplusplus.com/reference/c...io/fscanf.html

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kiz View Post
    i tried intialising it as 'a' aswell

    let try and explain wat isnt working again if it didnt seem clear

    these things happen in the program

    case 1

    1. I run program
    2. I enter 'Q' at menu selection
    3. Program ends

    case 2

    1. I run program
    2. I enter '1' at menu selection
    3. I enter my name
    4. program loops back to menu selection
    5. i enter 'Q'
    6. program loops back to menu selection rather than end

    why wont it end in case 2???
    Because fscanf() is "stupid" when it comes to reading in strings - this is _ONE_ of many reasons not to use it. It reads a line of text (in the case of "%s") until the first "whitespace", such as newline (from the "enter" key), leaving that whitespace in the input buffer. Next time round, it sees the newline and returns to you with an empty string, because the newline hasn't been read yet. You need to flush the input buffer - check the FAQ for how to "clear the input buffer" [DO NOT USE fflush(input), as that is definitely NOT the right thing to do, check the FAQ on the top of these pages to see why].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM