#help with c++ hw code. My loop is not running, and I cant figure out why
1 messages · Page 1 of 1 (latest)
Plan and code a program utilizing one or more repetition structures to solve the following problem:
Write a program for Ben's Books and Games for all customers that make purchases throughout the day. Charges for customers are based upon the following:
Books: 10.00 Each
Puzzles: 12.25 Each
Games: 25.50 Each
Total purchases for a customer above $100.00 receive a 10% discount.
Have the user enter a character designation for purchase type (B, P, G). Be sure to use a loop to get valid user input. Have the user enter the number of items in the selected category. Each customer will only purchase products in one category. Use a loop here to get this valid user input for the number of items purchased in that category. Be sure to check for invalid data and handle it appropriately - one input at a time.
Loop for multiple customers - have the user enter Y or N to repeat for another customer for the entire day.
Input
Purchase designation (B, P, G) - error check this using a loop to get valid letter input.
Number of items (error check this using a separate loop to get valid number input).
Output
Itemized listing of charges for each customer. For extra credit output an itemized summary of all charges for the day. Be sure to align all decimal points and clearly label and format all output..
im very close to finishing both I just need to figure out why my loop wont run
// This program helps track bens bookstore
#include <iostream>
#include <iomanip>
using namespace std;
//constants
const float BOOK = 10.00;
const float PUZZLE = 12.25;
const float GAME = 25.50;
// Decleration
int purchaseType(int b, int p, int g) {
float total = 0;
int numbItem;
char purchaseType;
cout << "Please enter the type of product you would like to purchase (b, p, g) for Books, Puzzles, and Games.: ";
cin >> purchaseType;
if (char purchaseType ='b') {
cout << "Book Price is $10.00";
total = BOOK * numbItem;
}
else if (char purchaseType = 'p') {
cout << "Puzzle price is $12.25";
total = PUZZLE * numbItem;
}
else if (char purchaseType = 'g') {
cout << "Game Price is $25.50";
total = GAME * numbItem;
}
else {
cout << "Please enter a valid purchase letter, b, p, or g.";
}
}
// User Input/Output
int main() {
float total = 0;
float discTotal;
int numbItem;
cout << showpoint << fixed << setprecision(2) << endl;
cout << "Please enter the amount of the item you wish to purchase: ";
cin >> numbItem;
if (total > 100) {
discTotal = (total * .10) + total;
cout << "Congragulations you spent over 100 dollars allowing you to be rewarded a 10% discount." << endl;
cout << "Amount of items selected: " << numbItem << endl;
cout << "Total: " << total << endl;
cout << "Discounted Total: " << discTotal << endl;
}
else if (numbItem < 0) {
cout << "Error invalid number";
}
else {
cout << "Amount of items selected: " << numbItem << endl;
cout << "Total: " << total << endl;
}
return 0;
} ```
EDITED VERSION NOT SURE IF THIS IS ANY BETTER
#include <iomanip>
using namespace std;
const int s = 35;
const int a = 60;
const int r = 45;
int personStatus(string s, string a, string r) {
string personStatus;
int total;
int numbTicket;
if (personStatus = 's') {
total = 's' * numbTicket;
}
else if (personStatus = 'a') {
total = 'a' * numbTicket;
}
else if (personStatus = 'r') {
total = 'r' * numbTicket;
}
else {
cout << "Error with personal status please either enter s for student. A for adult, or r for retired." << endl;
}
return 0;
}
int main()
{
string personStatus;
int total;
int numbTicket;
cout << showpoint << fixed << setprecision(2);
cout << "Please enter your personal status (s,a,r) for Atudent, Adult, or Retired.";
cin >> personStatus;
cout << endl;
cout << "Please enter the amount of tickets you would like to purchase: ";
cin << numbTicket;
cout << "Total amount of tickets selected: " << numbTicket << endl;
cout << "Total amount: " << total << endl;
return 0;
}```
Trying ti complete this one above first as its due sooner and the one above it due at night. I think im closer to that one but i literally have no idea what these error are telling me to do
help with c++ hw code. My loop is not running, and I cant figure out why
also ps i must use repitition structures
Ok i managed to complete the book store one my loops is running correctly now i changed alot💪🏾. I stil need help with the other one gonna try to tackle that thought
Nevermind
It works but it only does book now
here is my updated code Everything runs now except it always goes down the book pathway even if i enter p or g as a loop
And my error check to see if the user input is equal to either b/p/g does not work but something tells me if i fix the loop that will also fix
Why am i so stupid bruh ugh😭😐😞
To check the equality between 2 values you need to use a double equal sign (==) not a single one.
if (purchaseType == 'b') {
cout << "Book Price is $10.00" << endl;
numbItem = numbItemB;
total = BOOK * numbItem;
paying = BOOK;
} else if (purchaseType == 'p') {
cout << "Puzzle price is $12.25" << endl;
numbItem = numbItemP;
total = PUZZLE * numbItem;
paying = PUZZLE;
} else if (purchaseType == 'g') {
cout << "Game Price is $25.50" << endl;
numbItem = numbItemG;
total = GAME * numbItem;
paying = GAME;
} else {
cout << "Please enter a valid purchase letter, b, p, or g.";
}
If you can, use function. You can simplify a lot your code by using function but also make it easier to write it.
Yeah im supposed to but im scared im gonna mess up my entire code
Keep this one and write another version with functions
Ok I will
In this one you have a good idea of what to do but there's just a lot of syntactic mistakes and some problems with your types
It's really important and will help you a lot to write your programs if you pay more attention to how your indent (horizontal spaces) and generally format your code.
Im not very confident with my functions yet we just learned them
I tried again and it compiled but
Yeah no its not going well
Hmmm
I'll send you an example with your ticket thing
Okay
Trying to figure out why my discount wont go through anymore and how to make one of the things like games sold be 2 and puzzles/ books sold be 0 as of right now they all are the same number
#include <iostream>
#include <iomanip>
using namespace std;
// Constants are always UPPERCASES and each words are separeted by underscores (_)
// I also gave descriptive name, thay way when we use or read them in the code we
// directly know what they are and what they do.
const int STUDENT_PRICE = 35;
const int ADULT_PRICE = 60;
const int RETIRED_PRICE = 45;
// I have 2 parameters, the number of tickets and the buyer's status
int calcualtePrice(int numberTickets, string buyerStatus) {
// I set price to ADULT_PRICE by default
int price = ADULT_PRICE;
// I check if the status is "s" or "r". If it's not,
// the default one, ADULT_PRICE, will be used.
if (buyerStatus == "s") {
price = STUDENT_PRICE;
} else if (buyerStatus == "r")
price = RETIRED_PRICE;
}
// I calculate and return the result
return price * numberTickets;
}
Ok gonna try this and see what im doing weong
sho what should i put inside my paramater i guess im just confused as to what that does
This is a function
You can call it from your main to calculate the price of the tickets
To use it, you would need to pass 2 parameters to the call. The number of tickets in int and the status of the buyer in string.
Ex. calcualtePrice(5, "s");
In your code main, 5 and "s" will be stored in variables.
so for the bookstore one an example could be StoreItem( int numItem, string itemName)
;compile
#include <iostream>
#include <iomanip>
using namespace std;
// Constants are always UPPERCASES and each words are separeted by underscores (_)
// I also gave descriptive name, thay way when we use or read them in the code we
// directly know what they are and what they do.
const int STUDENT_PRICE = 35;
const int ADULT_PRICE = 60;
const int RETIRED_PRICE = 45;
// I have 2 parameters, the number of tickets and the buyer's status
int calcualtePrice(int numberTickets, string buyerStatus) {
// I set price to ADULT_PRICE by default
int price = ADULT_PRICE;
// I check if the status is "s" or "r". If it's not,
// the default one, ADULT_PRICE, will be used.
if (buyerStatus == "s") {
price = STUDENT_PRICE;
} else if (buyerStatus == "r") {
price = RETIRED_PRICE;
}
// I calculate and return the result
return price * numberTickets;
}
int main() {
// In your case, those definition will be empty. This is just for the example to work.
int numberTickets = 5;
string userStatus = "s";
// Get your values from the user and store them in your variables
cout << "Total amount of tickets selected: " << numberTickets << endl;
cout << "Total amount: " << calcualtePrice(numberTickets, userStatus) << endl;
return 0;
}
Total amount of tickets selected: 5
Total amount: 175
What would this function do? By reading the name you gave me, the first thing I think it will do is store an item somewhere. I don't think that's right based on the context and code you sent me. So, be sure to give a name that describe exactly what the function does. Here's how to write a good function name
- Choose a word with meaning (provide some context)
- Avoid generic names (like tmp)
- Attach additional information to a name (use suffix or prefix)
- Don’t make your names too long or too short
- Use consistent formatting (in cpp's case use camelCase)
- Avoid abbreviation.
This will also help you map or understand what you're trying to do.
i cant bro im going to practice more but i need to take a break i give up ive been working all day. https://replit.com/@Ty2225/book-store-4A#main.cpp is there any way to get my function to apply the discount if its over 100$ and to change the output so that it accurately displays how many of what item what purchased at the end
seriously though im going to go over everything you said
i cant do anymore though