#help with c++ hw code. My loop is not running, and I cant figure out why

1 messages · Page 1 of 1 (latest)

scarlet atlas
#

this is my other assignment i must complete

#

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;
} ```
scarlet atlas
#

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;
}```
scarlet atlas
#

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

scarlet atlas
#

help with c++ hw code. My loop is not running, and I cant figure out why

scarlet atlas
#

also ps i must use repitition structures

scarlet atlas
#

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

scarlet atlas
#

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😭😐😞

severe ore
#
    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."; 
    }
scarlet atlas
#

Omg bro

#

I swear i tried that😭😭

#

It worked

severe ore
#

If you can, use function. You can simplify a lot your code by using function but also make it easier to write it.

scarlet atlas
#

Yeah im supposed to but im scared im gonna mess up my entire code

severe ore
#

Keep this one and write another version with functions

scarlet atlas
#

Ok I will

severe ore
#

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.

scarlet atlas
#

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

severe ore
#

I'll send you an example with your ticket thing

scarlet atlas
#

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

severe ore
#
#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;
}
scarlet atlas
#

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

severe ore
#

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.

scarlet atlas
#

so for the bookstore one an example could be StoreItem( int numItem, string itemName)

severe ore
#

;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;
}
quasi pewterBOT
#
Program Output
Total amount of tickets selected: 5
Total amount: 175
severe ore
# scarlet atlas so for the bookstore one an example could be StoreItem( int numItem, string item...

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.
severe ore
scarlet atlas
#

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

Ty2225

Run C++ code live in your browser. Write and run code in 50+ languages online with Replit, a powerful IDE, compiler, & interpreter.

#

seriously though im going to go over everything you said

#

i cant do anymore though

severe ore
#
discount = total * 0.10;
total = total - discount;

or simpler

discount = total * 0.10;
total -= discount;

or simpler

total -= total * 0.10;
#

It needs to be after you calculated the total of course

scarlet atlas
#

it seems to be not running at all when i have it set to total > 100

#

oh

#

i see what you mean let me try

#

Yes i did it

#

Worked like a charm now to find out why if it says i sold 13 books and games and puzzles if i can only choose one item at a time

#

Should be 13 0 0