Varied interest rate calculator - c++

Hey I am new to coding and was wondering if you guys could help me out with calculating different interest rates and then adding them to the next interest rate. So basically I'm trying to get interest rate A and adding it to the starting value of 100. then I want to get interest rate B for 100 and add that value to interest A. So far here is my code but I get 10 lines for each interest rate. Sorry if this sounds confusing but hopefully the code makes it more clear or maybe I could try to explain better if whoever reads this wants to. Thanks!!
int intv;
cout << " Accumulate interest on a savings account. ";
cout << " Starting value is $100 and interest rate is 1.25% ";
cout << endl;
intv = 100;
index = 1;
while ( index <= 10 )
{
cout << " Year " << index << " adds 1.25% for a total of " << .0125 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.27% for a total of " << .0127 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.28% for a total of " << .0128 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.30% for a total of " << .0130 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.31% for a total of " << .0131 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.32% for a total of " << .0132 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.35% for a total of " << .0135 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.36% for a total of " << .0136 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.38% for a total of " << .0138 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.40% for a total of " << .0140 * intv + intv << "." << endl;
index = index + 1;
}
Instead of doing this for me I just want hints. I wanna fix this myself but am stuck as to what I have to do.
The desired out come of this is for the program to give me this:
Year 1 adds 1.25 for a total of 101.25
year 2 adds 1.27 for a total of 102.52
year 3 adds 1.28 for a total of 103.80
year 4 adds 1.30 for a total of 105.09
year 5 adds 1.31 for a total of 106.41
year 6 adds 1.33 for a total of 107.74
year 7 adds 1.35 for a total of 109.09
year 8 adds 1.36 for a total of 110.45
year 9 adds 1.38 for a total of 111.83
year 10 adds 1.40 for a total of 113.23
Total interest credited was 13.23

Sounds like you could use a for loop:
double rate = 0.125;
for (unsigned int index = 0; index < max_rates; ++index)
{
cout << " Year " << index << " adds "
<< (rate * 100.0)
<< "% for a total of "
<< rate * intv + intv << "." << endl;
rate += 0.002;
}

You need use a function to replace
cout << " Year " << index << " adds 1.25% for a total of " << .0125 * intv + intv << "." << endl;
The function can convert the index to adds value like
double foo(int index);
The input value is 'index', the output value is adds value like 1.25%, 1.38% .etc.
Then delete all cout lines. And just add this line:
cout << " Year " << index << " adds " << foo(index) * 100.0 << "% for a total of " << foo(index) * intv + intv << "." << endl;
I guess that is you want.

Related

How can I display a $ sign with a value while using setw and setprecision in c++

I want to display the dollar sign next to its value in the second column, but if I convert the value into a string, the setprecision doesn't work and it displayed more decimals than I would like. Currently the formatting doesn't look good.
My current code:
string unit = "m";
double width = 30.123;
double length = 40.123;
double perimeter = 2 * width + 2 * length;
double area = width * length;
double rate = (area <= 3000) ? 0.03 : 0.02;
double cost = area * rate;
const int COLFMT = 20;
cout << fixed << setprecision(2);
cout << setw(COLFMT) << left << "Length:"
<< setw(COLFMT) << right << length << " " << unit << endl;
cout << setw(COLFMT) << left << "Width:"
<< setw(COLFMT) << right << width << " " << unit << endl;
cout << setw(COLFMT) << left << "Area:"
<< setw(COLFMT) << right << area << " square" << unit << endl;
cout << setw(COLFMT) << left << "Perimeter:"
<< setw(COLFMT) << right << perimeter << " " << unit << endl;
cout << setw(COLFMT) << left << "Rate:"
<< setw(COLFMT) << right << rate << "/sqaure" << unit << endl;
cout << setw(COLFMT) << left << "Cost:"
<< setw(COLFMT) << right << "$" << cost << endl;
Produces this poorly formatted output:
Length: 40.12 m
Width: 30.12 m
Area: 1208.63 square m
Perimeter: 140.49 m
Rate: 0.03/square m
Cost: $36.26
"Currently the formatting doesn't look good."
That's because std::right pertains to what follows it, in your case "$". So the dollar sign is right-aligned and not the value that follows on afterwards.
What you want is the fully formatted monetary value "$36.26" to be right aligned. So build that up as a string first with stringstream.
stringstream ss;
ss << fixed << setprecision(2) << "$" << cost;
cout << left << "Cost:" << setw(COLFMT) << right << ss.str() << endl;

C++ setup columns using cout

So i'm just starting to learn c++ and i'm curious if its a way to formate your output with cout so it will look nicely and structured in columns
for example.
string fname = "testname";
string lname = "123";
double height = 1.6;
string fname2 = "short";
string lname2 = "123";
double height2 = 1.8;
cout << "Name" << setw(30) << "Height[m]" << endl;
cout << fname + " " + lname << right << setw(20) << setprecision(2) << fixed << height << endl;
cout << fname2 + " " + lname2 << right << setw(20) << setprecision(2) << fixed << height2 << endl
The output looks like this:
Name Height[m]
testname 123 1.60
short 123 1.80
I want it to look like this:
Name Height[m]
testname 123 1.60
short 123 1.80
The problem i'm trying to solve is that i want to place height at a specific position from name but depending what length of name i take the value of height either gets far away to the right or will be very close to the left. Is there a way to fix this?
First of all, with an output stream like std::cout, you cannot travel back in time and modify output which was already performed. That makes sense -- just imagine std::cout wrote into a file because you launched your program with program.exe > test.txt, and test.txt was on a USB drive which has been disconnected in the meanwhile...
So you have to get it right immediately.
Basically, there are two ways to do so.
You can assume that no entry in the first column will ever be wider than a certain number of characters, which is what you have attempted. The problem is that your setw is at the wrong position and that right should be left. A stream manipulator must be placed before the elements which should be affected. And since you want left-aligned columns, you need left:
cout << left << setw(20) << "Name" << "Height[m]" << endl;
cout << left << setw(20) << fname + " " + lname << setprecision(2) << fixed << height << endl;
cout << left << setw(20) << fname2 + " " + lname2 << setprecision(2) << fixed << height2 << endl;
But this solution is not very general. What if you'll have a name with 21 characters? Or with 30 characters? Or 100 characters? What you really want is a solution in which the column is automatically set only as wide as necessary.
The only way to do this is to collect all entries before printing them, finding the longest one, setting the column width accordingly and only then print everything.
Here is one possible implementation of this idea:
std::vector<std::string> const first_column_entries
{
"Name",
fname + " " + lname,
fname2 + " " + lname2
};
auto const width_of_longest_entry = std::max_element(std::begin(first_column_entries), std::end(first_column_entries),
[](std::string const& lhs, std::string const& rhs)
{
return lhs.size() < rhs.size();
}
)->size();
// some margin:
auto const column_width = width_of_longest_entry + 3;
std::cout << std::left << std::setw(column_width) << "Name" << "Height[m]" << "\n";
std::cout << std::left << std::setw(column_width) << fname + " " + lname << std::setprecision(2) << std::fixed << height << "\n";
std::cout << std::left << std::setw(column_width) << fname2 + " " + lname2 << std::setprecision(2) << std::fixed << height2 << "\n";
The next step of evolution would be generalising the std::vector into a self-written class called Table and iterating that Table's rows in a loop in order to print the entries.
string fname = "testname";
string lname = "123";
double height = 1.6;
string fname2 = "short";
string lname2 = "123";
double height2 = 1.8;
cout << left << setw(30) << "Name" << left << "Height[m]" << endl;
cout << left << setw(30) << fname + " " + lname << right << setw(6) << setprecision(2) << fixed << height << endl;
cout << left << setw(30) << fname2 + " " + lname2 << right << setw(6) << setprecision(2) << fixed << height2 << endl;

Weird hexadecimal answers to simple math in code::blocks

I'm trying to do the following assignment in a c++ book.
After running this:
#include <iostream>
using namespace std;
int main()
{
double first_arg;
double second_arg;
cout << "Enter first argument: ";
cin >> first_arg;
cout << "Enter second argument: ";
cin >> second_arg;
cout << first_arg << " * " << second_arg << " = "
<< cout << first_arg * second_arg << "\n";
cout << first_arg << " + " << second_arg << " = "
<< cout << first_arg + second_arg << "\n";
cout << first_arg << " / " << second_arg << " = "
<< cout << first_arg / second_arg << "\n";
cout << first_arg << " - " << second_arg << " = "
<< cout << first_arg - second_arg << "\n";
I get some unexpected results. Like this result copied straight from the windows cli:
Enter first argument: 7
Enter second argument: 9
7 * 9 = 0x6fcc43c463
7 + 9 = 0x6fcc43c416
7 / 9 = 0x6fcc43c40
7 - 9 = 0x6fcc43c4-2
I'm using the latest version of codeblocks with the default compiler settings. Thanks.
cout << first_arg << " * " << second_arg << " = "
<< cout << first_arg * second_arg << "\n";
You have two cout in one line since there is no semicolon on line 1
To fix this either get rid of the second cout or add a semicolon at the end of the first line on each cout statement.
If you look at the last 2 digits of each answer you will see the answer you wish to get so its still printing out the answer you want its just after the pointer to cout.

trying to order 3 values from least to greatest C++

I have a program that takes numbers that a person enters and sums it.
This happens 3 times, so I have 3 totals. The problem I am having is that I need to order them from greatest to least no matter what the sums come out to be.(this isnt the full code assume the sums are calculated and are declared)
#include <iostream>
#include <string>
using namespace std;
string firstName1, lastName1; // input and output for the users names
string firstName2, lastName2;
string firstName3, lastName3;
// from greatest to least
if ( sum > sum_2 > sum_3 )
{
cout << "Total for" << " " << firstName1 << " " << lastName1 << " " << "$" << sum << ".00" << endl;
cout << "Total for" << " " << firstName2 << " " << lastName2 << " " << "$" << sum_2 << ".00" << endl;
cout << "Total for" << " " << firstName3 << " " << lastName3 << " " << "$" << sum_3 << ".00" << endl;
}
In c++, the syntax sum > sum_2 > sum_3 won't evaluate as you're assuming. It's equivalent to (sum > sum_2) > sum_3.
In the case where sum is greater than sum_2, sum > sum_2 will evaluate to true. Then, this boolean value will be converted to an integer, 1 and compared with sum_3.
To do what you're trying to accomplish try:
if (sum > sum_2 && sum_2 > sum_3)
Use a helper swap function:
void swap( int *a, int *b )
{
int temp = *a;
*a = *b;
*b = temp;
}
And bubble sort it:
int sums[3] = { sum, sum_2, sum_3 };
for ( int i = 0; i < 3; ++i )
for ( int j = 0; j < i; ++j )
if ( sums[j] < sums[i] )
swap( &sums[j], &sums[i] );
cout << "Total for" << " " << firstName1 << " " << lastName1 << " " << "$" << sums[0] << ".00" << endl;
cout << "Total for" << " " << firstName2 << " " << lastName2 << " " << "$" << sums[1] << ".00" << endl;
cout << "Total for" << " " << firstName3 << " " << lastName3 << " " << "$" << sums[2] << ".00" << endl;

How Would I Read From An External File [duplicate]

This question already has answers here:
Getting input from external files?
(4 answers)
Closed 9 years ago.
Basically, I need a basic .txt -- maybe a .csv -- file that is going to be read by the program and use a number located in the file.
I would want the file to look like this:
oranges: 1.50
apples: 2.10
pears: 4.70
I would then want the program to read 1.50 from the first line and assign that to the variable orangeCost. I would then want the second part to read the second line and assign 2.10 to the variable apples, etc.
This is what I have so far:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
int garden();
//Lawn
int lawnLength;
int lawnWidth;
int lawnTime = 20;
float lawnCost = 15.50;
cout << "Length of lawn required: "; // Asks for the length
cin >> lawnLength; // Writes to variable
cout << "Width of lawn required: "; // Asks for the width
cin >> lawnWidth; // Writes to variable
int lawnArea = (lawnLength * lawnWidth); //Calculates the total area
cout << endl << "Area of lawn required is " << lawnArea << " square meters"; //Prints the total area
cout << endl << "This will cost a total of " << (lawnArea * lawnCost) << " pounds"; //Prints the total cost
cout << endl << "This will take a total of " << (lawnArea * lawnTime) << " minutes" << endl << endl; //Prints total time
//Concrete Patio
int concreteLength;
int concreteWidth;
int concreteTime = 20;
float concreteCost = 20.99;
cout << "Length of concrete required: "; // Asks for the length
cin >> concreteLength; // Writes to variable
cout << "Width of concrete required: "; // Asks for the width
cin >> concreteWidth; // Writes to variable
int concreteArea = (concreteLength * concreteWidth); //Calculates the total area
cout << endl << "Area of concrete required is " << concreteArea << " square meters"; //Prints the total area
cout << endl << "This will cost a total of " << (concreteArea * concreteCost) << " pounds"; //Prints the total cost
cout << endl << "This will take a total of " << (concreteArea * concreteTime) << " minutes" << endl << endl; //Prints total time
//Wooden Deck
int woodenDeckLength;
int woodenDeckWidth;
int woodenDeckTime = 30;
float woodenDeckCost = 15.75;
cout << "Length of wooden deck required: "; // Asks for the length
cin >> woodenDeckLength; // Writes to variable
cout << "Width of wooden deck required: "; // Asks for the width
cin >> woodenDeckWidth; // Writes to variable
int woodenDeckArea = (woodenDeckLength * woodenDeckWidth); //Calculates the total area
cout << endl << "Area of wooden deck required is " << woodenDeckArea << " square meters"; //Prints the total area
cout << endl << "This will cost a total of " << (woodenDeckArea * woodenDeckCost) << " pounds"; //Prints the total cost
cout << endl << "This will take a total of " << (woodenDeckArea * woodenDeckTime) << " minutes" << endl << endl; //Prints total time
//Rectangular Pond
int rectangularPondLength;
int rectangularPondWidth;
int rectangularPondTime = 45;
float rectangularPondCost = 25.00;
cout << "Length of rectangular pond required: "; // Asks for the length
cin >> rectangularPondLength; // Writes to variable
cout << "Width of rectangular pond required: "; // Asks for the width
cin >> rectangularPondWidth; // Writes to variable
int rectangularPondArea = (rectangularPondLength * rectangularPondWidth); //Calculates the total area
cout << endl << "Area of rectangular pond required is " << rectangularPondArea << " square meters"; //Prints the total area
cout << endl << "This will cost a total of " << (rectangularPondArea * rectangularPondCost) << " pounds"; //Prints the total cost
cout << endl << "This will take a total of " << (rectangularPondArea * rectangularPondTime) << " minutes" << endl << endl; //Prints total time
//Water Features
int waterFeatures;
int waterFeaturesTime = 60;
float waterFeaturesCost = 150.00;
cout << "Number of water features required: "; // Asks for the amount of water features needed
cin >> waterFeatures; // Writes to variable
cout << endl << "Number of water feature(s) required is " << waterFeatures << " water feature(s)"; //Prints the total area
cout << endl << "This will cost a total of " << (waterFeatures * waterFeaturesCost) << " pounds"; //Prints the total cost
cout << endl << "This will take a total of " << (waterFeatures * waterFeaturesTime) << " minutes" << endl << endl; //Prints total time
//Garden Lights
int gardenLights;
int gardenLightsTime = 10;
float gardenLightsCosts = 5.00;
cout << "Number of garden lights required: "; // Asks for the amount of water features needed
cin >> gardenLights; // Writes to variable
cout << endl << "Number of garden light(s) required is " << gardenLights << " garden light(s)"; //Prints the total area
cout << endl << "This will cost a total of " << (gardenLights * gardenLightsCosts) << " pounds"; //Prints the total cost
cout << endl << "This will take a total of " << (gardenLights* gardenLightsTime) << " minutes" << endl << endl; //Prints total time
//Quotation
string quotation;
cout << "Do you want to save a quotation? " << endl; //Asks if they want to save a quotation
cin >> quotation; //Saves if they want to save a quotation or not
if (quotation == "yes")
{
std::string nameOfFile;
cout << "What would you like to save the quotation as? (Maybe using your last name) " << endl;
cin >> nameOfFile;
ofstream outfile(nameOfFile + ".txt"); // Opens file in write mode
//Lawn
outfile << "The total lawn area is " << lawnArea << " square meters" << endl; // Writes users data into the file
outfile << "The total cost for the lawn is £" << (lawnArea * lawnCost) << endl; // Writes users data into the file
outfile << "The total time for the lawn is " << (lawnArea * lawnTime) << " minutes" << endl << endl; // Writes users data into the file
//Concrete Patio
outfile << "The total concrete patio area is " << concreteArea << " square meters" << endl;
outfile << "The total cost for the concrete patio is £" << (concreteArea * concreteCost) << endl;
outfile << "The total time for the concrete patio is " << (concreteArea * concreteTime) << " minutes" << endl << endl;
//Wooden Deck
outfile << "The total wooden deck area is " << woodenDeckArea << " square meters" << endl;
outfile << "The total cost for the wooden deck is £" << (woodenDeckArea * woodenDeckCost) << endl;
outfile << "The total time for the wooden deck is " << (woodenDeckArea * woodenDeckTime) << " minutes" << endl << endl;
//Rectangular Pond
outfile << "The total rectangular pond area is " << rectangularPondArea << " square meters" << endl;
outfile << "The total cost for the wooden deck is £" << (rectangularPondArea * rectangularPondCost) << endl;
outfile << "The total time for the wooden deck is " << (rectangularPondArea * rectangularPondTime) << " minutes" << endl << endl;
//Water Features
outfile << "The total number of water feature(s) needed are " << waterFeatures << endl;
outfile << "The total cost for the water feature(s) is £" << (waterFeatures * waterFeaturesCost) << endl;
outfile << "The total time for the water feature(s) is " << (waterFeatures * waterFeaturesTime) << " minutes" << endl << endl;
//Garden Lights
outfile << "The total number of garden light(s) needed are " << gardenLights << endl;
outfile << "The total cost for the garden light(s) is £" << (gardenLights * gardenLightsCosts) << endl;
outfile << "The total time for the garden light(s) is " << (waterFeatures * waterFeaturesTime) << " minutes" << endl << endl;
//CLOSE FILE
outfile.close(); // Close's the file.
cout << "Quotation saved." << endl;
}
else
{
cout << "Quotation not saved." << endl;
}
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
return 0;
}
Thank you for any help! :)
Here's a way of doing it:
Create a class fruit with private variables name and cost.
Then create a constructor that takes a string (a line from the input file). Extract everything up to ':' into name and the value that follows into cost.
Your function that read file needs to do something like this:
declare an array of fruit
loop:
get a line from file into a variable
call the fruit class with this variable, and add the created object into an array
When you reach the end of the file, you have an array of fruits that contains all the data from the file.
to learn about classes, check: http://www.tutorialspoint.com/cplusplus/cpp_classes_objects.htm
to learn about reading from files, check: http://www.cplusplus.com/doc/tutorial/files/