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;
}