Thread: how to check if an inputted character matches one of a few desired characters.

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    2

    how to check if an inputted character matches one of a few desired characters.

    Hi,

    As part of a project I have to create a calculator that allows the user to input 3 numbers, and two operators from the following: '+', '-', '*' , '/'.
    The inputted operators gets assigned to a char variable (operator1 and operator2). This is done using scanf.

    I want to be able to check that only one of the four operators:  '+' , '-' , '*' and '/'  has been assigned into each char variable and terminate the program if not.

    So if either operator one or two do not contain either '+' , '-' , '*' or '/' the program would be terminated.

    What would be the best way to approach this?
    Any help wold be greatly appreciated thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Maybe
    Code:
    if ( strchr("+-*/",ch) == NULL ) {
       // ch is invalid
    }
    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.

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    2
    Thank you Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help printf desired number of characters
    By Swoorup in forum C Programming
    Replies: 2
    Last Post: 05-22-2012, 07:58 AM
  2. Check to check string for a series of characters
    By robi in forum C Programming
    Replies: 1
    Last Post: 02-28-2012, 05:42 PM
  3. Printing an array of inputted characters
    By Nephilim in forum C Programming
    Replies: 2
    Last Post: 03-13-2011, 08:45 AM
  4. Replies: 2
    Last Post: 12-14-2009, 07:42 AM
  5. check if user input matches a word
    By fakebloo in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2004, 07:12 PM

Tags for this Thread