I'm doing an assignment for class where I need to output my program to look something like this. Right now I'm stuck on how to output the years like that.
I've added a counter for years and asked for current year as an input?
Can someone lend me a hand?
Town A Town B
8.5% 0.3%
Year 5000 8000
-------------------------------------------
2016 5425 8024
2017 5886 8048
2018 6386 8072
2019 6929 8096
2020 7518 8121
2021 8157 8145
cout << "What year is it? ";
cin >> curYear;
cout << "Enter the population of town A: ";
cin >> townA;
cout << "Enter the growth rate of town A: ";
cin >> growthA;
cout << "Enter the population of town B: ";
cin >> townB;
cout << "Enter the growth rate of town B: ";
cin >> growthB;
while ((townA <= 0) && (growthA <=0) && (townB > townA) && (growthB < growthA) && (growthB > 0))
{
cout << "Error: Values must be positive, please try again." << endl;
cout << "Enter the population of town A: ";
cin >> townA;
cout << "Enter the growth rate of town A: ";
cin >> growthA;
cout << "Enter the population of town B: ";
cin >> townB;
cout << "Enter the growth rate of town B: ";
cin >> growthB;
cout << endl;
}
years = 0;
while (townA <= townB)
{
finalA = ((growthA / 100) * (townA)) + townA;
finalB = ((growthB / 100) * (townB)) + townB;
cout << "Town A " << finalA << endl;
cout << "Town B " << finalB << endl;
years++;
curYear++;
townA = finalA;
townB = finalB;
}
cout << "\n\n" << endl;
cout << setw(3) << "Town A" << setw(15) << "Town B" << endl;
cout << setw(3) << growthA << "%" << setw(14) << growthB << "%" << endl;
cout << setw(4) << townA << setw(16) << townB << endl;
cout << "Year" << endl;
cout << curYear << endl;
cout << "--------------------------------------------" << endl;
cout << endl;
cout << "It took Town A " << years << " years to exceed the population of Town B." << endl;
return 0;
}
Related
This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Closed 1 year ago.
Hi so im creating a program to have the user input 5 candidates and votes then the program spits back out the winner and percent of each candidate but i cant seem to figure out why the double varible dont show as a number like 40.5 or something just 0 and 100. I am still learning how this works
string candidate[5];
int votes[5];
int total;
double percent[5];
string winner;
cout << "Enter the first candidate: ";
cin >> candidate[0];
cout << "Enter the amount of votes he/she recieved: ";
cin >> votes[0];
cout << endl << "Enter the second candidate: ";
cin >> candidate[1];
cout << "Enter the amount of votes he/she recieved: ";
cin >> votes[1];
cout << endl << "Enter the third candidate: ";
cin >> candidate[2];
cout << "Enter the amount of votes he/she recieved: ";
cin >> votes[2];
cout << endl <<"Enter the fourth candidate: ";
cin >> candidate[3];
cout << "Enter the amount of votes he/she recieved: ";
cin >> votes[3];
cout << endl << "Enter the fifth candidate: ";
cin >> candidate[4];
cout << "Enter the amount of votes he/she recieved: ";
cin >> votes[4];
total = votes [0] + votes[1] + votes[2] + votes[3] + votes[4];
percent[0] = (votes[0] / total * 100);
percent[1] = (votes[1] / total * 100);
percent[2] = (votes[2] / total * 100);
percent[3] = (votes[3] / total * 100);
percent[4] = (votes[4] / total * 100);
if (votes[0] > votes[1] && votes[2] && votes[3] && votes[4])
winner = candidate[0];
if (votes[1] > votes[0] && votes[2] && votes[3] && votes[4])
winner = candidate[1];
if (votes[2] > votes[1] && votes[0] && votes[3] && votes[4])
winner = candidate[2];
if (votes[3] > votes[1] && votes[2] && votes[0] && votes[4])
winner = candidate[3];
if (votes[4] > votes[1] && votes[2] && votes[3] && votes[0])
winner = candidate[4];
cout << "Candidate" << setw(5) << "Votes" << setw(5) << "Percent" << endl;
cout << "------------------------------------------------" << endl;
cout << candidate[0] << setw(5) << votes[0] << setw(5) << percent[0] << endl;
cout << candidate[1] << setw(5) << votes[1] << setw(5) << percent[1] << endl;
cout << candidate[2] << setw(5) << votes[2] << setw(5) << percent[2] << endl;
cout << candidate[3] << setw(5) << votes[3] << setw(5) << percent[3] << endl;
cout << candidate[4] << setw(5) << votes[4] << setw(5) << percent[4] << endl;
cout << "Winner: " << winner << endl;
cout << "Total Votes: " << total;
return 0;}
All of the variables in the calculation on the right hand side of your assignment are ints, so the result is an int.
You would have to convert the operands to doubles to get a double result.
percent[0] = (double(votes[0]) / double(total) * 100);
Just needed some help with my program.
I keep getting an error with my if statement.
Could you help me out please.
This is the code I have written.
#include <iostream>
#include <cstdlib>
#include <iostream>
#include <string>
char carChoice;
int random_integer = rand();
int pricePerDay;
int daysOfHire;
int totalCost;
int age;
int telephoneNumber[30];
char name[30];
char address[30];
char response [4];
using namespace std;
int main()
{
cout << "Royal Rentals" << endl;
cout << "---------------------------------------------"<<"\n" <<endl;
cout << "Current Stock: " <<endl;
cout << "A - Lamborghini Aventador" <<endl;
cout << "B - Lamborghini Huracan" <<endl;
cout << "C - Mercedes Benz AMG GT S" <<endl;
cout << "D - Audi R8 V10" <<endl;
cout << "Which car would you like to rent (A, B, C or D)" << "\n" << endl;
cin >> carChoice;
cout << "How many days would you like to hire the car for" << "\n" << endl;
cin >> daysOfHire;
cout << "How old are you?" << "\n" << endl;
cin >> age;
if (age < 21)
{
cout << "Sorry but you have to over the age of 21 to hire our super cars" << "\n" << endl;
cout << "Please close the program to hire a car suitable for your age" << "\n" << endl;
exit (0);
}
cout << "What is your name?" << "\n" << endl;
cin >> name;
cout << "Have you got a full drivers license?" "\n" << endl;
cin >> response;
if (response == 'Y' //not "Y" but only one 'Y')
{
cout << "blah"<< "\n" <<endl;
}
else if (response == 'N')
{
cout << "blah"<< "\n" << endl;
}
cout << "what is your address?" << "\n" << endl;
cin >> address;
cout << "what is your telephone number?"<< "\n" << endl;
cin >> telephoneNumber;
if (carChoice == 'a')
{
totalCost = daysOfHire * 685;
cout << "You have chosen the following car for hire: Lamborghini Aventador" << endl;
cout << "The price per day is 685.00 GBP" << endl;
cout << "The Total Cost is: " << totalCost << endl;
cout << "Invoice Number; " << random_integer << endl;
}
else if (carChoice == 'b')
{
totalCost = daysOfHire * 585;
cout << "You have chosen the following car for hire: Lamborghini Huracan" << endl;
cout << "The price per day is �585.00 GBP" << endl;
cout << "The Total Cost is: " << totalCost << endl;
cout << "Invoice Number; " << random_integer << endl;
}
else if (carChoice == 'c')
{
totalCost = daysOfHire * 485;
cout << "You have chosen the following car for hire: Mercedes Benz AMG GT S" << endl;
cout << "The price per day is �485.00 GBP" << endl;
cout << "The Total Cost is: " << totalCost << endl;
cout << "Invoice Number; " << random_integer << endl;
}
else if (carChoice == 'd')
{
totalCost = daysOfHire * 445;
cout << "You have chosen the following car for hire: Audi R8 V10" << endl;
cout << "The price per day is �445.00 GBP" << endl;
cout << "The Total Cost is: " << totalCost << endl;
cout << "Invoice Number; " << random_integer << endl;
}
else
{
cout << "You have entered an invalid response" << endl;
}
}
The statement is to verify if you have a valid driving license, if you don't then the program should display a message and close. If they do then it should continue so they can enter the rest of their details.
What am I missing from it and could some correct my if statement.
Many thanks,
Irbaaz
if (response == 'Y')
Response is an array, you cannot compare an array with an integer. If you want to check that the whole string consists of one 'Y' symbol, you should check
Strlen(response) == 1 && response[0] == 'Y'
I'm a beginner who has some questions relating to my homework for school.
With my current code I'm stuck in a neverending loop which I'm assuming is because my condition is never actually met (Pop A being greater than Pop B) and I'm unsure how to move on. I'm also not sure how to properly increment/calculate the years needed for Town A to surpass Town B but this is what I've got so far.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int townA;
int townB;
double growthA;
double growthB;
double finalA;
double finalB;
int years = 0;
cout << "Enter the population of town A: ";
cin >> townA;
cout << "Enter the growth rate of town A: ";
cin >> growthA;
cout << "Enter the population of town B: ";
cin >> townB;
cout << "Enter the growth rate of town B: ";
cin >> growthB;
while ((townA <= 0) && (growthA <=0) && (townB > townA) && (growthB < growthA) && (growthB > 0))
{
cout << "Error: Values must be positive, please try again." << endl;
cout << "Enter the population of town A: ";
cin >> townA;
cout << "Enter the growth rate of town A: ";
cin >> growthA;
cout << "Enter the population of town B: ";
cin >> townB;
cout << "Enter the growth rate of town B: ";
cin >> growthB;
cout << endl;
}
years = 0;
while (townA <= townB)
{
finalA = ((growthA / 100) * (townA)) + townA;
finalB = ((growthB / 100) * (townB)) + townB;
cout << "It took Town A " << years << " years to exceed the population of Town B." << endl;
cout << "Town A " << finalA << endl;
cout << "Town B " << finalB << endl;
}
cout << "\n\n" << endl; // Teacher required us to output it to the screen incase anyone is wondering why I have this block
cout << setw(3) << "Town A" << setw(15) << "Town B" << endl;
cout << setw(3) << growthA << "%" << setw(10) << growthB << "%" << endl;
cout << setw(3) << townA << setw(7) << townB << endl;
cout << "Year" << endl;
cout << "--------------------------------------------" << endl;
return 0;
}
You are not updating the values of townA and townB in the loop. Hence, you never get out of the loop.
Also, you need to increment years in the loop. Use:
while (townA <= townB)
{
finalA = ((growthA / 100) * (townA)) + townA;
finalB = ((growthB / 100) * (townB)) + townB;
cout << "Town A " << finalA << endl;
cout << "Town B " << finalB << endl;
// Update the values of the key variables.
++years;
townA = finalA;
townB = finalB;
}
// This needs to be moved from the loop.
cout << "It took Town A " << years << " years to exceed the population of Town B." << endl;
I have to write a program that simulates an ice cream cone vendor. The user inputs the number of cones, and for each cone, the user inputs the number of scoops, then the flavor(a single character) for each scoop. At the end, the total price is listed. For the pricing, 1 scoop costs 2.00, 2 scoops costs 3.00 and each scoop after 2 costs .75.
I'm having trouble with the pricing. The correct price is displayed if the user only wants one cone.
/*
* icecream.cpp
*
* Created on: Sep 14, 2014
* Author:
*/
#include <iostream>
#include <string>
using namespace std;
void welcome() {
cout << "Bob and Jackie's Ice Cream\n";
cout << "1 scoop - $1.50\n";
cout << "2 scoops - $2.50;\n";
cout << "Each scoop after 2 - $.50\n";
cout << "Ice Cream Flavors: Only one input character for each flavor.\n";
}
bool checkscoops(int scoops) {
int maxscoops = 5;
if ((scoops > maxscoops) || (scoops < 1))
return false;
else
return true;
}
bool checkcones(int cones) {
int maxcones = 10;
if ((cones > maxcones) || cones < 1)
return false;
else
return true;
}
int price(int cones, int numberofscoops) {
float cost = 0.00;
{
if (numberofscoops == 5) {
cost = cost + 5 + (.75 * 3);
}
if (numberofscoops == 4) {
cost = cost + 5 + (.75 * 2);
}
if (numberofscoops == 3) {
cost = cost + 5.75;
}
if (numberofscoops == 2) {
cost = cost + 5.00;
}
if (numberofscoops == 1) {
cost = cost + 2.00;
}
}
cout << "Total price is: " << cost << endl;
}
int buildcone(int numcones) {
char flav1, flav2, flav3, flav4, flav5;
int numberofscoops;
for (int i = 1; i <= numcones; i++) {
cout << "Enter the amount of scoops you wish to purchase. (5 max): ";
cin >> numberofscoops;
checkscoops(numberofscoops);
while (checkscoops(numberofscoops) == false) {
cout << "You are not allowed to buy more than 5 scoops and you "
"cannot buy less than one scoop. Please try again.\n";
cout << "How many scoops would you like?(5 max): ";
cin >> numberofscoops;
checkcones(numberofscoops);
}
cout << "You are buying " << numberofscoops
<< " scoops of ice cream.\n";
if (numberofscoops == 5) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << "Enter flavor 4: ";
cin >> flav4;
cout << "Enter flavor 5: ";
cin >> flav5;
cout << " ( " << flav1 << " )/" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " ( " << flav4 << " )" << endl;
cout << " ( " << flav5 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 4) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << "Enter flavor 4: ";
cin >> flav4;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " ( " << flav4 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 3) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 2) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 1) {
cout << "Enter a flavor: ";
cin >> flav1;
cout << " ( " << flav1 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
}
price(numcones, numberofscoops);
}
int main() {
int numberofcones;
int numberofscoops;
welcome();
cout << "How many cones would you like?(10 max) ";
cin >> numberofcones;
checkcones(numberofcones);
while (checkcones(numberofcones) == false) {
cout << "You are not allowed to buy more than 10 cones and you cannot "
"buy less than one cone. Please try again.\n";
cout << "How many cones would you like?(10 max): ";
cin >> numberofcones;
checkcones(numberofcones);
}
cout << "You are buying " << numberofcones << " ice cream cones.\n";
buildcone(numberofcones);
}
Start by changing the return value of price() to float, or the function won't be able to return the proper cost. Also, since cones is not used to compute the cost of the purchase, we don't it as a parameter:
float price(int numberofscoops)
{
float total_cost = 0.0f;
if (numberofscoops == 1) {
total_cost = 2.0f;
}
else if (numberofscoops == 2) {
total_cost = 3.0f;
}
else if (numberofscoops > 2) {
total_cost = 5.0f + ((numberofscoops-2) * 0.75f);
}
return total_cost;
}
You code could have other problems, but I think these changes will let you continue to debug and fix the code on your own.
Your while() loop is flawed. Comment your call to checkcones() as shown below. You're already calling checkcones() as the conditional in your while(), no need to evaluate again as this will sent you into a perma-loop. You've got two of these while() statements that I could see, you'll want to comment out both.
while ( checkcones( numberofcones ) == false )
{
cout << "You are not allowed to buy more than 10 cones and you cannot buy less than one cone. Please try again.\n";
cout << "How many cones would you like?(10 max): ";
cin >> numberofcones;
// THIS LINE IS THE PROBLEM :)
// checkcones(numberofcones);
}
After this fix, your program begins to work but the pricing fails. You should be able to figure that out with the answer given above.
I would also see if you can figure out how to implement a c++ class with members and methods as your current approach is very "c" like. Happy coding! :)
Hey guys basically I need to put a while loop in my code for example a user can enter the number of tickets for the children and adding -1 to stop. Can you teach me how and where to put my while loop? Its my first time taking a C++ class. I would greatly appreciate your help guys. here is my code and an example of the output.
Example output
Chesapeake Amusement Park
Enter children ticets or -1 to stop... 10
Enter adult tickets................... 11
Chesapeake Amusement Park
----------------------------
Tickets Price Total
Child 10 10.00 10.00
Adult 11 20.50 220.50
21
Security Fee 15.00
Total Bill $ 335.00
Cash received.....340
Change 4.50
Enter children tickets or -1 to stop...
Code
#include <iostream>
#include <iomanip>
using namespace std;
const double ADULTPRICE = 20.50;
const double SECURITYFEE = 15.00;
int main ()
{
double adultTotal, childTotal, totalBill, change, cash;
double CHILDPRICE = 12.00;
int childTix, adultTix, tixTotal;
cout << "\n Chesapeake Amusement Park" << endl << endl;
cout << " Enter children tickets or -1 to stop... ";
cin >> childTix;
if (childTix >= 8)
CHILDPRICE = 10.00;
cout << " Enter adult tickets.................... ";
cin >> adultTix;
childTotal = CHILDPRICE * childTix;
adultTotal = ADULTPRICE * adultTix;
totalBill = childTotal + adultTotal;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "\n\n Chesapeake Amusement Park";
cout << "\n -------------------------";
cout << "\n\n Tickets Price Total\n";
cout << " Children " << setw(3) << childTix
<< setw(14) << CHILDPRICE
<< setw(11) << childTotal;
cout << "\n Adults " << setw(3) << adultTix
<< setw(14) << ADULTPRICE
<< setw(11) << adultTotal;
tixTotal = childTix + adultTix;
cout << "\n ";
cout << "\n " << setw(11) << tixTotal;
cout << "\n ";
if ((tixTotal >= 20) || (childTix >= 14))
cout << "\n Security Fee " << setw(14) << SECURITYFEE;
cout << "\n";
cout << "\n Total Bill $" << setw(11) << totalBill;
cout << "\n ";
cout << "\n ";
cout << "\n Cash received...... ";
cin >> cash;
change = cash - totalBill;
cout << "\n ";
cout << "\n ";
cout << "\n Change " << setw(11) << change;
return 0;
}
Your code is great as a beginner! You just need to do minor changes to your code.
The primary change is to use a correct while expression to make the loop stops when the user inputs (-1) as a value for childTix. I do not recommend that you use the break keyword to exist the loop. You should always try to stop the loop naturally by letting the while expression be evaluated to FALSE. This makes your program easier to read and trace.
Another thing you need to add is the constant variable DISCOUNTEDCHILDPRICE when the number of children tickets is greater than 7. You cannot play with the value of CHILDPRICE inside the while loop cause this will lead to inconsistent results after you run the while loop the next time if the number of child tickets is less than 8. Instead, use if statement inside the body of the loop to figure out if there is a discount or not on children' tickets.
There is also a few other changes not mentioned above.
Here is the revised version of your code. I put the whole program so you and other beginners can learn from it and avoid having such problems in your future programs.
#include <iostream>
#include <iomanip>
using namespace std;
const double ADULTPRICE = 20.50;
const double SECURITYFEE = 15.00;
const double CHILDPRICE = 12.00;
const double DISCOUNTEDCHILDPRICE = 10;
int main()
{
double adultTotal, childTotal, totalBill, change, cash;
int childTix, adultTix, tixTotal;
cout << "\n Chesapeake Amusement Park" << endl << endl;
cout << " Enter children tickets or -1 to stop... ";
cin >> childTix;
while (childTix != -1)
{
cout << " Enter adult tickets.................... ";
cin >> adultTix;
if (childTix < 8)
childTotal = CHILDPRICE * childTix;
else
childTotal = DISCOUNTEDCHILDPRICE * childTix;
adultTotal = ADULTPRICE * adultTix;
totalBill = childTotal + adultTotal;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "\n\n Chesapeake Amusement Park";
cout << "\n -------------------------";
cout << "\n\n Tickets Price Total\n";
cout << " Children " << setw(3) << childTix
<< setw(14) << CHILDPRICE
<< setw(11) << childTotal;
cout << "\n Adults " << setw(3) << adultTix
<< setw(14) << ADULTPRICE
<< setw(11) << adultTotal;
tixTotal = childTix + adultTix;
cout << "\n\n " << setw(11) << tixTotal;
cout << "\n ";
if ((tixTotal >= 20) || (childTix >= 14))
cout << "\n Security Fee " << setw(14) << SECURITYFEE;
cout << "\n\n Total Bill $" << setw(11) << totalBill;
cout << "\n\n";
cout << "\n Cash received...... ";
cin >> cash;
change = cash - totalBill;
cout << "\n\n";
cout << "\n Change " << setw(11) << change;
cout << "\n Chesapeake Amusement Park" << endl << endl;
cout << " Enter children tickets or -1 to stop... ";
cin >> childTix;
}
return 0;
}
First try on your own , then see the solution .
while(1)
{
cout << " Enter adult tickets.................... ";
cin >> adultTix;
if(adultTix == -1)
break;
childTotal = CHILDPRICE * childTix;
adultTotal = ADULTPRICE * adultTix;
totalBill = childTotal + adultTotal;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "\n\n Chesapeake Amusement Park";
cout << "\n -------------------------";
cout << "\n\n Tickets Price Total\n";
cout << " Children " << setw(3) << childTix
<< setw(14) << CHILDPRICE
<< setw(11) << childTotal;
cout << "\n Adults " << setw(3) << adultTix
<< setw(14) << ADULTPRICE
<< setw(11) << adultTotal;
tixTotal = childTix + adultTix;
cout << "\n ";
cout << "\n " << setw(11) << tixTotal;
cout << "\n ";
if ((tixTotal >= 20) || (childTix >= 14))
cout << "\n Security Fee " << setw(14) << SECURITYFEE;
cout << "\n";
cout << "\n Total Bill $" << setw(11) << totalBill;
cout << "\n ";
cout << "\n ";
cout << "\n Cash received...... ";
cin >> cash;
change = cash - totalBill;
cout << "\n ";
cout << "\n ";
cout << "\n Change " << setw(11) << change;
}
Another way of writing while loop is for( ; ; ) . The main point was to use keyword break .