Thread: help for a beginner

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

    help for a beginner

    any reason why this program wont do as its told. I just want it to stop
    displaying the menu when i enter "Q" at this stage. But when i enter any
    character including "Q" the menu is just displayed another 2 times. I
    cant find anything wrong with the code???? Im sure it is a simple problem but i just cant find it

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char * argv[])
    {
     // Mainline Variable Declarations
    FILE * output = stdout;
    FILE * input = stdin;
    
    char selection[1];
    
    while (strcmp(selection,"Q") != 0)
              {
              fprintf(output,"1. First name\n2. ISO setting\n3. Aperture setting\n\nPlease enter a          
              menu number or '?' for help\n");
            
              fscanf(input,"%c",&selection);
              }
    }
    Cheers

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can't really call strcmp (effectively) to compare "Q" with an array only capable of holding a single character (that character would have to be null). Perhaps you want selection to be just a simple char instead of an array of char of length 1. You can then skip the strcmp call and just compare the value of the chars with each other directly.

    Maybe do...while would be better here.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1.
    Code:
    fscanf(input, "%c", &selection)
    is wrong if you want to use "selection" in strcmp(), since there is no guarantee that the string is terminated with a zero. Use %s [1].
    2. Selection, if you are going to use it as a string (e.g. with strcmp), needs to be at the very least 2 bytes.
    3. If you are unlucky, it is possible [even if it's unlikely] that your while loop is never entered, as selection may by coincidence match "Q" - in which case your loop is finished before the user has a chance to choose anything.
    4. If you read using fscanf(), for a char, the last newline [from hitting enter] will be left in the input buffer, so next time round, this will be "accepted" as the char you input. Again, using fgets() will solve this.


    [1] As I've explained to the other 5 people doing the same excercise, you may want to avoid fscanf(), as fgets() is a safer function - reading strings with fscanf() is unsafe because if the user types in a string that is too long, there's nothing in fscanf to indicate the max size of the string. I do understand that your teacher will reduce your score for this, but I would argue that this is completely WRONG, and shows that your lecturer has no clue about how to write user-friendly and reasonably safe software. Getting into a good habit on "think safe" early on in a programming education is a good thing.

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

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    cheers mate that helped

    Im at university atm and just started an engineering program unit. Our lecturer, for some reason, wants us to stick to fscanf for now anyway otherwise we get marked down apparently but i see where u are coming from.

    thanks again

  5. #5
    Registered User
    Join Date
    Sep 2007
    Location
    South Africa
    Posts
    20
    There are a multitude of ways to do this. 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

    here are a few different ways to solve your problem that you might want to look at:
    (these are by no means complete or the best ways to implement your menu. Use them to better understand using &#37;s and %c)

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char * argv[])
    {
        // Mainline Variable Declarations
        FILE * output = stdout;
        FILE * input = stdin;
    
        char selection[256];
    
        do
        {
            fprintf(output,"1. First name\n2. ISO setting\n3. Aperture setting\n\nPlease enter a menu number or '?' for help\n");
            fscanf(input,"%s",selection);
        } while (strcmp(selection,"Q") != 0);
    }
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char * argv[])
    {
        // Mainline Variable Declarations
        FILE * output = stdout;
        FILE * input = stdin;
    
        char selection[256];
    
        while (1)
        {
            fprintf(output,"1. 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,"Q") != 0 || selection[0]=='Q')
            {
                break;
            }
        } 
    }
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char * argv[])
    {
        // Mainline Variable Declarations
        FILE * output = stdout;
        FILE * input = stdin;
    
        char selection[256];
    
        while (1)
        {
            fprintf(output,"1. First name\n2. ISO setting\n3. Aperture setting\n\nPlease enter a menu number or '?' for help\n");
            fscanf(input, "%s", selection);
            if ('Q' == selection[0])
            {
                fprintf(output, "\nYou chose to quit!\n");
                break;
            }
            switch (selection[0])
            {
                case '1':
                    /*do menu 1 stuff*/
                    break;
                case '2':
                    /*do menu 2 stuff*/
                    break;
                case '3':
                    /*do menu 3 stuff*/
                    break;
                case '?':
                     /*display help menu!*/
                     break;
                default :
                    fprintf(output, "\n%c is NOT a valid selection\n", selection[0]);
                    break;
            }
        } 
    }
    Last edited by wyliek; 09-19-2007 at 08:19 AM. Reason: error in code

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://cboard.cprogramming.com/showthread.php?t=93740
    Try to keep to 1 thread per question.

    BTW, there are several other people from your course with the same program style.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM