Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    C++ four function calculator

    I'm trying to create a four-function calculator with parameter passing, but i'm getting errors. Please help!

    //////COMPILER DIRECTIVES
    #include <iostream> //provides cin and cout
    #include <cstdlib> //provides exit_success
    #include <cmath>
    using namespace std; //allows all standard library to be used


    /////////FUNCTION PROTOTYPES
    void print_headings(); // print table headings
    float compute ( float&, float&, char);


    ////////////DRIVER FUNCTION

    main()
    {
    print_headings(); //Print table heading

    int result; // the result of the calculations
    char oper_num; // the user-specified operator
    int value; // value specified after the operator

    result = 0; // initialize the result
    // loop until the 'q' command is reached
    while (1)
    { //loop starts with 1
    cout << "Result: " << result << '\n';
    cout << "Enter operator and number: "; //user is prompted for input

    cin >> oper_num;

    if ((oper_num == 'q') || (oper_num == 'Q')) //quit program when 'q' is
    //reached
    break;

    cin >> value;
    compute (Result, value, oper_num);
    cin >> oper_num;
    }
    return (0);

    } //end main()

    ///////////HELPER FUNCTION IMPLEMENTATIONS

    void print_headings()
    //Prints table headings to standard output
    //Precondition: table constants have been pre-established
    //Postcondition: table header written to standard out, in preparation for values
    //library Utilities: iostream
    {
    //Print intro statement and headings
    cout << endl;
    cout << "*************************************************************" << endl;
    cout << " calculator.cc " << endl;
    cout << " This is a four-function calculator that add, " << endl;
    cout << " subtracts, multiplies,and divides simple integers, " << endl;
    cout << " and maintains an ongoing accumulated result (starting with 0) " << endl;
    cout << " Program by Debbie_lee104 " << endl;
    cout << " Jul. 13, 2007 " << "\n\n";
    cout << "**************************************************************" << endl;
    cout << endl;

    } //End print_headings()


    float compute ( float &Result, float &value, char oper_num)
    {
    switch (oper_num)
    {

    if (oper_num == '+') //perform addition when the operator is a plus(+)
    {
    result += value; //value specified after the operation
    }
    else if (oper_num == '-')
    {
    result -= value;
    }
    else if (oper_num == '*')
    {
    result *= value;
    }
    else if (oper_num == '/') {
    if (value == 0) {
    cout << "Error: Attempt to Divide by zero\n";
    } else
    result /= value;
    } else {
    cout << "Error:'x' is not a valid operator\n";
    }
    }
    }

    /////////END OF PROGRAM FILE//////////////////
    Last edited by debbie_lee104; 07-16-2007 at 03:04 AM.

  • #2
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    It would have been nice if you'd specified what errors you were getting. It makes it much easier for us to help you.

    I had to change the following in order for your program to compile:

    In main:
    int result; AND int value;
    TO
    float result; AND float value;
    Your compute function expects float values to be passed to it, not ints, thus your variables should be declared as floats.

    In main:
    compute (Result, value, oper_num);
    TO
    compute(result,value,oper_num);
    C++ is Case-Sensitive. Your variable is declared as result NOT Result.

    In compute:
    float compute ( float &Result, float &value, char oper_num)
    TO
    float compute ( float &result, float &value, char oper_num)
    Same reason as above.


    Your switch in your compute function wasn't written properly and wasn't doing what you expected. If you use a switch you shouldn't need the if-statements. Use one of the two options below,

    If-Statements
    Code:
    float compute ( float &result, float &value, char oper_num)
    {
    
    if(oper_num == '+')
    {
    result += value; //value specified after the operation
    }
    else if (oper_num == '-')
    {
    result -= value;
    }
    else if (oper_num == '*')
    {
    result *= value;
    }
    else if (oper_num == '/') {
    if (value == 0) {
    cout << "Error: Attempt to Divide by zero\n";
    } else
    result /= value;
    } else {
    cout << "Error:'x' is not a valid operator\n";
    }
    }
    Switch Statement
    Code:
    float compute ( float &result, float &value, char oper_num)
    {
    switch(oper_num)
    {
      case '+':
            result += value; //value specified after the operation
            break;
      
      case '-':
            result -= value;
            break;
    
      case '*':
            result *= value;
            break;
    
      case '/':
            if (value == 0) {
                    cout << "Error: Attempt to Divide by zero\n";
            }
            else
                    result /= value;
            break;
      default:
            cout << "Error:'x' is not a valid operator\n";
            break;
    }
    }
    You have some other logic issues in your code that can cause your program to enter an infinite-loop. I'll let you work on that.
    Last edited by Gox; 07-16-2007 at 06:29 AM.

  • #3
    New Coder
    Join Date
    Jun 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks a bunch! I'll work on that.

    D.

  • #4
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,063
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Quote Originally Posted by debbie_lee104 View Post
    Code:
    /////////FUNCTION PROTOTYPES
    float compute ( float&, float&, char);
    While this will compile, it is generally a good idea to put your function variable declarations in the prototype area as well (It helps you (and anyone else following you) to know which variable is used and which spot they are used in - i.e. -
    Code:
    /////////FUNCTION PROTOTYPES
    float compute (float &Result, float &value, char oper_num);
    Starting this in practice now will help immensely in the future.

    There also appears to be a logic error in your application (unless I am misunderstanding the assignment). Here you present this to the user:
    Quote Originally Posted by debbie_lee104 View Post
    Code:
       cout << " calculator.cc " << endl;
       cout << " This is a four-function calculator that add, " << endl; 
       cout << " subtracts, multiplies,and divides simple integers, " << endl; 
       cout << " and maintains an ongoing accumulated result (starting with 0) " << endl;
    But your program only allows for the entry of one variable instead of two (integers instead of integer).

    Other than that, looks good so far.

    P.S. --> Gox is correct about your loop issue.

    -sage-
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000

  • #5
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,063
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Now just a little clarafication on variables and their use in functions.
    Code:
    //////COMPILER DIRECTIVES
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    using namespace std;
    
    /////////FUNCTION PROTOTYPES
    float AddMe(int A, int B, int C);
    
    
    ////////////DRIVER FUNCTION
    main()
    { 
    	int Z = 10;
     	int Y = 20; 
    	int X = 30; 
    
    	cout << "Value 1 = " << Z << '\n';
    	cout << "Value 2 = " << Y << '\n';
    	cout << "Value 3 = " << X << '\n';
    	cout << "Add them together and you get [" << AddMe(Z, Y, X) << "]"
    	cout << endl;
    	return (0);
    }
    
    ///////////HELPER FUNCTION IMPLEMENTATIONS
    int AddMe(int A, int B, int C)
    {
    	return (A + B + C);
    }
    When thinking of functions and their parameters, you do not have to use the same name as the function parameter itself. As you can see here, I have three integer variables that are instansiated as Z, Y and X respectively. The AddMe function allows for three integer variables to be pass (By value) as parameters. The scope of the AddMe variables (A, B and C) exists within the AddMe function and not within the main function.

    I hope this helps to clarify a few things.

    -sage-
    Last edited by sage45; 07-16-2007 at 08:58 PM.
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000

  • #6
    New Coder
    Join Date
    Jun 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you all- I'm new to this and I appreciate all your comments. My program is working great!

    Now i'm trying to create a makefile with the following target: calc, p2_calculator.o, and clean.

    I'm trying to follow some instructions from my google search but i'm not sure what i'm doing.
    I made a : make p2_calculator and the result was "p2_calculator is up to date". Is this correct because i don't see any new file being created?

    Thanks,
    debbie_lee104
    Last edited by debbie_lee104; 07-17-2007 at 02:45 AM.

  • #7
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    The make file's purpose is to compile your code. If your code has already been compiled in the past you'll have a file called, say 'calculator'. When you call make it'll just overwrite this executable and won't actually create a new file.

    Now, if your program has been compiled already and you call make to compile it again without changing any of the source files you will indeed get the message "..is up to date". Which means you have not changed any code since the last time you compiled.

    On Unix (I don't think it'll work in dos) if you wish to force make to compile anyways you can try typing 'touch *' at the command prompt. This will update the timestamp on your files to the current time, making it look like they've been changed.

    I'm not sure that was clearly explained, let us know if you need further clarification.

  • #8
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    The 'clean' target in your makefile should delete all the products of previous compilations so that you can force a recompile by running 'make clean' and then 'make whatever_other_target'

    eg:
    Code:
    clean:
    	-rm -f *.o *~ <other specific names or paths you told gcc to output to, in your case likely just 'calc'>
    (prefixing a command with - in a makefile means a failure for that line will not cause make to report failure of the whole process)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •