Math in C++

This is a discussion on Math in C++ within the C++ Programming forums, part of the General Programming Boards category; How can I get the program below to reject all characters except numbers and these symbols (-, =, +, *, ...

  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 (-, =, +, *, /, %)
    Attached Files Attached Files

  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
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 03:40 AM
  3. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 02: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, 09:06 PM

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