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

This is a discussion on how can I let the user input the word 'help' in my program?? within the C Programming forums, part of the General Programming Boards category; Hello all, I am new in programming and I am teaching myself through tutorials and youtube videos. I am making ...

  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
    21,739
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 04:17 PM
  4. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

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