Use character variable as operator

This is a discussion on Use character variable as operator within the C++ Programming forums, part of the General Programming Boards category; I'm trying to use a user-inputted character variable as an operator, similar to the following: Code: int num1 = 5; ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    3

    Use character variable as operator

    I'm trying to use a user-inputted character variable as an operator, similar to the following:

    Code:
    int num1 = 5;
    int num2 = 5;
    int sum;
    char oper;
    
    cin >> oper;
    
    sum = (num1 oper num2);
    I'm new to programming, and wasn't sure if this is possible. I know how to do it in a different way with a switch statement on the oper variable, but would like to know if it's possible to convert the oper variable in some way that it would be possible to utilize "sum = (num1 oper num2);" without a compilation error. Anyone know?

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    in some languages (python and lisp that I know of), this is possible, but not in C++.
    you could use a switch statement to get around this.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    I don't think you can do that. C++ is not an interpreted language. It doesn't have interpreters. Only way I know of (using only C++) is to build your own eval type-of thing. Which will invariably use a switch statement.

    But maybe someone knows better.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Invariably. You will use a switch statement to do this operation!

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    It doesn't absolutely have to be a switch statement but it is probably the easiest:
    Code:
    switch(oper)
    {
    case '*' :
        sum = num1 * num2;
        break;
    etc... etc... etc...
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    And what happens if I enter "q"?
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    29
    then big people with black suits and dark cars will knock on your door. OR you will use default.

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    3
    Quote Originally Posted by jafet
    And what happens if I enter "q"?
    Code:
    cout << "What part of don't enter 'q' do you not understand?";

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    8
    You could store function pointers in a std::map, using characters as keys. While this wouldn't validate the num1 oper num2 syntax (Which is impossible, anyway), you could use myFunctionMap[oper](num1, num2).

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    try this:

    Stringy Sums

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by DrkMatter
    You could store function pointers in a std::map, using characters as keys. While this wouldn't validate the num1 oper num2 syntax (Which is impossible, anyway), you could use myFunctionMap[oper](num1, num2).
    Bad idea. At the very least you must first look up the pointer and test if it actually evaluates to something.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 01:14 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 02:02 PM
  5. character string as a variable?
    By Guideon72 in forum C Programming
    Replies: 4
    Last Post: 10-19-2001, 07:59 AM

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