I'm new to C++ and development in general. Frankly, I have no idea what is going on. I'm just trying to display a string on one line, but the program is giving me a confusing error.
I would really appreciate any help.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// This program calculates and displays to user
int main()
{
// Constants are state and county taxes.
const float STATE_TAX_RATE = 0.04,
COUNTY_TAX_RATE = 0.02;
// float variables are :
float gross_sales = 0,
net_sales = 0,
county_tax_payment = 0,
state_tax_payment = 0,
total_tax_payment = 0;
// string variable
string month;
// integer variable
int year;
// Get month, year, and sales information from user
cout << "For what month is this? (Please type the name of the month.)\nAnswer: ";
getline(cin, month);
cout << "For what year?\nAnswer: ";
cin >> year;
cout << "How much was total sales at the register?\nAnswer: ";
cin >> gross_sales;
// Calculate the net income
net_sales = (gross_sales)/(1 + STATE_TAX_RATE + COUNTY_TAX_RATE);
// Calculate total taxes paid.
total_tax_payment = (gross_sales - net_sales);
// cout << total_tax_payment; // output test
// Calculate total state taxes paid.
state_tax_payment = (total_tax_payment * (2.0/3.0));
// cout << state_tax_payment; //output test
// Calculate county taxes paid.
county_tax_payment = (total_tax_payment * (1.0/3.0));
//Display the information
cout << "Month: " << month << " " << year << endl;
cout << "--------------------" << endl;
cout << "Total collected:\t $" << fixed << setw(9) << setprecision(2) << right << gross_sales << endl;
cout << "Sales: \t\t\t\t $" << fixed << setw(9) << setprecision(2) << right << net_sales << endl;
cout << "County Sales Tax:\t $" << fixed << setw(9) << setprecision(2) << right << county_tax_payment << endl;
cout << "State Sales Tax:\t $" << fixed << setw(9) << setprecision(2) << right << state_tax_payment << endl;
cout << "Total Sales Tax:\t $" << fixed << setw(9) << setprecision(2) << right << total_tax_payment << endl;
return 0;
}
The output looks like this:
For what month is this? (Please type the name of the month.)
Answer: March
For what year?
Answer: 2008
How much was total sales at the register?
Answer: 26572.89
(lldb)
At "(lldb)" The program just stops... and Xcode indicates something I don't understand on "cout << "Month: " << month << " " << year << end;", telling where an issue is, then a lot of complex debugging info. The indicator is green colored.
Thanks again for any help!!!
Because state_tax_payment and total_tax_payment are not initialize state_tax_payment = net_sales / state_tax_payment; and county_tax_payment = net_sales / county_tax_payment; lines can be result in undefined behavior
Initialize the all float variables
Assign some value to state_tax_payment and total_tax_payment
Correct the type mention by ' Thomas Matthews'.
Then your program works fine . May be it exit after execution finish. so you can add something like 'getchar()' , std::cin.get() to pause the console.
The actual problem was identified by Tony D.
The debugger in Xcode had a breakpoint set to the particular line of code. I simply had to drag it out of the gutter. For those who don't know, that the green arrow on the left in of the lines of code is a breakpoint. Drag it to the bottom, out of the code, to remove it.
I'm sure I made a total newbie mistake, since I am one, but lesson learned.
Related
My objective is to calculate and output a loan repayment schedule. The thing I would like to get help on is putting the principles added to the equation and printing out the repayment schedule. I am not sure if I did the calculations right as I have not had a personal finance class yet, and still get to grasp the concept of loans.
The loan repayment schedule is based on full price of an auto, their interest rate and their payment, assuming no money is put down. All fees and taxes are included in the price and will be financed. I also have to out put the repayment schedule to both the screen and a file - one month per line. . If the user has a credit rate of 800, they get a 3% annual interest rate; 700+ gets 5% interest rate; 600+ get 7% interest rate; and less than 600 get 12% interest rate
The credit scores for 700, 600, and below 600 are left blank because I am just going to copy the 800 credit score part again but change the interest rates.
// This program calculates a loan depending on the pereson's credit score
// how much they can pay per month. It almost outputs the month, principal,
// payment, interest, and the money that's been applied
#include <iostream>
#include <cstdio>
#include <iomanip>
using namespace std;
int main() {
int month = 0, creditScore = 0, whichCar;
double principle, payment = 0.0, interestPaid, applied, interestRate;
cout << fixed << setprecision(2) << showpoint; // Sets total or whatever to 2 decimal points
cout << "---------------------------------------------" << endl; // Displays welcome banner
cout << "| |" << endl;
cout << "| JOLLY GOOD SHOW WE HAVE CARS AYEEE |" << endl;
cout << "| |" << endl;
cout << "---------------------------------------------" << endl;
cout << endl;
cout << "Hey, I see you want a car. You can only purchase one car though." << endl;
cout << endl;
cout << "1. Furawree: $6,969.69" << endl; // Displays menu of autos
cout << "2. Buggee: $420,420.420" << endl;
cout << "3. Sedon: $900" << endl;
cout << "4. Truck: $900,000.90" << endl;
cout << "5. Couppee: $22,222.22" << endl;
cout << endl;
cout << "Which car would you like to purchase?" << endl; // Asks user car type and user inputs car #
cout << "Please enter the number of the car: ";
cin >> whichCar;
cout << endl;
switch(whichCar) { // If user choses a number 1-5, then it asks them how much they can pay each month for the car and their credit score
case 1: // FURAWREE
principle = 6969.69;
break;
case 2: // BUGGEE
principle = 420420.42;
break;
case 3: // SEDON
principle = 900;
break;
case 4: // TRUCK
principle = 900000.90;
break;
case 5: // COUPPEE
principle = 22222.22;
break;
default: // If user doesn't pick a number from 1-5
cout << "Yea uhhmmm we don't have that sorry, go away." << endl;
}
cout << "Please enter how much you can pay each month for this Furawree: ";
cin >> payment;
cout << "Please enter your credit score: ";
cin >> creditScore;
if (creditScore >= 800) {
interestRate = .03 / 12;
do {
interestPaid = principle * interestRate;
applied = payment - interestPaid;
month++;
} while (principle < 0) ;
cout << "Month " << " Principle " << " Payment " << " Interest " << " Applied " << endl;
cout << month << " $" << principle << " $" << payment << " " << interestPaid << " $" << applied << endl;
} else if (creditScore >= 700) {
// Will be copied from the 800 credit score
} else if (creditScore >= 600) {
// Will be copied from the 800 credit score
} else {
// Will be copied from the 800 credit score
}
cout << endl;
cout << endl;
cout << "Your payment: $" << payment << endl;
cout << "Your credit score: " << creditScore << endl;
cout << endl;
cout << endl;
system("pause");
return 0;
}
Mate, you need to fix code under credit - 800.
loop condition is incorrect
cout is after the loop, therefore it will print only once .
principle is not incremented nor decremented . and you are checking if principle is less than 0, however principle is set more than 0. so the loop will execute only once.
you need a fix some thing like this. I have just fine tuned little bit. pls fix the rest
if (creditScore >= 800) {
interestRate = .03 / 12;
cout << "Month " << " Principle " << " Payment " << " Interest " << " Applied " << endl;
cout <<"-------------------------------------------------------" << endl;
do {
interestPaid = principle * interestRate;
applied = payment - interestPaid;
principle = principle - applied;
cout << month << " $" << principle << " $" << payment << " " << interestPaid << " $" << applied << endl;
month++;
} while (principle > 0) ;
} else if (creditScore >= 700) {
Note :-
The above code is not following any object oriented concepts. Its not even functional programming. Introduce classes, methods to reduce headache and it will help to debug.
use \t\t to get spaces instead of spaces.
This code will need a big re-work to make it look professional .
I'm 2 days news to programming and this is my first post, so I'd greatly appreciate your help and patience. :)
My current assignment is to have a user input 2 items bought from a store, including price and quantity, to generate a receipt. For some reason, I can't get any of my code to display after the first item's info gets displayed.
#include <iostream>
#include <iomanip> // For column organization
#include <string> // For item names
using namespace std;
const float TAX = 0.08675;
int main()
{
string itemOne, itemTwo;
double priceOne, priceTwo;
int countOne, countTwo;
cout << "Hello, what is the first item that you are purchasing today?" << endl;
cout << "Please enter the item below." << endl;
getline(cin, itemOne);
cout << endl << "Thank you." << endl;
cout << "Now enter the price and then the quantity of " + itemOne + "(s) purchased, separated by a space." << endl;
cin >> priceOne >> countOne;
cin.ignore();
cout << endl << "What is the second item that you are purchasing today?\n";
cout << "Please enter the item below." << endl;
getline(cin, itemTwo);
cout << endl << "Thank you." << endl;
cout << "Now enter the price and then the quantity of " + itemTwo + "(s) purchased, separated by a space." << endl;
cin >> priceTwo >> countTwo;
/* Calculations for the Receipt */
float subTotal, finalPriceOne, finalPriceTwo, salesTax, finalTotal;
finalPriceOne = countOne * priceOne;
finalPriceTwo = countTwo * priceTwo;
subTotal = finalPriceOne + finalPriceTwo;
salesTax = subTotal * TAX;
finalTotal = subTotal + salesTax;
/* Receipt */
cout << endl << "Your receipt has been calculated and is for your viewing below..." << endl << endl;
cout << "---------------------------------------------------------------\n";
cout << left << setw(15) << "Item";
cout << right << setw(15) << "Quantity";
cout << right << setw(15) << "Price";
cout << right << setw(15) << "Ext. Price";
cout << endl;
cout << "---------------------------------------------------------------\n";
cout << setprecision(2) << fixed;
cout << left << setw(15) << itemOne;
cout << right << setw(15) << countOne;
cout << right << setw(15) << priceOne;
cout << right << setw(15) << finalPriceOne;
cout << endl;
cout << left << setw(15) << itemTwo;
cout << right << setw(15) << countTwo;
cout << right << setw(15) << priceTwo;
cout << right << setw(15) << finalPriceTwo;
cout << endl;
cout << left << setw(15) << "Tax";
cout << right << setw(15) << salesTax;
cout << endl;
cout << left << setw(15) << "Total";
cout << right << setw(15) << finalTotal;
cout << endl;
return 0;
}
On my computer (Windows g++ CodeBlocks) everything is fine. The problem surely comes from your IDE. Here is your code output on my screen :
Just for information, try to use C++ functionalities as POO to dsign your objects. It will be easier for you when coding bigger applications.
I think your execution is just paused at that breakpoint on the endl for the second item. If you step past it (or just remove the breakpoint), does the whole line for the second item appear? If so, it's because of something called "line buffering", where the program doesn't actually output as soon as you tell it to, but instead it collects up things until it sees a line-ending and then it outputs the whole line.
(Breakpoints, in case it's something you did by accident, are a feature of interactive debugging systems where you can have the whole program pause before executing a given line of code. They're often set in IDEs via a right-click menu or a click in the margin next to the line of code.)
I'm a bit new to C++ and I'm making this small program to calculate the gross total of movies tickets.
#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
using namespace std;
int adultTick, childTick;
const int aPrice = 14;
const int cPrice = 10;
float rate() {
const double RATE = .20;
return RATE;
}
double grossTotal = (aPrice * adultTick) + (cPrice * childTick);
int main() {
cout << "Box Office Earnings Calculator ....\n" << endl;
cout << "Please Enter the Name of the Movie: ";
string movie_name;
getline(cin, movie_name);
cout << endl << " \" \" " << "adult tickets sold: ";
cin >> adultTick;
cout << " \" \" " << "child tickets sold: ";
cin >> childTick;
cout << endl << setw(10) << left << "Movie Title: " << setw(20) << right << " \" " << movie_name << " \" " << endl;
cout << setw(10) << left << "Adult Tickets Sold: " << setw(20) << right << adultTick << endl;
cout << setw(10) << left << "Child Tickets Sold: " << setw(20) << right << childTick << endl;
cout << setw(10) << left << "Gross Box Office Profit: " << setw(20) << right << "$ " << grossTotal;
}
At the very end, there is where the program its suppose to display the total? I thought the Arithmetic was correct however I don't understand Why it continuously displays a zero? What could I be doing wrong?
It works if I don't create a variable for the Arithmetic "grossTotal" but I have to do further formatting with "setprecision" and "fixed" function.
The code in main doesn't change grossTotal.
The declaration
double grossTotal = (aPrice * adultTick) + (cPrice * childTick);
… creates a variable grossTotal with a specified initial value. It does not declare a relationship between the values of these variables.
At the time the initializer expression (to the right of =) is evaluated adultTick and childTick are zero, because as namespace scope variables they have been zero-initialized.
int adultTick, childTick;
The shown code declares these variables in the global scope, and these variables get zero-initialized.
double grossTotal = (aPrice * adultTick) + (cPrice * childTick);
The shown code also declares this variable in the global scope, and the calculated formula computes to 0, so this variable will be set to 0.
cout << setw(10) << left << "Gross Box Office Profit: " << setw(20) << right << "$ " << grossTotal;
And this line in main() displays the value of the grossTotal variable, which is, of course, 0.
It is true that before this line, the preceding code in main() sets adultTick and childTick. Which makes no difference whatsoever, since the value of grossTotal has been initialized, already.
You need to change your code so that main() calculates the value of grossTotal, after these other variables are set.
I have my program doing 90% of what I want all that is left to do is get the total by adding all my subtotals and outputting to a file. It my be something simple but I can't seem to find a way to get the total of all my subtotals added together. To be honest, even though I need to make it output into a text file I have not attempted anything yet because I was trying to figure a way to get my total. Would someone mind finding me a solution and explaining it so I get a better understanding.
//Libraries
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main() {
ifstream theFile("input.txt");
string name;
int units;
double price, subtotal;
cout << "\n" <<string(80, '*') << endl;
cout.width(66); cout << "Inventory Report For Jane Doe International Hardware" << endl;
cout << string(80, '*') << "\n" << endl;
cout << left << setw(20) << "ITEM";
cout << right << setw(20) << "NUMBER OF UNITS";
cout << right << setw(20) << "UNIT COST ($)";
cout << right << setw(20) << "TOTAL VALUE ($)" << endl;
cout << string(80, '-') << "\n" <<endl;
cout << fixed;
cout << setprecision(2);
while (theFile >> name >> units >> price) {
subtotal = units*price;
cout << left << setw(20) << name << right << setw(15) << units << right << setw(20) << price << right << setw(20) << subtotal <<endl;
}
cout << "\n" <<string(80, '-') << endl;
cout <<left << setw(20) << "Inventory Total ($)" << right << setw(55) << "total" <<endl;
return 0;
}
My input text file
Chisel 50 9.99 Hammer 30 15.99 Nails 2000 0.99
Bolts 200 2.99 Nuts 300 1.99 Soap 55 1.89
You need to sum up all subtotals. However, each subtotal is only accessible in its iteration, afterwards, it's lost since you reassign subtotal.
Hence, declare a variable total outside of your while loop, then add the subtotal to the total in each iteration. Thus, add the following line
subtotal = units*price;
total += subtotal;
Now you can print total later on.
So i am very new to programming and C++. This little simple program is my second one and I'm in need of a little assistance. Below is my code followed by the output that I am getting and what I want it to look like. If anyone can point me in the right direction, or let me know how to change this it would be much appreciated.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
double propValue, //Property Value
assessment, //Assessment
srAssessment, //Sr Assessment
taxRate, //Tax rate
annualPropTax, //Annual Property tax
quarterlyTax; //Quarterly Tax
string name;
const double EXEMPT = 5000, //shows the total after exemption
QUARTER = 4, //represents the amount of quarters in a year
TAXPERHUNDRED = 0.01, //represents tax rate for every $100
SIXTYPERCENT = 0.6; //Represents the tax based on 60% of original value
//Gets name from user
cout << "Please enter your full name: ";
getline(cin, name);
//gets property value from user
cout << "Enter the actual value of the property: ";
cin >> propValue;
//Gets tax rate
cout << "Enter the tax rate for each $100 of assessed value: ";
cin >> taxRate;
cout << endl << endl;
//Calculates assessment
assessment = propValue * SIXTYPERCENT;
//Calculates Sr. Assessment
srAssessment = assessment - EXEMPT;
//Calculates annual property tax
annualPropTax = srAssessment * taxRate * TAXPERHUNDRED;
//Calculates Quarterly tax
quarterlyTax = annualPropTax / QUARTER;
//Displays owners name
cout << "Property owner's name: " << name << endl;
cout << endl;
cout << setprecision(2) << fixed;
//Displays Assesment
cout << "Assessment: " << setw(18) << "$ " << srAssessment << endl;
//Displays Annual Property tax
cout << "Annual Property Tax" << setw(11) << "$ " << std::right << annualPropTax << endl;
//Displays Quarterly Property tax
cout << "Quarterly Property Tax" << setw(8) << "$ " << std::left << quarterlyTax;
cout << endl << endl;
}
This is the current output:
Assessment: $ 175000.00
Annual Property Tax $ 7177.50
Quarterly Property Tax $ 1780.63
What I need it to do is display as so:
Assessment: $ 175000.00
Annual Property Tax $ 7177.50
Quarterly Property Tax $ 1780.63
I guess it should be intutive. Add setw to second print also:
cout << "Assessment: " << setw(18) << "$ " << setw(10) << std::right << srAssessment << endl;
// ^^^^^^^^^^^^^^^^^^^^^^^^^
cout << "Annual Property Tax" << setw(11) << "$ " << setw(10) << std::right << annualPropTax << endl;
// ^^^^^^^^^^^
cout << "Quarterly Property Tax" << setw(8) << "$ " << setw(10) << std::right << quarterlyTax;
// ^^^^^^^^^^^^^^^^^^^^^^^^^
Add right in your cout statement
Here is another stack overflow post related:
Right Justifying output stream in C++
std::cout << std::right << std::setw(x) << "output";
Where x is an integer to represent the width of the following "output".