help with caculator progam


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: help with caculator progam

  1. #1
    Join Date
    Jun 2006
    Posts
    69

    help with caculator progam

    I am trying to build a simple caculater program. Everthing works until I try to get program to read the operand + or- or * / want it to display the operand that the user choose. Have not done math part yet until i get this part to work any suggestion would be helpful.

    #include <iostream>
    #include <iomanip>

    using namespace std;

    int main()
    {
    //declared variables
    double num1, num2, num3;
    char ch1, ch2, ch3, ch4;
    ch1 = '+';
    ch2 = '-';
    ch3 = '*';
    ch4 = '/';
    //Prompt user fo numbers
    cout << "Enter two numbers:"; cin >> num1 >> num2; cout << endl << endl;

    cout <<"Enter Operator:"; cout << " " "+" " " "(addition)" " " "-" " "
    <<"(subtraction)" " " "*" " "<< "(multiplication)" " " "/" " " <<"(division):" << endl;

    cin.get (ch1)|| cin.get(ch2)|| cin.get(ch3)|| cin.get(ch4); cout

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    I'm a little confused, why not just cin the operator:
    Code:
    int n1, n2;
    char op;
    cout << "Enter two numbers:" << endl;
    cin >> n1 >> n2;
    cout << "Enter the operator (+,-,*, or /):" << endl;
    cin >> op;
    while( op != '+' && op != '-' && op != '*' && op != '/' ) {
      cout << op << " is not a valid operator, please enter one of +,-,* or /" << endl;
      cin >> op;
    }
    
    // Do processing here ....
    Hope this helps.
    ~evlich

  3. #3
    Join Date
    Jun 2006
    Posts
    69
    yes that help thank you was not sure how to declare the char.

  4. #4
    Join Date
    Jun 2006
    Posts
    69
    OK .tried that did not work keeps giving error
    reviced code.
    #include <iostream>
    #include <iomanip>

    using namespace std;

    int main()
    {
    //declared variables
    double num1, num2, num3;
    char op;

    //Prompt user fo numbers
    cout << "Enter two numbers:"; cin >> num1 >> num2; cout << endl << endl;

    cout <<"Enter Operator:"; cout << " " "+" " " "(addition)" " " "-" " "
    <<"(subtraction)" " " "*" " "<< "(multiplication)" " " "/" " " <<"(division):" << endl;

    cin >> op;
    while( op != '+' && op != '-' && op != '*' && op != '/' ) {
    cout << op << " is not a valid operator, please enter one of +,-,* or /" << endl;
    cin >> op; cout << op << endl;

    If (op = '+') then
    num3 = num1 + num2
    else
    If (op ='-') then
    num3 = num1 - num2
    else
    if (op = '*') then
    num3 =num1 * num2
    else
    if (op = '/') then
    num3 = num1 / num2
    End If
    cin >> num1 >> op >> num2 >> num3 ;
    cout << num1 << op << num2 << num3



    system("Paused");
    return 0;
    }

  5. #5
    Join Date
    Jun 2006
    Posts
    69
    Did not work need it show the operand.

  6. #6
    Join Date
    Aug 2003
    Posts
    313
    It looks like you are missing a closing brace.
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    //declared variables
    double num1, num2, num3;
    char op;
    
    //Prompt user fo numbers 
    cout << "Enter two numbers:"; cin >> num1 >> num2; cout << endl << endl;
    
    cout <<"Enter Operator:"; cout << " " "+" " " "(addition)" " " "-" " "
    <<"(subtraction)" " " "*" " "<< "(multiplication)" " " "/" " " <<"(division):" << endl;
    
    cin >> op;
    while( op != '+' && op != '-' && op != '*' && op != '/' ) {
    cout << op << " is not a valid operator, please enter one of +,-,* or /" << endl;
    cin >> op;
    } // Add teh ending of the while loop
    
    // All of these = need to be == since you are compareing
    // This syntax isn't C++, if( op == '+' ) { ... } else { ... }
    If (op = '+') then
    num3 = num1 + num2 
    else 
    If (op ='-') then
    num3 = num1 - num2
    else
    if (op = '*') then
    num3 =num1 * num2 
    else
    if (op = '/') then
    num3 = num1 / num2
    End If
    //cin >> num1 >> op >> num2 >> num3 ; // REMOVE
    cout << num1 << op << num2 << num3 
    
    
    
    system("Paused");
    return 0; 
    }
    Hope this helps.
    ~evlich

  7. #7
    Join Date
    Jun 2006
    Posts
    69
    still getting error on if statment( if undeclared first use of this function and something expected before then and error on blank line above If statement.

  8. #8
    Join Date
    Jun 2006
    Posts
    69
    now it is telling me expected primaraay expression before == and something missing before then

  9. #9
    Join Date
    Jun 2006
    Posts
    69
    It' s all working except the if ststement gives me the following error line.In function `int main()':
    expected `;' before "If"
    [Build Error] Error 1

  10. #10
    Join Date
    Aug 2003
    Posts
    313
    If should be lower case:
    Code:
    if( op == '+' ) {
      num3 = num1 + num2;
    } else if( op == '-' ) {
      num3 = num1 - num2;
    } ...
    That should work.
    ~evlich

  11. #11
    Join Date
    Jun 2006
    Posts
    69
    It is in lower case but still gives me
    In function `int main()':
    expected `;' before "If"

  12. #12
    Join Date
    Jun 2006
    Posts
    69
    I have got it read the if statement but it is non caculating the numbers any suggestions.
    reviced code
    #include <iostream>
    #include <iomanip>

    using namespace std;

    int main()
    {
    //declared variables
    double num1, num2, result;
    char op;

    //Prompt user fo numbers
    cout << "Enter two numbers:"; cin >> num1 >> num2; cout << endl << endl;
    //Prompt user for operand
    cout <<"Enter Operator:"; cout << " " "+" " " "(addition)," " " "-" " "
    <<"(subtraction)," " " "*" " "<< "(multiplication)," " " "/" " " <<"(division):" << endl;
    cin >> op;
    {
    if ( op != '+' && op != '-' && op != '*' && op != '/' ) // program reads operand
    cout << op << endl;
    }
    cin >> op;

    if (op == '+')
    result = num1 + num2;
    else if (op == '-')
    result = num1 - num2;
    else if (op == '*')
    result = num1 * num2;
    else if (op == '/')
    result = num1 / num2;
    return result;




    system("Paused");
    return 0;

    }

  13. #13
    Join Date
    Aug 2003
    Posts
    313
    You are returning the number instead of printing it out. I'm assuming that you want to print the number, so at the bottom replace:
    return result;
    with
    cout << result;

    Hope this helps.
    ~evlich

  14. #14
    Join Date
    Jun 2006
    Posts
    69
    I have tried that what I want it do do is caculate results based on users choise then show (example )14.45 / 75.23 =.019

  15. #15
    Join Date
    Jun 2006
    Posts
    69
    Can Anyone tell me why it does not show results.
    #include <iostream>
    #include <iomanip>

    using namespace std;

    int main()
    {
    //declared variables
    double num1, num2, result;
    char op;

    //Prompt user fo numbers
    cout << "Enter two numbers:"; cin >> num1 >> num2; cout << endl << endl;
    //Prompt user for operand
    cout <<"Enter Operator:"; cout << " " "+" " " "(addition)," " " "-" " "
    <<"(subtraction)," " " "*" " "<< "(multiplication)," " " "/" " " <<"(division):" << endl;
    cin >> op;
    {
    if ( op != '+' && op != '-' && op != '*' && op != '/' ) // program reads operand
    cout << op << endl;
    }
    cin >> op;
    if (op == '+')
    result = num1 + num2;
    else if (op == '-')
    result = num1 - num2;
    else if (op == '*')
    result = num1 * num2;
    else if (op == '/')
    result = num1 / num2;
    else

    cout << num1 << op << num2 << result;



    system("Paused");
    return 0;

    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center