Thread: Math in C++

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    3

    Question Math in C++

    How can I get the program below to reject all characters except numbers and these symbols (-, =, +, *, /, %)

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    25
    You need to write your own input function using get
    something like this:
    Code:
    do {
    	a = cin.get()
    	if ( a == desiredchar )
    		input += a
    } while ( inputting )

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    A switch and case statement may be easier to use.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    bool ValidChar(char C)
    {
      if((C >= '0') && (C <= '9')) return true;
      if(C == '-') return true;
      if(C == '=') return true;
      if(C == '+') return true;
      if(C == '*') return true;
      if(C == '/') return true;
      if(C == '%') return true;
      return false;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Or even
    Code:
    bool ValidChar(char C)
    {
      return strchr( "0123456789-+*/%=", C ) != NULL;
    }
    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.

  6. #6
    Registered User
    Join Date
    Jan 2004
    Posts
    3
    Where do I put the bool statement? (I'm a newbie.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math
    By knightjp in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-01-2009, 05:36 PM
  2. Help with C++ Math
    By aonic in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2005, 04:40 AM
  3. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  4. Math Header?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 09-17-2004, 06:39 AM
  5. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM