Need help with simple program please


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Need help with simple program please

  1. #1
    Join Date
    Mar 2007
    Posts
    4

    Need help with simple program please

    I'm trying to write a simple calculator program and it mostly runs ok except the multiplication and division part of the calculations. It just returns the first number entered when I try and do these. Does anyone have any idea what I have done wrong or need to add to this program?
    Here's the code:


    #include <iostream>

    using namespace std;

    int main()
    {
    float num1;
    float num2;
    float total;

    cout << "Enter an equation i.e.(1+1) : " << endl;
    cin >> num1 >> num2;

    if(num1 + num2)
    {
    total = num1 + num2;
    cout << "The total is " << total << endl;
    }
    else if (num1 - num2)
    {
    total = num1 - num2;
    cout << "The total is " << total << endl;
    }
    else if (num1 * num2)
    {
    total = num1 * num2;
    cout << "The total is " << total << endl;
    }
    else if (num1 / num2)
    {
    total = num1 / num2;
    cout << "The total is " << total << endl;
    }

    return 0;
    }



    Thanks for the help! :)

  2. #2
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Hi,
    by the looks of it I have to wonder why you say that the program works mostly ok. ;-)
    The way you intend to work your program you should read in a string and then parse it for numbers and operators.
    Your cascaded if's actually are equivalent to
    Code:
    if(num1 + num2 != 0)
    {
    total = num1 + num2;
    cout << "The total is " << total << endl;
    }
    else if (num1 - num2 != 0)
    {
    total = num1 - num2;
    cout << "The total is " << total << endl;
    }
    else if (num1 * num2 != 0)
    {
    total = num1 * num2;
    cout << "The total is " << total << endl;
    }
    else if (num1 / num2 != 0)
    {
    total = num1 / num2;
    cout << "The total is " << total << endl;
    }
    which is probably not what you intended.
    Once you extracted your operator from the input string you want to use something like
    Code:
    string mathOperator;
    // ... assign the operator 
    // ...
    if(mathOperator == "+")
    {
       result = num1 + num2;
    }
    else if(mathOperator == "-")
    {
       result = num1 - num2;
    }
    ....
    Hope that get's you on the right track?!

    Cheers,

    D
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  3. #3
    Join Date
    Mar 2007
    Posts
    4
    I have made changes to it and I keep getting these stupid errors. I don't know if there is something weird going on with my visual studio or what but here is the code I have now:

    string mathOperator;
    string +;
    string -;
    string *;
    string /;
    float num1;
    float num2;
    float total;

    cout << "Enter an equation i.e.(1+1) : " << endl;
    cin >> num1 >> mathOperator >> num2;

    if (mathOperator == "+")
    {
    total = num1 + num2;
    cout << "The total is " << total << endl;
    }
    else if (mathOperator == "-")
    {
    total = num1 - num2;
    cout << "The total is " << total << endl;
    }
    else if (mathOperator == "*")
    {
    total = num1 * num2;
    cout << "The total is " << total << endl;
    }
    else if (mathOperator == "/")
    {
    total = num1 / num2;
    cout << "The total is " << total << endl;
    }

    And here is the errors it is giving me.

    error C2143: syntax error : missing ';' before '+'
    1>.\Source4.cpp(9) : error C2059: syntax error : ';'
    1>.\Source4.cpp(10) : error C2143: syntax error : missing ';' before '-'
    1>.\Source4.cpp(10) : error C2059: syntax error : ';'
    1>.\Source4.cpp(11) : error C2059: syntax error : ';'
    1>.\Source4.cpp(12) : error C2143: syntax error : missing ';' before '/'
    1>.\Source4.cpp(12) : error C2143: syntax error : missing ';' before '/'

    The errors are all about the math operators, even if I change them from a string it still gives me errors...any ideas???

  4. #4
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    No mate, there's something weird going on with your code: ;-)
    Code:
    string +; // syntax error: what you probably want is sth like
    const string PLUS = "+";
    string -; // see above
    string *; // see above
    string /; // see above
    BTW: I don't think you need these lines at all.
    D
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  5. #5
    Join Date
    Mar 2007
    Posts
    4
    I didn't have these lines to begin with but someone else mentioned that I need them since the multiplication and division weren't working. I don't understand why the + and - would work and not the * and /. Doesn't make sense to me but as you can probably tell I am pretty new to this.

  6. #6
    Join Date
    Mar 2007
    Posts
    4
    OK I got it working now. Thanks for the help! :) :WAVE:

Similar Threads

  1. Replies: 1
    Last Post: 11-12-2005, 05:37 AM
  2. processing additional data when program is running
    By Ervin Rodriguez in forum Database
    Replies: 1
    Last Post: 05-16-2003, 09:45 AM
  3. Calling Access97 from a VB6 program
    By Jos Roijakkers in forum VB Classic
    Replies: 1
    Last Post: 04-22-2002, 11:29 AM
  4. problem in program in c++
    By mheasen in forum Architecture and Design
    Replies: 0
    Last Post: 03-20-2002, 10:24 AM
  5. Please help with running executable vb program.
    By Gordon Reichhardt in forum VB Classic
    Replies: 2
    Last Post: 01-08-2002, 11:06 AM

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