Thread: "simple" math

  1. #1
    Registered User
    Join Date
    Jul 2008
    Location
    Redding, CA
    Posts
    3

    "simple" math

    This has been bugging the hell out of me. I have a tax calculator that I making in the console to make sure I understand how simple math works but I keep getting an error "non-lvalue in assignment" and a warning that I am "converting to 'int' from 'floant'" (yet I don't do know how to convert data types yet). I am using Dev-C++ and this website's tutorials. This is the code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    //variables
    float item_cost;
    float tax_rate;
    float result;
    float tax;
    
    cout<<"Tax Calculator \n";
    cout<<"How much does your item cost:";
    cin>> item_cost;
    cin.ignore();
    cout<<"\n";
    
    cout<<"What is your area's tax rate:";
    cin>> tax_rate;
    cin.ignore();
    cout<<"\n";
    
    cout<<"Calculating... \n";
    if (tax_rate > 0) {
       return tax_rate = tax_rate/100; //error*
       return item_cost * tax_rate = tax; //error**
       return item_cost + tax = result; //error**
       }
    else { 
        return item_cost = result; //error*
        return tax_rate = 0; //error*
        }
        
    cout<<"Report: \n";
    cout<<"Item Cost: "<<item_cost<<"\n";
    cout<<"Tax Rate: "<<tax_rate<<"\n";
    cout<<"Tax: "<<tax<<"\n";
    cout<<"Total: "<<result<<"\n";
    cin.get();
    }
    Error* = converting to int from float
    Error** = non-lvalue in assignment

    I have no clue why it gives me those errors. D:

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Two problems. First, you're using the return statement incorrectly. You use return to exit a function. Your simple program has only a single function, main, so any call to return will exit the program. In the places you are using it you can just remove that word completely. Second, in C++ when you assign a value to a variable, the variable has to be on the left side of the = sign. So for example, if you had a variable named tax, and you wanted to calculate the tax by multiplying the item cost by the tax rate, then you would use tax = item_cost * tax_rate; not the other way around.

    By the way, the default type for floating point numbers (i.e. numbers that can have fractional parts) in C++ is double, not float. I would use double instead of float since it will probably be more accurate.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Location
    Redding, CA
    Posts
    3
    Second, in C++ when you assign a value to a variable, the variable has to be on the left side of the = sign.
    Damn, I don't know how many times I have forgotten that and did it like I was in school (gradeschool).

    Ok, thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math
    By knightjp in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-01-2009, 05:36 PM
  2. Help with C++ Math
    By aonic in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2005, 04:40 AM
  3. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  4. Math Header?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 09-17-2004, 06:39 AM
  5. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM