I am having an issue with this program. I need it to ask for a user id then ask for book code and then the cost of a book. An individual can enter an unknown number of books. the program needs to then calculate the individual students book total and then ask another student who does the same. the program must then display the grand totals and total number of books. I cant seem to figure out what to use to be able to keep track of the individual students entries. I would be able to do this from what I was reading about arrays. But we are not to that point yet. The professor wants us to do this with a loop. I am so lost, any help would be awesome.
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
//Declare Variables.
int student_id;
char book_code;
float book_cost;
float tax_amount;
float book_subtotal;
const int SENTINEL = -9999;
const double TAX = .07;
float total_book_cost;
int number_books;
int total_books_sold;
double grand_total;
//Set Variables to Zero.
number_books = 0;
total_book_cost = 0.00;
grand_total = 0.00;
//Set Decimal to two places.
cout << fixed << showpoint;
cout << setprecision(2);
//Input Data
cout<<"Please enter your Student ID, then press enter."<<endl;
cin>>student_id;
while (student_id != SENTINEL){
cout<<"Please enter your Book Code, then press enter."<<endl;
cin>>book_code;
cout<<"Please enter the cost of the book, then press enter."<<endl;
cout<<"$"; cin>>book_cost;
tax_amount = book_cost * TAX;
book_subtotal = book_cost + tax_amount;
total_book_cost += book_subtotal;
number_books++;
cout<<"\tStudent Textbook Purchases Report"<<endl;
cout<<"********************************************"<<endl;
cout<<"Student"<<"\tBook"<<"\tBook"<<"\tTax"<<"\tBook"<<endl;
cout<<"Id"<<"\tCode"<<"\tCost"<<"\tAmount"<<"\tSubtotal"<<endl;
cout<<"--------------------------------------------"<<endl;
cout<<student_id<<setw(5)<<book_code<<setw(8)<<"$"<<book_cost<<
setw(3)<<"$"<<tax_amount<<setw(4)<<"$"<<book_subtotal<<endl;
cout<<endl;
cout<<"Total number of books purchased:"<<setw(8)<<number_books<<endl;
cout<<"Total books cost including tax:"<<setw(9)<<"$"<<total_book_cost<<endl;
cout<<"Please enter your Student ID, then press enter."<<endl;
cin>>student_id;
}
grand_total += total_book_cost;
total_books_sold += number_books;
cout<<"**************************************************"<<endl;
cout<<"Grand Totals:"<<endl;
cout<<"Total number of students who purchased books:"<<endl;
cout<<"Total number of books sold:"<<endl;
cout<<"Total cost of all books and taxes:"<<setw(9)<<"$"<<grand_total<<endl;
//Can put grand totals here
system("Pause");
return 0;
}
You could use the loop as this:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
//Set Decimal to two places.
cout << fixed << showpoint;
cout << setprecision(2);
int total_books_sold = 0;
double grand_total = 0.0;
const int SENTINEL = -9999;
int student_id = SENTINEL;
//Input Data
cout<<"Please enter your Student ID, then press enter."<<endl;
cin>>student_id;
while (student_id != SENTINEL){
double total_book_cost = 0.0;
int number_books = 0;
char book_code = '\0';
while (true)
{
cout<<"Please enter your Book Code, then press enter."<<endl;
cin>>book_code;
if (book_code == 'x')
break;
float book_cost;
cout<<"Please enter the cost of the book, then press enter."<<endl;
cout<<"$"; cin>>book_cost;
const double TAX = .07;
double tax_amount = book_cost * TAX;
double book_subtotal = book_cost + tax_amount;
total_book_cost += book_subtotal;
number_books++;
cout<<"\tStudent Textbook Purchases Report"<<endl;
cout<<"********************************************"<<endl;
cout<<"Student"<<"\tBook"<<"\tBook"<<"\tTax"<<"\tBook"<<endl;
cout<<"Id"<<"\tCode"<<"\tCost"<<"\tAmount"<<"\tSubtotal"<<endl;
cout<<"--------------------------------------------"<<endl;
cout<<student_id<<setw(5)<<book_code<<setw(8)<<"$"<<book_cost<<
setw(3)<<"$"<<tax_amount<<setw(4)<<"$"<<book_subtotal<<endl;
cout<<endl;
};
grand_total += total_book_cost;
total_books_sold += number_books;
cout<<"Total number of books purchased:"<<setw(8)<<number_books<<endl;
cout<<"Total books cost including tax:"<<setw(9)<<"$"<<total_book_cost<<endl;
cout<<"Please enter your Student ID, then press enter."<<endl;
cin>>student_id;
}
cout<<"**************************************************"<<endl;
cout<<"Grand Totals:"<<endl;
cout<<"Total number of students who purchased books:"<<endl;
cout<<"Total number of books sold:"<<endl;
cout<<"Total cost of all books and taxes:"<<setw(9)<<"$"<<grand_total<<endl;
//Can put grand totals here
system("Pause");
return 0;
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
We are supposed to make a retail sales cashier but I just can't figure out the loops at all. We have only learned simple selection and repetition statements so far and I know that's all I need but I just can't seem to figure it out.
Project Overview
Starter Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double cashDrawer = 500.00;
int productID = 0;
int quantity = 0;
double price = 0.0;
double subtotal = 0.0;
double salesTax = 0.0;
double totalSale = 0.0;
int anotherSale = 1;
// Loop for repeat sales
// Enter the first Product ID for the first sale (-1 to exit)
// Main loop for each sale
// Switch statement to determine the price, and calculate sales tax, if any, for the item.
// Get next Product ID
// Print properly formatted output for each sale
// Another sale?
// Display how much is in the cash drawer at the end
}
What I have so far:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double cashDrawer = 500.00;
int productID = 0;
int quantity = 0;
double price = 0.0;
double subTotal = 0.0; // for receipt purposes
double salesTax = 0.0; // for receipt purposes
double totalSale = 0.0; // for receipt purposes
int anotherSale = 1;
double taxRate = 0.075; // default tax rate
// Loop for repeat sales
while ()
{
// Enter the first Product ID for the first sale (-1 to exit)
cout << "Enter the first Product ID: ";
cin >> productID;
// Main loop for each sale
while (productID > 0)
{
// Switch statement to determine the price, and calculate sales tax, if any, for the item.
switch (productID)
{
case 101:
price = 65.00;
taxRate = 0.075;
break;
case 102:
price = 12.50;
taxRate = 0;
break;
case 103:
price = 24.50;
taxRate = 0.00;
break;
case 104:
price = 38.75;
taxRate = 0.075;
break;
case 105:
price = 17.80;
taxRate = 0.075;
break;
case 106:
price = 16.50;
taxRate = 0;
break;
case 107:
price = 42.85;
taxRate = 0.075;
break;
case 108:
price = 32.99;
taxRate = 0.075;
break;
case 109:
price = 28.75;
taxRate = 0.075;
break;
case 110:
price = 51.55;
taxRate = 0;
break;
default:
cout << "INVALID PRODUCT ID: Product ID not found." << endl;
}
cout << "Enter the quantity: ";
cin >> quantity;
subTotal += price * quantity;
salesTax += price * quantity * taxRate;
totalSale = subTotal + salesTax;
// Get next Product ID
cout << "Enter the next Product ID: ";
cin >> productID;
}
// Print properly formatted output for each sale
// Another sale?
}
// Display how much is in the cash drawer at the end
}
Any help is appreciated, thank you guys in advance.
The basic aspect of a loop statement is to repeat the same set of instructions until a specified condition is met. You have successfully figured it out for the inner loop of your code where you are checking if productID>0. You need to do the same for the outer loop and impose a similar condition on anotherSale i.e. while(anotherSale!=0). Every time the inner loop finish, just ask the user for the value of anotherSale; if the user enters 0, the loop should break.
I'm currently working on an assignment and it's meant to take the concept of a car body shop offering different trim packages at different prices, the program is meant to use a pretest loop to stop the function if a user inputs a code that isn't already on the array, and then the user inputs the base price of their car and the program is meant to add the base price, the trim price and a 15% sales tax and give the new user their total cost. If I only had to create a function that displayed array's I think I'd have no trouble but I'm currently tearing my hair out trying to wrap my head around how to get all the different functions to work together
currently my algorithm is
1)enter the base price of car
2.) Enter the trim package code
3.) searchIndex=0
while OptionPackageCodeArray =[search index]
searchIndex++
end loop
if searchIndex<5
input packageCode
OptionPackageCode[searchIndex] = packageCode
else
Display error "This code does not exist"
end if
4.) Determine totalCost
PackageCostArray[searchIndex] + basePrice = costwithPackage
totalCost = costwithPackage*0.15
5.) display total cost
"The price of your car with Trim is" : totalCost
end loop
and the actual C++ I have written so far is
#include <iostream>
#include <string>
using namespace std;
int main()
{
//declare variables
double basePrice = 0.00;
string OptionPackageCodeArray[] = {"BB", "SP", "NP", "HE", "UC"};
double PackageCostArray [] = {1500.00, 3250.00, 4575.00, 7500.00, 5220.00};
double totalCost = 0.00
//prompt for base price
cout << "Enter base price:";
cin>>basePrice;
cout <<"enter package code: BB, SP, NP, HE, UC";
cin >> OptionPackageCodeArray;
}
however I'm stuck at this point
if anybody has any suggestions I'd be happy to take them.
you just write the code step by step. you can read blow code for reference.
double basePrice = 0.00;
static const int num = 5;
string OptionPackageCodeArray[num] = {"BB", "SP", "NP", "HE", "UC"};
double PackageCostArray [num] = {1500.00, 3250.00, 4575.00, 7500.00, 5220.00};
double totalCost = 0.00;
while(true)
{
//prompt for base price
cout << "Enter base price:";
cin>>basePrice;
std::string package;
cout <<"enter package code: BB, SP, NP, HE, UC"<<std::endl;
cin >> package;
int i = 0;
for (; i < num; i++)
{
if (OptionPackageCodeArray[i] == package)
{
break;
}
}
if (i == num)
{
break;
}
else
{
std::cout<<(basePrice + PackageCostArray[i]) * 0.15<<std::endl;
}
}
I am working on an assignment in C++ but i am relatively new to the C++ programming language, however, I am getting errors in my output.
Question
The manager of the Crosswell Carpet Store has asked you to write a program to print customers’ bills. The manager has given you the following information:
The length and width of a room are expressed in terms of meters and centimeters. For example, the length might be reported as 16.7 meters.
The store does not sell fractions of a meter. Thus, the length and width must always be rounded up.
The carpet charge is equal to the number of square meters purchased times the carpet cost per square meter. Sales tax equal to 14% of the carpet cost must be added to the bill.
The labour cost is equal to the number of square meters purchased times R24.00, which is the labor cost per square meter. No tax is charged on labour.
Each customer is identified by a five-digit number, and that number should appear on the bill. Large-volume customers, identified by a customer number starting with a '0', may be given a discount. The discount applies to the cost before sales tax is added.
The sample output follows:
CROSWELL CARPET STORE STATEMENT
Customer name : xxxxx
Customer number : xxxxx
Carpet price : xx.xx
Labour : xx.xx
Subtotal : xx.xx
Less discount : xx.xx
Subtotal : xx.xx
Plus tax : xx.xx
Total : xx.xx
And my answer to this question is as follows:
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
int calculateCarpetSize (int length, int width)
{
int carpetSize;
carpetSize = ceil(length * width);
return carpetSize;
}
float calculateCarpetCost (int carpetSize , float sellingPrice)
{
float carpetCost;
carpetCost = carpetSize * sellingPrice;
return carpetCost;
}
float calculateLabourCost(int carpetSize)
{
float labourCost;
labourCost = carpetSize * 24.00;
return labourCost;
}
bool qualifyForDiscount(string customerNo)
{
string dis = "0";
if (customerNo.compare(0, dis.length(), dis) == 0)
{
return true;
}
else{
return false;
}
}
float computeDiscount ()
{
int discountPercentage;
float discount;
cout << "Enter the Percetage Discount: ";
cin >> discountPercentage ;
discount = discountPercentage / 100;
return discount;
}
void printCustomerStatement(string customerName, string customerNo, float carpetCost, float labourCost, float discount)
{
float subtotal = carpetCost + labourCost - discount;
float vat = subtotal*0.14;
cout << "\n CROSWELL CARPET STORE"<<endl;
cout << " STATEMENT"<<endl;
cout << "Customer name : "<<customerName<<endl;
cout << "Customer number : "<<customerNo <<'\n'<<endl;
cout << "Carpet price : "<<carpetCost<<endl;
cout << "Labour : "<<labourCost <<'\n'<<endl;
cout << "Subtotal : "<<carpetCost+labourCost<<endl;
cout << "Less discount : "<<discount <<'\n'<<endl;
cout << "Subtotal : "<<subtotal<<endl;
cout << "Plus Tax : "<<vat<<endl;
cout << "Total : "<<subtotal - vat<<endl;
}
int main()
{
string customerName;
string customerNo;
float carpetSize;
float sellingPrice;
float length;
float width;
float carpetCost;
float labourCost;
float discount;
cout <<"ENTER CUSTOMER NAME:";
cin >>customerName;
cout <<"ENTER CUSTOMER NUMBER:";
cin >>customerNo;
cout <<"ENTER ROOM WIDTH:";
cin >>width;
cout <<"ENTER ROOM LENGTH:";
cin >>length;
cout <<"ENTER SELLING PRICE:";
cin >>sellingPrice;
calculateCarpetSize(length, width);
calculateCarpetCost(carpetSize, sellingPrice);
calculateLabourCost(carpetSize);
if(qualifyForDiscount(customerNo))
{
computeDiscount();
}else
{
discount = 0.00;
}
printCustomerStatement(customerName, customerNo, carpetCost, labourCost, discount);
return 0;
}
I think the problem may be my function or my datatypes, or perhaps both.
EDIT
I get an output of this sort
ENTER CUSTOMER NAME:Quatban
ENTER CUSTOMER NUMBER:02234
ENTER ROOM WIDTH:20
ENTER ROOM LENGTH:20
ENTER SELLING PRICE:35
ENTER DISCOUNT PERCENTAGE: 2
CROSWELL CARPET STORE
STATEMENT
Customer name : Quatban
Customer number : 02234
Carpet price : 1.4e+004
Labour : 9.6e+003
Subtotal : 2.4e+004
Less discount : 0
Subtotal : 2.4e+004
Plus Tax : 3.3e+003
Total : 2e+004
First
Thus, the length and width must always be rounded up
Which means your function calculateCarpetSize should look more like :
int calculateCarpetSize (float length, float width)
{
int carpetSize;
carpetSize = ceil(length) * ceil(width);
return carpetSize;
}
Then
In your main :
calculateCarpetSize(length, width);
calculateCarpetCost(carpetSize, sellingPrice);
calculateLabourCost(carpetSize);
You should be storing the result of these functions like :
carpetSize = calculateCarpetSize(length, width);
carpetCost = calculateCarpetCost(carpetSize, sellingPrice);
labourCost = calculateLabourCost(carpetSize);
Edit
This actually gives you as output :
ENTER CUSTOMER NAME:Quatban
ENTER CUSTOMER NUMBER:02234
ENTER ROOM WIDTH:20
ENTER ROOM LENGTH:20
ENTER SELLING PRICE:35
Enter the Percetage Discount: 2
CROSWELL CARPET STORE
STATEMENT
Customer name : Quatban
Customer number : 02234
Carpet price : 14000
Labour : 9600
Subtotal : 23600
Less discount : 0
Subtotal : 23600
Plus Tax : 3304
Total : 20296
This looks like your (expected ?) result, even if I believe the last line should be subtotal + tax and not subtotal - tax but I may be wrong.
to all programmers enthusiast and professionals out there! I am new at C++ programming. I just want to ask how am I going to approach this task in my program "purchasing item". The user was inputting an item code and this item code has a corresponding value and inputting also the quantity of this item. The program will ask "would you like to purchased another item" if the user said yes the program will loop and need to add all previous value that the user inputted. My problem is how am I going to loop this and total all value of item that the user inputted. We dont use fstream.h
p104 and p103 is an item code
#include<iostream.h>
#include<stdio.h>
#include<math.h>
#include<conio.h>
main()
{
clrscr();
int p104 = 25, order, total = 0, subtotal, p103 = 22,itemcode, quantity, back;
do{
cout<<"Enter Item Code: ";
cin>>itemcode;
cout<<"\nEnter number of quantity: ";
cin>>quantity;
cout<<subtotal;
total=total+subtotal;
cout<<"\n"<<total;
cout<<"\nWould you like to purchase other item? [Y]-yes?";
cin>>back;
}
while(back=='Y'||back=='y');
getch();
return 0;
}
Use getchar() after cin>>back_; for taking the newline character and calculate subtotal based on condition.
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<conio.h>
using namespace std;
main()
{
//clrscr();
char back_;
int p104 = 25, order, total = 0, subtotal =0, p103 = 22,itemcode, quantity;
do{
cout<<"Enter Item Code: ";
cin>>itemcode;
cout<<"\nEnter number of quantity: ";
cin>>quantity;
//cout<<subtotal;
if (itemcode == 104){
subtotal = quantity * p104;
}else if (itemcode == 103){
subtotal = quantity * p103;
}
total=total+subtotal;
subtotal =0;
cout<<"\n"<<total;
cout<<"\nWould you like to purchase other item? [Y]-yes?";
cin>>back_;
getchar();
}
while(back_=='Y'||back_=='y');
return 0;
}
I created a program that calculates loans, but it doesn't go under the guidelines of what my professor asked. Can show me the correct alteration. Source Code would be awesome and time saving, but you don't have to.
Heres the problem:
Write a program that lets the user enter the loan amount and the loan period in number of years and displays the monthly and total payments for each interest rate starting from 5% to 8%, with an increment of 1/8. Heres a sample run:
Loan amount: 10000 [Enter]
Numbers of Years: 5: [Enter]
Interest rate Monthly Payment Total Payment
5% 188.71 11322.74
5.125% 189.28 11357.13
Heres my previous code:
# include <iostream>
# include <iomanip>
# include <cmath>
using namespace std;
int main ()
{
double loanAmountA;
double annualRate;
double paymentAmount;
double amountInterest;
double ratePeriod;
double balanceAfter;
double amountApplied;
double balance;
double paymentPeriod;
int paymentsPerYear;
int totalPayments;
int loanCount = 1;
int paymentCount = 1;
bool anotherLoan = true;
char response;
while (anotherLoan == true)
{
cout<<"Enter amount of loan A:$ ";
cin>>loanAmountA;
cout<<endl;
cout<<"Enter annual percentage rate (APR): "<<"%";
cin>>annualRate;
cout<<endl;
cout<<"Enter the number of payments per year: ";
cin>>paymentsPerYear;
cout<<endl;
cout<<"Enter the total number of payments: ";
cin>>totalPayments;
cout<<endl;
cout<<"Payment Payment Amount Amount to Balance after";
cout<<endl;
cout<<"Number Amount Interest Principal This Payment";
cout<<endl;
cin.ignore(80,'\n');
while (paymentCount <=totalPayments)
{
annualRate = annualRate / 100;
balance = loanAmountA - totalPayments * paymentAmount;
ratePeriod = balance * annualRate;
paymentAmount = loanAmountA * (totalPayments / paymentsPerYear * annualRate) / totalPayments;
balanceAfter = balance - paymentAmount;
balance = loanAmountA - (paymentCount * paymentAmount);
cout<<left<<setprecision(0)<<setw(3)<<paymentCount;
cout<<setw(13)<<left<<fixed<<setprecision(2)<<paymentAmount;
cout<<setw(26)<<left<<fixed<<setprecision(2)<<ratePeriod;
cout<<setw(39)<<left<<fixed<<setprecision(2)<<balance;
cout<<setw(42)<<left<<fixed<<setprecision(2)<<balanceAfter;
if (paymentCount % 12 == 0)
{
cout<<endl;
cout<<"Hit <Enter> to continue: "<<endl;
cin.ignore(80,'\n');
cin.get();
}
paymentCount++;
loanCount++;
cout<<endl;
}
cout<<"Would you like to calculate another loan? y/n and <enter>";
cin>>response;
if (response == 'n')
{
anotherLoan = false;
cout<<endl<<endl;
cout<<"There were"<<loanCount<< "loans processed.";
cout<<endl<<endl;
}
}
return 0;
}
Did you try to use the debugger, and find the point of failure?