-3

I got the error stating invalid operands of the binary operator, but all the data float, so how to solve this issue? The error was found in the function of the monthly installment calculation. Why does it occur even though both are float numbers? The operand was used to calculate the total installment and the monthly installment that needs to be paid by the user for the selected car brand that is chosen by the user itself.

#include <iostream>
#include <iomanip>
using namespace std;

//Functions
void showWelcome();
void showMenu();
void showBrand_L(int);
void showBrand_I(int);
void getPrice_L(float);
void getPrice_I(float);
float getLoan(float);
float showMonthly1(float, float, float, float, float);
float showMonthly2(float, float, float, float, float);

int main ()
{
    int choice;  //Menu choice
    int Brand1,Brand2;
    float Monthly_installment,Interest,Total_interest,Loan, car1,car2,Price1,Price2,PriceL,PriceI;
    
    //Constants for Menu choice
    const int Local=1, International=2, Quit=3;
    
    do
    {
        showWelcome(); // Show Welcome screen
        showMenu(); // Display Menu
        cin >> choice;
         
        //Validate menu selection
        while (choice < Local || choice > Quit)
        {
            cout << "Please enter a valid menu choice: ";
            cin >> choice;
        }
        
        //If the user doesn't want to quit, proceed;
        if (choice != Quit)
        {
            switch (choice)
            {
                case Local:
                getPrice_L(PriceL);
                showBrand_L(Brand1);
                getLoan(Loan);
                showMonthly1(Price1,Loan,Monthly_installment,Interest,Total_interest);
                break;
                
                case International:
                getPrice_I(PriceI);
                showBrand_I(Brand2);
                getLoan(Loan);
                showMonthly2(Price2,Loan,Monthly_installment,Interest,Total_interest);
                break;
            }
        }
    }
    while (choice != Quit);
    return 0;
}
//Welcome Function
void showWelcome()
{
    cout << "Welcome to the Car Loan Calculator" << endl << endl;
}
//Menu Function
void showMenu ()
{
    cout << "Please choose the car nationality that you desired" << endl << endl
    << "1. Local" << endl
    << "2. International" << endl
    << "3. Quit" << endl << endl;
}

//Price Function for Local Car
void getPrice_L(float PriceL)
{
    //Storing the car Price
   float Price1[2] = {93200,73000};
}

//Price function for International car
void getPrice_I(float PriceI)
{
    //Storing the car Price2
    float Price2[5] = {139913,282540,123000,268888,146938};
}

//Display the choice of the Local Brand that is offered
void showBrand_L(int Brand1)
{
    cout<<"\n++++++++++++++++++++++++";
    cout<<"\n+       LOCAL          +";
    cout<<"\n++++++++++++++++++++++++";
    cout<<"\n+    1.PROTON          +";
    cout<<"\n+    2.PERODUA         +";
    cout<<"\n++++++++++++++++++++++++";
    cin>>Brand1;
    
    float Price1[2] = {93200,73000};
    
    
    switch (Brand1)
    {
        case 1:
        cout<<"The car you choose is Proton"<<endl;
        cout<<"The price of the car is RM "<<Price1[0]<<endl;
        
        break;
        case 2:
        cout<<"The car you choose is Perodua"<<endl;
        cout<<"The price of the car is RM "<<Price1[1]<<endl;
        
        break;
    }
}

//Display the choice of the International Brand that are offered
void showBrand_I(int Brand2)
{
    cout<<"\n++++++++++++++++++++++++";
    cout<<"\n+    INTERNATIONAL     +";
    cout<<"\n++++++++++++++++++++++++";
    cout<<"\n+     1.HONDA          +";
    cout<<"\n+     2.VOLVO          +";
    cout<<"\n+     3.TOYOTA         +";
    cout<<"\n+     4.FORD           +";
    cout<<"\n+     5.ISUZU          +";
    cout<<"\n++++++++++++++++++++++++";
    cin>>Brand2;
    
    float Price2[5] = {139913,282540,123000,268888,146938};

    
    switch (Brand2)
    {
        case 1:
        cout<<"The car you choose is Honda"<<endl;
        cout<<"The price of the car is RM "<<Price2[0]<<endl;
        
        break;
        case 2:
        cout<<"The car you choose is Volvo"<<endl;
        cout<<"The price of the car is RM "<<Price2[1]<<endl;
        
        break;
        case 3:
        cout<<"The car you choose is Toyota"<<endl;
        cout<<"The price of the car is RM "<<Price2[2]<<endl;
        
        break;
        case 4:
        cout<<"The car you choose is Ford"<<endl;
        cout<<"The price of the car is RM "<<Price2[3]<<endl;
        
        break;
        case 5:
        cout<<"The car you choose is Isuzu"<<endl;
        cout<<"The price of the car is RM "<<Price2[4]<<endl;
        
        break;
    }
}

//Function for Loan
float getLoan(float Loan)
{
    cout<<"How many years do you want to take the loan for?\n";
    cin>>Loan;
    return Loan;
}

//Function to calculate the monthly installment for local car
float showMonthly1(float Price1[],float Loan,float Monthly_installment,float Interest, float Total_interest)
{
    cout<<"The interest for the car is 3.5 %\n";
    Interest=3.5;
  
    Total_interest= (Interest/100) * (Price1) * (Loan);
    Monthly_installment= (Loan+Total_interest)/(Loan*12);
  
    cout<<"The total interest need to be paid is RM "<< Total_interest<<endl;
    cout<<"Your monthly installment will be total of RM "<< Monthly_installment<<endl; 
   
}

//Function to calculate the monthly installment for international car
float showMonthly2(float Price2[],float Loan,float Monthly_installment,float Interest, float Total_interest)
{
    cout<<"The interest for the car is 3.5 %\n";
    Interest=3.5;
    
    
    Total_interest=(Interest/100)*(Price2)*(Loan);
    Monthly_installment= (Loan+Total_interest)/(Loan*12);
  
    cout<<"The total interest need to be paid is RM "<< Total_interest<<endl;
    cout<<"Your monthly installment will be total of RM "<< Monthly_installment<<endl;
    
}
  • The error clearly says that you're trying to multiply a `float` and a `float*` In particular `Price1` is of type `float*`. – Jason Jul 16 '22 at 06:46
  • You have added the array operator `[]` to your `Price1` and `Price2` arguments. Why? – Adrian Mole Jul 16 '22 at 06:48
  • Is it because Price1 is and array ? – Iffah Nasuha Jul 16 '22 at 06:51
  • @AdrianMole that is because I want to take the value of the car from the array in the above statements. Can't I do that – Iffah Nasuha Jul 16 '22 at 06:52
  • That's because when I put just Price1 and Price2, it did not call the value of the car – Iffah Nasuha Jul 16 '22 at 06:52
  • @IffahNasuha You can dereference `Price1` and `Price2` before multiplying them with another `float` so that you'll no longer get this error. Also refer to a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) as this is very basic and is explained in every beginner level C++ book. – Jason Jul 16 '22 at 06:58
  • @IffahNasuha There are plenty of dupes for "how to pass array to function in c++". Refer to [how to ask](https://stackoverflow.com/help/how-to-ask). – Jason Jul 16 '22 at 07:01
  • 1
    Tldr use std::array or std::vector and pass that like any other class.dont use raw pointers – Pepijn Kramer Jul 16 '22 at 07:07

1 Answers1

-1

Your functions are backwards

//Price Function for Local Car
void getPrice_L(float PriceL)
{
    //Storing the car Price
    float Price1[2] = {93200,73000};
}

This function (apparently) gets a price, so the return type should be float and there should be no parameter. Something like this

float getPrice_L()
{
    //Storing the car Price
    float Price1[2] = {93200,73000};
    return Price1[0];
}

Then in main you do this

PriceL = getPrice_L();

You pass a parameter to a function when you want that function to use that parameter. You use a return value in a function when you want that function to return a value. Generally speaking functions which are called getSomething should be returning values.

Same issue with showBrand_L, this should return the brand, not have the brand as a parameter. Like this

//Display the choice of the Local Brand that is offered
int showBrand_L()
{
    cout<<"\n++++++++++++++++++++++++";
    cout<<"\n+       LOCAL          +";
    cout<<"\n++++++++++++++++++++++++";
    cout<<"\n+    1.PROTON          +";
    cout<<"\n+    2.PERODUA         +";
    cout<<"\n++++++++++++++++++++++++";
    int Brand1;
    cin>>Brand1;
    
    float Price1[2] = {93200,73000};
    
    switch (Brand1)
    {
        case 1:
        cout<<"The car you choose is Proton"<<endl;
        cout<<"The price of the car is RM "<<Price1[0]<<endl;
        
        break;
        case 2:
        cout<<"The car you choose is Perodua"<<endl;
        cout<<"The price of the car is RM "<<Price1[1]<<endl;
        
        break;
    }
    return Brand1;
}

then in main

Brand1 = showBrand_L();

There are I think lots of issues with your code, but the first task is to understand the difference between passing a parameter to a function, and returning a value from a function. And to design your functions so that they choose the correct option. At the moment many of your functions return nothing, even though that's clearly what they should be doing.

john
  • 85,011
  • 4
  • 57
  • 81