Results 1 to 17 of 17

Thread: C++ Help     submit to reddit submit to twitter

  1. #1
    Relic Shield
    Join Date
    Nov 2006
    Posts
    1,778
    BG Level
    6

    C++ Help

    I'm teaching myself C++ and have run into a problem in the basics. I'm trying to create a basic calculator that I can specify the first number then the second number then the type of math.

    Here is my code for reference:
    Code:
    //
    // Mike's super awesome calculator
    //
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    int main(int nNumberofArgs, char* pszArgs[])
    {
        // Enter first number
        int first;
        cout << "Enter the first number:";
        cin >> first;
        
        // Enter second number
        int second;
        cout << "Enter the second number:";
        cin >> second;
        
        // Enter type
        int type;
        cout << "Enter the type:";
        cin >> type;
        
        // Calculate answer
        int answer;
        answer = first type second;
        
        // Display answer
        cout << "The answer is:";
        cout << answer << endl;
        
        // Wait for user
        system ("Pause");
        return 0;
    }
    Well I'm getting a compile error on this like:
    Code:
        answer = first type second;
    Can anyone help me with this?

    Move to tech if needed.

  2. #2
    blax n gunz
    Join Date
    May 2005
    Posts
    11,141
    BG Level
    9

    you're not properly applying a function to your first or second integer variables. You can't just store an integer for the 'type' of operation you wish to apply and expect the compiler to read your mind and say 'oh by the way that integer is really a mathematical operator.'

    You have to create some logic that interprets the user input for the operation and funnels it to the correct kind of function. In C it would have been a switch statement.

  3. #3
    Vic
    Vic is offline
    CoP Dynamis
    Join Date
    May 2006
    Posts
    251
    BG Level
    4

    You need to create a function that would execute the calculation based on the value of the input for "type". For example, if you want to stick with a datatype of int for type, you can assign a value for which operator to use depending on the value of "type".

    ex. in a if else statement:
    if(type == 1)
    {
    answer == first * second;
    }
    else if(type == 2)
    {
    answer == first / second;
    }
    etc...

    or a switch statement

    switch(type)
    {
    case 1:
    {
    answer == first * second;
    break;
    }
    etc...
    }

    The same can be applied for any kind of datatype, so if you wish for the user to input a string of what mathematical operation they want to be executed, for instance "add" or "multiply", then replace ints with string.

    Generally, when the state of your program changes based off of the user's input during runtime, using if else statements or switch statements is required.

  4. #4
    Relic Horn
    Join Date
    Oct 2005
    Posts
    3,132
    BG Level
    7
    FFXI Server
    Unicorn
    WoW Realm
    Shattered Hand

    Kudos on making your calculator reverse polish notation. Thinking like that will help you a lot when you get much deeper into CS. I think your question got answered fairly well otherwise.

  5. #5
    Bagel
    Join Date
    Jan 2006
    Posts
    1,428
    BG Level
    6

    I'd personally say, from the user's perspective, don't have them input integers for the type of operation. Have them input a char instead, and do an if/else block or switch statement using the inputted char.


    One tip for dealing with compile errors, is try to think like the computer when debugging. The computer is seeing the statement as (int assignment int int int) which doesn't really make too much sense when you think about it. Always need some sort of operator or punctuation statement between each variable/identifier for a statement to make sense.

    I could go into a nice tirade about how learning BNF and parse trees could be beneficial, but that's for much later

  6. #6
    Relic Shield
    Join Date
    Nov 2006
    Posts
    1,778
    BG Level
    6

    I don't really understand what dezzimal is saying 0-0 or if s/he is even talking to me... (I'm a bit sleepy and reading this for dummies book on it) I'm moving on from the basics into chap 2. I think what you guys are talking about is over my head right now.

  7. #7
    blax n gunz
    Join Date
    May 2005
    Posts
    11,141
    BG Level
    9

    I hope you at least understand why your code isn't compiling.

    But Reverse Polish Notation is on wikipedia if you want to know what it is.

  8. #8
    Nidhogg
    Join Date
    Feb 2006
    Posts
    3,901
    BG Level
    7
    FFXI Server
    Carbuncle

    Took me a second to figure out what you were trying to do lol. But ya what Correction said is 100% right and it made me lol.

    It would be like this:

    Code:
    //
    // Mike's super awesome calculator
    //
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    int main(int nNumberofArgs, char* pszArgs[])
    {
        // Enter first number
        int first;
        cout << "Enter the first number:";
        cin >> first;
        
        // Enter second number
        int second;
        cout << "Enter the second number:";
        cin >> second;
        
        // Enter type
        char type;
        cout << "Enter the type:";
        cin >> type;
        
        int answer;
        if(type == '+')
        {
          // Calculate answer
          answer = first + second;
        }
        else if(type == '-')
        {
           // Stuff for minus
        }
        // So on so forth
    
        
        // Display answer
        cout << "The answer is:";
        cout << answer << endl;
        
        // Wait for user
        system ("Pause");
        return 0;
    }
    What Vic posted would also work.

    And yes you really need to know why what you had doesn't compile before you move onto anything else.

  9. #9
    Relic Shield
    Join Date
    Nov 2006
    Posts
    1,778
    BG Level
    6

    Well from what I currently understand is int cant hold a symbol such as + - etc. If you do try to "make" it hold a symbol it turns the symbol into a number. The other part I kind of sort of don't understand... What I was thinking would happen is since something is stored in type it would be like:

    first = 60
    type = +
    second = 30
    answer = 90

    But that's how it works in my head.

    Ah I see what RPN is now... Heh. I was just setting it up that way because I knew how int worked but not how type would work if it didn't work with int.

    Also would it be smart to use double instead of int so you can use decimals?

  10. #10
    Nidhogg
    Join Date
    Feb 2006
    Posts
    3,901
    BG Level
    7
    FFXI Server
    Carbuncle

    The thing is that the integers are built in types. So the compiler knows what they are and how to use them already technically. Like was said before you can't just type the plus or minus symbol and expect the program to figure out what you want to do with it automatically. You have to explicitly show it what to do.

  11. #11
    Relic Shield
    Join Date
    Nov 2006
    Posts
    1,778
    BG Level
    6

    Quote Originally Posted by Aerin View Post
    The thing is that the integers are built in types. So the compiler knows what they are and how to use them already technically. Like was said before you can't just type the plus or minus symbol and expect the program to figure out what you want to do with it automatically. You have to explicitly show it what to do.
    Ah gotcha. If and Elseif seem to be the way to go as I learned a bit about them when shell scripting in my Linux+ class. Anyone on the matter of using double instead of int?

  12. #12
    Bagel
    Join Date
    Jan 2006
    Posts
    1,428
    BG Level
    6

    make it work first using just ints.

    You still need to fix the problem with trying to put an operator value into an integer. Store type as a 'char' instead of an 'int' and that will solve some problems. This will instead store a single character representation of what the user inputted. (Easiest basic storage types in c++: int, char, string, double)

    From there, go into an if/else block or switch statement to determine which operation to do on the two numbers, do it, and then you return it.


    edit: Yea, what aerin did.

  13. #13
    blax n gunz
    Join Date
    May 2005
    Posts
    11,141
    BG Level
    9

    Feel free to use double, it makes more sense than having 2/3 return 0 from an int-only calculator. Eventually you'll learn to format output so you don't always end up with 13+ significant digits of precision getting printed out.

  14. #14
    Relic Shield
    Join Date
    Nov 2006
    Posts
    1,778
    BG Level
    6

    I thank you guys for all of the help. Here is my finished code if anyone wants to take a peak at it.

    Code:
    //
    // Mike's super awesome calculator
    //
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    int main(int nNumberofArgs, char* pszArgs[])
    {
        //Display Program
        cout << "Welcome to Mike's super awesome calculator \n";
        
        // Enter first number
        double first;
        cout << "Enter the first number: ";
        cin >> first;
        
        // Enter type
        char type;
        cout << "Enter the type: ";
        cin >> type;
     
        // Enter second number
        double second;
        cout << "Enter the second number: ";
        cin >> second;
           
        // Calculate answer
        double answer;
        if(type == '+')
        {
                answer = first + second;
        }
        else if(type == '-')
        {
             answer = first - second;
        }
        else if(type == '*')
        {
             answer = first * second;
        }
        else if(type == '/')
        {
             answer = first / second;
        }
        
        // Display answer
        cout << "The answer is: ";
        cout << answer << endl;
        
        // Wait for user
        system ("Pause");
        return 0;
    }

  15. #15
    Banned.

    Join Date
    Aug 2007
    Posts
    2,547
    BG Level
    7

    Looks good, minus basic input error catching like division by 0.

  16. #16
    Nikkei's Hoe
    Worse than her at uno

    Join Date
    Dec 2006
    Posts
    6,236
    BG Level
    8
    FFXIV Character
    Eanae Hikari
    FFXIV Server
    Gilgamesh
    FFXI Server
    Cerberus
    WoW Realm
    Hyjal

    And doesn't have a case to deal with any characters that aren't + - * or /

  17. #17
    blax n gunz
    Join Date
    May 2005
    Posts
    11,141
    BG Level
    9

    I'm guessing that input validation will be covered in a later chapter of his book.

Similar Threads

  1. WAR/NIN help plz
    By cyphx in forum General Discussion
    Replies: 18
    Last Post: 2006-04-28, 14:11
  2. Ouch, Call for help on Cassie{/comfort}
    By Deadkennedys in forum General Discussion
    Replies: 3
    Last Post: 2004-09-27, 07:48
  3. Need help on money making methods
    By in forum General Discussion
    Replies: 2
    Last Post: 2004-09-16, 21:57