I am trying to concatenate output variables in C In my code there are some calculations and when I am printing the output of the variables I am getting error.
My code:
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
int main()
{
string name;
cout<<"Enter the name of the borrower: ";
cin>>name;
float mortage_blanace;
cout<<"Enter the mortage balance: ";
cin>>mortage_blanace;
float interest_rate;
cout<<"Enter the annual interest rate: ";
cin>>interest_rate;
float current_monthly_payment;
cout<<"Enter the current monthly payment: ";
cin>>current_monthly_payment;
float extra_monthly_payment;
cout<<"Enter the extra monthly payment: ";
cin>>extra_monthly_payment;
cout<<"\n";
int new_payment = current_monthly_payment extra_monthly_payment;
float i = (interest_rate/100)/12;
int current_duration_in_months = (log(current_monthly_payment/(current_monthly_payment/i)-mortage_blanace))/(log(1 i));
int new_duration_in_months = (log(new_payment/(new_payment/i)-mortage_blanace))/(log(1 i));
float current_interest = (current_monthly_payment*new_duration_in_months)-mortage_blanace;
float new_interest = (new_payment*new_duration_in_months)-mortage_blanace;
int current_duration_years = current_duration_in_months/12;
int current_duration_months = current_duration_in_months%12;
int new_duration_years = new_duration_in_months/12;
int new_duration_months = new_duration_in_months%12;
float savings = current_interest-new_interest;
string fees;
cout<<mortage_blanace " " mortage_blanace;
cout<<"\n";
}
return 0;
}
But I am getting following error:
Untitled1.cpp: In function 'int main()':
Untitled1.cpp:58:30: error: invalid operands of types 'float' and 'const char [7]' to binary 'operator '
cout<<mortage_blanace " " mortage_blanace;
How can I get concatenated output in C like the one I want?!
Any help is appreciated.
CodePudding user response:
You can use the insertion operator (<<
) to display values to standard output instead of trying to concatenate a float with an array of characters.
cout << mortage_blanace << " " << mortage_blanace << endl;
CodePudding user response:
You cannot use string concatenation, i.e.
directly for a float
and a std::string
. You can use insertion operator as Tanner Dolby said or if you need to use
you can convert the float
value to std::string
using std::to_string
. You can then use string concatenation.
cout<<to_string(mortage_blanace) " " to_string(mortage_blanace);
cout<<"\n";