C++ While Loops - c++

I am having trouble getting my while loop to run. I am a very beginner coder and I have made many attempts with no success to make this work. I need help PLEASE!! Please be very specific and with laymen terms with your help since I am new to this.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string mystr1;
string mystr2;
cout << "Please provide your first and last name" << endl;
getline (cin, mystr1);
cout << endl;
cout << "Please provide your shipping address" << endl;
getline (cin, mystr2);
cout << endl;
cout << "Hello " << mystr1 << " and welcome to Faulk Couture
Handbags Boutique" << endl; // prints Hello and welcome to Faulk
Couture Handbags Boutique
cout << endl;
cout << "We have a variety of specialty and fashionable handbags to
select from. Please see below for the available products and their
descriptions." << endl;
cout << endl;
cout << "Product 1: Crosby Carryall in black priced at $395. This
sophisticated and spacious Crosby Carryall is a work-to-weekend favorite
and is finished with " << endl;
cout << "bound leather edges, a detachable leather strap and petite
brass turnlocks securing its two zippered compartments."<< endl;
cout << endl;
cout << "Product 2: Prairie satchel with chain nude priced at $450.
Crafted in lightweight pebble leather with a bit of sheen, this
gracefully curved shape distills" <<endl;
cout << "the satchel to its purest form. The simple design is finished
with a slender strap and an elegant chain detail that detaches for a
different look." << endl;
cout << endl;
cout << "Product 3: Faulk Swagger 20 brown priced at $325. This
Statement belting with double-turnlock hardware is one of our most
popular designs with a little bit of “swagger.” "<< endl;
cout << "Named for a bold, brass-trimmed Bonnie Cashin design from 1967,
this very modern carryall in refined pebble leather comes finished with
a detachable strap for crossbody wear." << endl;
cout << endl;
cout << "Product 4: Zip top tote in brown priced at $285. This
sophisticated and light weight in signature canvas with hand-finished
leather trim, this aptly named tote is made for one-the-go ease." <<
endl;
cout << "A modern, flared shape and oversized strap anchors add playful
proportions to its spacious, brightly lined design." << endl;
cout << endl;
cout << "Product 5: Wristlet 24 priced at $175. This striking, feminine
design in polished pebble leather has space enough for a tablet and an
elegant chain that converts it from wristlet to top handle." << endl;
cout << "A dog-leash clip on the strap and an embossed hangtag charm
finish it with signature Faulk Couture Style." << endl;
cout << endl;
double cost, total, amount;
int product;
cout << "Please enter the product number for your bag choice" << endl;
cin >> product;
cout <<"The respective price for this bag is: " << endl;
cin>>cost;
cout<<"Please enter the quantity you would like to purchase for this
bag choice" << endl;
cin>>amount;
total = cost*amount;
cout <<"Your total purchase price for " <<amount<< " qty of product
number " <<product<< " is " <<total<<"."<< endl;
int choice=1;
while (choice==1);
{
cout << "To purchase another bag, please enter 1 (anything else to
quit)" << endl;
cin >> choice;
cout << "Please enter the product number for your next bag choice" <<
endl;
cin >> product;
cout << "The respective price for this bag is: " << endl;
cin >> cost;
cout << "Please enter the quantity you would like to purchase for this
bag choice" << endl;
cin >> amount;
total = cost*amount;
cout <<"Your total purchase price for " <<amount<< " qty of product
number " <<product<< " is " <<total<<"."<< endl;
}
cout << endl;
return 0;
}

You're ending your while loop with a semicolon that's why it isn't entering the while loop
Here is the while loop without the semicolon
while (choice==1) {
cout << "To purchase another bag, please enter 1 (anything else to
quit)" << endl;
cin >> choice;
cout << "Please enter the product number for your next bag choice" <<
endl;
cin >> product;
cout << "The respective price for this bag is: " << endl;
cin >> cost;
cout << "Please enter the quantity you would like to purchase for this
bag choice" << endl;
cin >> amount;
total = cost*amount;
cout <<"Your total purchase price for " <<amount<< " qty of product
number " <<product<< " is " <<total<<"."<< endl;
}

Related

How to include dollar signs?

For a project I have to write an application that calculates the price of gas. The code works great, but I noticed something that will probably get points deducted. My total price doesn't include a dollar sign. I am stuck on where to add them and how. Below is my code. Please help!
// FinalProject1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
using namespace std;
const double PRICE_OF_REGULAR = 1.67;
const double PRICE_OF_SPECIAL = 1.87;
const double PRICE_OF_SUPER = 1.99;
int main()
{
cout << "Gas Pump Calculator!" << endl;
double numberOfGallons;
cout << "Please enter number of gallons needed: ";
cin >> numberOfGallons;
cout << endl;
cout << "1. Regular" << endl;
cout << "2. Special" << endl;
cout << "3. Super+" << endl;
cout << endl;
int choice;
cin >> choice;
switch (choice)
{
case 1:
cout << endl;
cout << "You chose regular. The total price of gas is: " << (numberOfGallons * PRICE_OF_REGULAR);
cout << endl;
break;
case 2:
cout << endl;
cout << "You chose special. The total price of gas is: " << (numberOfGallons * PRICE_OF_SPECIAL);
cout << endl;
break;
case 3:
cout << endl;
cout << "You chose super+. The total price of gas is: " << (numberOfGallons * PRICE_OF_SUPER);
cout << endl;
break;
}
return 0;
}
Just print a dollar sign after the number:
cout << "You chose regular. The total price of gas is: "
<< (numberOfGallons * PRICE_OF_REGULAR) << "$";
//^ add dollar sign
Or before the number, depending on how you want to print it out.
cout << "You chose regular. The total price of gas is: $"
<< (numberOfGallons * PRICE_OF_REGULAR); //^ add dollar sign
Just add it before your value:
cout << "You chose regular. The total price of gas is: $"
<< (numberOfGallons * PRICE_OF_REGULAR);

If variable equal to 0

Then it will reset to the questions number 1.
I wanted to implement health points system in my code, so that if your hp goes to 0 (zero) when choosing the wrong answer, it will start to question number 1
I'm new to c++ and doesn't know much about it but if you have any recommendation how to improve my coding i'm happy to take your advice.
Code:
void questions()
{
int score, end, hp = 1;
char ans[28];
cout <<"\t\tHEALTH POINTS= " << hp <<"\n\n";
cout << "1.What thing has to be broken before it can be used?\n\n"; //Questions
cout << "[A]-Egg,";
cout << " [B]-Heart,"; //Choices
cout << " [C]-Cube,";
cout << " [D]-Case";
cout << "\n\n";
cout << "YOUR ANSWER IS: ";
cin >> ans[1];
if (ans[1]=='a'||ans[1]=='A') //This will decide if the input is correct
{
cout << "YOUR ANSWER IS CORRECT: [A] - Egg \n\n";
score++;
}
else
{
cout <<"\nWRONG! ";
cout <<"YOU NOW HAVE "<< (hp=(hp-1)) <<" HP LEFT\n\n";
}
cout << "2.Jimmy's mother had three children. The first was called April, \nthe second was called May. What was the name of the third?\n";
cout << "[A]-May,";
cout << " [B]-Jimmy,";
cout << " [C]-April,";
cout << " [D]-Third";
cout << "\n\n";
cout << "Your Answer is: ";
cin >> ans[2];
if (ans[2]=='b'||ans[2]=='B')
{
cout << "YOUR ANSWER IS CORRECT: [B] - Jimmy \n\n";
score++;
}
else
{
cout <<"\nWRONG! ";
cout <<"YOU NOW HAVE "<< (hp=(hp-1)) <<" HP LEFT\n\n";
}
cout << "\n\t\t YOUR SCORE IS:" << score << "/2, ";
cout <<"YOU HAVE "<< hp <<" HP LEFT\n\n";
cout << endl;
cout <<"\n\t\t PRESS ANY KEY TO GO BACK TO CHOICES...";
getch(); //Holds the screen
system("cls");
questions();
One way to improve your approach might be implementing some sort of function to handle asking a question, with predefined choices, and getting an answer back. Instead of writing the code out twice like you do above to ask two questions, you could call the same function twice, passing in the different arguments.

Cin not producing required results

i'm trying to set up a simple flight booking system program, but my second bit of cin code is not calling for input when i run the program. Unlike the initial cin that requires your name initially. the program just runs and returns 0. I'm a beginner at c++ and i know this is a simple fix so please be understanding . Thank you any guidance will be greatly appreciated.
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
int name;
int Seatnumber;
int optionnumber = 1-5 ;
std::string out_string;
std::stringstream ss;
ss << optionnumber;
out_string = ss.str();
cout << "Welcome to the COS1511 Flight Booking System" "\n" << endl;
cout << "Enter full name : " << endl;
cin >> name ; "\n";
cout << "\n" "The Available travel times for flights are:" << endl;
cout << " Depart Arrive" << endl;
cout << "1. 7.00 9.30" << endl;
cout << "2. 9.00 11.30" << endl;
cout << "3. 11.00 13.30" << endl;
cout << "4. 13.00 15.30" << endl;
cout << "5. 15.00 17.30" << endl;
cout << "Choose the time by entering the option number from the displayed list : " << endl;
cin >> optionnumber ;
if (optionnumber == 1-5){
cout << "\n" "The available seats for are as follows " << endl;
}
else
cout << "Incorrect option! Please Choose from 1-5 " << endl;
cout << "First Class(1920)" << endl;
cout << "|A1||A2||A3|----|A4||A5||A6|" << endl;
cout << "|B1||B2||B3|----|B4||B5||B6|" << endl;
cout << "|C1||C2||C3|----|C4||C5||C6|" << endl;
cout << "|D1||D2||D3|----|D4||D5||D6|" << endl;
cout << "| Economy Class(1600)" << endl;
cout << "|E1||E2||E3|----|E4||E5||E6|" << endl;
cout << "|F1||F2||F3|----|F4||F5||F6|" << endl;
cout << "|G1||G2||G3|----|G4||G5||G6|" << endl;
cout << "|H1||H2||H3|----|H4||H5||H6|" << endl;
cout << "|I1||I2|" << endl;
cout << "Please Key in a seat number to choose a seat(eg: A2)" << endl;
cin >> Seatnumber;
}
prompt the user to enter their name.
Then display a menu showing the available times for the flight.
the user can choose a preferred departure time(option 1-5)
the option selected should be validated for 1-5
if the user entered the correct time a seating arrangement for that particular flight time should be displayed to the next user for the user to choose a seat.
Warning
int optionnumber = 1-5 ;
does
int optionnumber = -4 ;
and
if (optionnumber == 1-5){
does
if (optionnumber == -4){
but you wanted if ((optionnumber >= 1) && (optionnumber <= 5))
if the user entered the correct time a seating arrangement for that particular flight time should be displayed to the next user for the user to choose a seat.
No, whatever the result of the test above you continue and write "First Class(1920)" etc so even when the choice is invalid
in
cin >> name ; "\n";
what did you expect about the "\n" ?
I encourage you to check the read success, currently if the user does not enter an integer you do not know that
But are you sure the name must be an integer ? Probably it must be s string
out_string is unused, it can be removed
Visibly Seatnumber is not an int but a string (A1 ...)
you probably want to loop until a valid time is enter, also fixing the other problems a solution can be :
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
string Seatnumber;
int optionnumber;
cout << "Welcome to the COS1511 Flight Booking System" "\n" << endl;
cout << "Enter full name : " << endl;
if (!(cin >> name))
// EOF (input from a file)
return -1;
cout << "\n" "The Available travel times for flights are:" << endl;
cout << " Depart Arrive" << endl;
cout << "1. 7.00 9.30" << endl;
cout << "2. 9.00 11.30" << endl;
cout << "3. 11.00 13.30" << endl;
cout << "4. 13.00 15.30" << endl;
cout << "5. 15.00 17.30" << endl;
cout << "Choose the time by entering the option number from the displayed list : " << endl;
for (;;) {
if (!(cin >> optionnumber)) {
// not an int
cin.clear(); // clear error
string s;
// flush invalid input
if (!(cin >> s))
// EOF (input from a file)
return -1;
}
else if ((optionnumber >= 1) && (optionnumber <= 5))
// valid choice
break;
cout << "Incorrect option! Please Choose from 1-5 " << endl;
}
cout << "\n" "The available seats for are as follows " << endl;
cout << "First Class(1920)" << endl;
cout << "|A1||A2||A3|----|A4||A5||A6|" << endl;
cout << "|B1||B2||B3|----|B4||B5||B6|" << endl;
cout << "|C1||C2||C3|----|C4||C5||C6|" << endl;
cout << "|D1||D2||D3|----|D4||D5||D6|" << endl;
cout << "| Economy Class(1600)" << endl;
cout << "|E1||E2||E3|----|E4||E5||E6|" << endl;
cout << "|F1||F2||F3|----|F4||F5||F6|" << endl;
cout << "|G1||G2||G3|----|G4||G5||G6|" << endl;
cout << "|H1||H2||H3|----|H4||H5||H6|" << endl;
cout << "|I1||I2|" << endl;
cout << "Please Key in a seat number to choose a seat(eg: A2)" << endl;
cin >> Seatnumber;
return 0;
}
Compilation and execution :
pi#raspberrypi:/tmp $ g++ -pedantic -Wextra -Wall cc.cc
pi#raspberrypi:/tmp $ ./a.out
Welcome to the COS1511 Flight Booking System
Enter full name :
bruno
The Available travel times for flights are:
Depart Arrive
1. 7.00 9.30
2. 9.00 11.30
3. 11.00 13.30
4. 13.00 15.30
5. 15.00 17.30
Choose the time by entering the option number from the displayed list :
aze
Incorrect option! Please Choose from 1-5
7
Incorrect option! Please Choose from 1-5
2
The available seats for are as follows
First Class(1920)
|A1||A2||A3|----|A4||A5||A6|
|B1||B2||B3|----|B4||B5||B6|
|C1||C2||C3|----|C4||C5||C6|
|D1||D2||D3|----|D4||D5||D6|
| Economy Class(1600)
|E1||E2||E3|----|E4||E5||E6|
|F1||F2||F3|----|F4||F5||F6|
|G1||G2||G3|----|G4||G5||G6|
|H1||H2||H3|----|H4||H5||H6|
|I1||I2|
Please Key in a seat number to choose a seat(eg: A2)
qsd
pi#raspberrypi:/tmp $
Note entering the name with cin >> name does not allow it to contain several names separated by a space, to allow composed name getline can be used

Car Loan Calculation (C++)

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 .

how can I print a char array in c++

I am working on a simple hotel reservsation Project but I couldn't print the customer name and payment type after entering their names'. Here is the piece of code that I typed.
cout << "Please enter the customer name: " << endl;
cin >> customername1;
cin.getline(customername1,100,'\n');
fflush(stdin);
cout << "Please enter the payment type: " << endl;
cin >> paymenttype;
cin.getline(paymenttype,100,'\n');
fflush(stdin);
cout << "How many days would you like to stay: " << endl;
cin >> days;
room_income = days * 30;
count_room++ ;
count_customer1++ ;
customer_name1[single]= new char [strlen(customername1)+1];
strcpy(customer_name1[single],customername1);
payment_type[single]= new char [strlen(paymenttype)+1];
strcpy(payment_type[single],paymenttype);
cout << "Room number : " << single << endl<< endl;
cout << "Customer name : "<< customer_name1 << endl << endl;
cout << "Payment type : "<< payment_type << endl<< endl;
cout << "Number of day for accomodation :"<< days << endl<< endl;
cout << "Income for this room : "<< room_income<< endl<< endl;
Some random numbers and letters are displayed for the customer name and payment type. How can I write them properly?
For the customer name and payment type, you try to print the array, instead of one element. Since the array is basically just a pointer to the first element, you will get a 'random number', which is the memory address.
Try:
cout << "Customer name : "<< customername1 << endl << endl;
cout << "Payment type : "<< paymenttype << endl<< endl;
And when you've got that working, follow the comments and look into std::string and vectors...