Thread: how can I let the user input the word 'help' in my program??

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    Question how can I let the user input the word 'help' in my program??

    Hello all,
    I am new in programming and I am teaching myself through tutorials and youtube videos. I am making a program in which the user inputs numbers 0-5. each number calls a function and run specific operations that relate to that function. I want to add a function where if the user enters the word "help" rather than a number, that the program will print help documentation out on the screen. My problem is, since the input the user puts in is an int ((meaning the program is expecting an input from the user of an int from 0-5, calling that variable num_input)), how do I make an exception to where if the user enters the word ''help" that the program will not throw an error because it is expecting a number.

    For example: what my program does now is:

    Enter a number: 1
    Enter a number : 17
    Enter a 2nd number: 5
    17 % 5 is 2

    enter a number: 4
    Type a fahrenheit temperature: 32
    The centigrade equivalent of 32 is 0.0

    I want the program to do this:
    Enter a number: help

    ((print out some help documentation))
    ________________________

    Your insights are highly appreciated.

    Thanks,
    Hanna

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Read the input as a string, then check if the string is a command such as help. If not, parse the string as an integer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    Quote Originally Posted by laserlight View Post
    Read the input as a string, then check if the string is a command such as help. If not, parse the string as an integer.
    The input is already read in as an integer. do you mean to read all inputs of numbers as strings as well?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by kharabeesh View Post
    The input is already read in as an integer. do you mean to read all inputs of numbers as strings as well?
    read in the input as a string, into a char array. Then if string[0] is greater than > '9', you must have a string with non-number char's.

    Code:
    if ((strcmp("help", charArray)) == 0)  //it's "help"!
    If it's not, get your number out of the string with sscanf(), strtoi(), or etc.
    Last edited by Adak; 10-12-2010 at 01:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strtok help on a word frequency program
    By steafo in forum C++ Programming
    Replies: 2
    Last Post: 07-25-2010, 09:17 PM
  2. word count program not working
    By vsovereign in forum C Programming
    Replies: 5
    Last Post: 06-04-2010, 02:11 PM
  3. IDE for C embedded advanced editing
    By Undici77 in forum Tech Board
    Replies: 32
    Last Post: 01-16-2010, 05:17 PM
  4. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM