Thread: Checking User Input

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    3

    Checking User Input

    I'm working on a simple windows application that loads and reads text from a file. I've already gotten the loading and printing code written. But, I'm at a little bit of a hangup, being that I'm new to programming in C. What I would like help on is adding sort of a "command" to my program. IE: If the player types the word "read" into the console, then the application loads the text file, and prints it.

    Thanks, Dusterdoo.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Do you know how to call fgets and compare strings with strcmp? That would be a start.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    I've heard of it, but the book I'm reading doesn't give much help on what I'm trying to do.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Generally speaking, you're not going to find "exactly what I'm trying to do" in a book. You're going to find all the tools you'll need; your book (or other tutorials) will tell you how to use fgets and strcmp. The point is, they'll teach you how to use them for everything, not just for this one particular application. So use your book to find out what fgets and strcmp do, and then you should have some idea as to how to use them to do what you want.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    Let me rephrase. The book I'm reading doesn't really explain fgets and strcmp at all. It just shows you an example program with only a couple comments.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Shouldn't be too difficult, based on what you say you've done already. Reading from the command prompt is just like reading from a file.

    I guess your code is something like

    Code:
    int main(void)
    {
       .... read file and print it
    }
    So you just slot options reading in before you do anything else. I'd structure it like:

    Code:
    int main(void)
    {
         buffer[MAX_STRING_LENGTH];
         while (fgets(buffer, MAX_STRING_LENGTH, stdin))  // this will stop when EOF is detected. EOF is Ctrl+Z on windows. you might want to accept an 'exit' command as well (see below).
        {
             if (strcpy(buffer, "read") == 0)
                 .... read file and print it
             else if (strcpy(buffer, "exit") == 0)
                  break;  // exit while loop.
                     
        }
    
    
    }
    Hope that helps.
    Last edited by smokeyangel; 06-25-2011 at 03:02 PM. Reason: Fix gets -- should be fgets

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Dusterdoo View Post
    Let me rephrase. The book I'm reading doesn't really explain fgets and strcmp at all. It just shows you an example program with only a couple comments.
    The compiler you're using should include documentation of it's library functions (If it doesn't, get a different one), so it's mostly a matter of (GULP) ... are you ready for this.... Reading the help files.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by smokeyangel View Post
    ...
    Code:
    ...     
        while (gets(buffer))  // this will stop when EOF is detected. EOF is Ctrl+Z on windows. you might want to accept an 'exit' command as well (see below).
        {
             if (strcpy(buffer, "read") == 0)
                 .... read file and print it
             else if (strcpy(buffer, "exit") == 0)
                  break;  // exit while loop.
                     
        }
    
    
    }
    All very valid and useful suggestions. One minor correction is to try to avoid using gets() if at all possible. You can read why gets() is bad.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Thanks - of course, you're right. I've edited my previous post to use fgets.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with error checking on input from user.
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 01-23-2009, 12:59 PM
  2. Checking the user input.
    By omnificient in forum C Programming
    Replies: 2
    Last Post: 05-13-2008, 10:03 PM
  3. Checking user input?
    By hayai32 in forum C Programming
    Replies: 9
    Last Post: 04-03-2005, 05:33 PM
  4. Checking user input
    By Cmuppet in forum C Programming
    Replies: 5
    Last Post: 08-05-2004, 10:32 AM
  5. Error checking user input
    By theharbinger in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2003, 09:57 AM