I am having to create a program that reads from the user what time they entered a parking garage and what time they left. The program uses this information to then figure out how much it costed the person to park there. For under 30 minutes, it's free. After 30 minutes and up to 2 hours, it is a $3 base charge, plus 5 cents every minute in excess of 30 minutes. Beyond 2 hours, it is an $8 base charge, plus 10 cents every minute in excess of 2 hours. I have so far got to convert the time the user inputted into all minutes. I am now stuck on what to do for the rest of my functions. I am new to programming, and the arguments in a function still confuse me. If you are able to help or provide any feedback, please tell in the comment section how the arguments were implemented to get the code to run correctly. Here is my code thus far:
#include <iostream>
using namespace std;
int elapsed_time(int entry_time, int exit_time)
{
int total = 0;
total = (exit_time / 100) * 60 + (exit_time % 100) - (entry_time / 100) * 60
+ (entry_time % 100);
return total;
} // returns elapsed time in total minutes
double parking_charge(int total_minutes) {
double total = 0;
double cents = 0;
if(total_mins < 30){
return 0;
}
else if (total_mins <= 120){
cents = (total_mins - 30) * 0.05;
return total = 3 + cents;
}
else{
cents = (total_mins - 120) * 0.10;
return total = 4.5 + 8 + cents;
}
} // returns parking charge
void print_results(total_minutes, double charge)
{
cout << "The charge of parking was: " << parking_charge(total_minutes)
}
int main() {
int entry_time = 0;
int exit_time = 0;
cout << "What was the entry time? (Enter in 24 hour time with no colon) ";
cin >> entry_time;
cout << "What was the exit time? (Enter in 24 hour time with no colon) ";
cin >> exit_time;
cout << print_results(total_minutes, charge);
}
I have updated my code with a working parking charge function. I am now aiming to get the print_results function working correctly and finding out how to get all of this to work together in the main function. Thanks to all for the help so far.
Your are almost done, You have need to call functions properly in main function. Declare two variables in main function totalTime and totalChargethen call the function
#include <iostream>
using namespace std;
int elapsed_time(int entry_time, int exit_time)
{
int total = 0;
total = (exit_time / 100) * 60 + (exit_time % 100) - (entry_time / 100) * 60
+ (entry_time % 100);
return total;
} // returns elapsed time in total minutes
double parking_charge(int total_minutes)
{
int charge;
if (total_minutes <= 30)
charge = 0;
else if (120 > total_minutes < 30)
charge = (3 + (0.05 * total_minutes));
else
charge = (8 + (0.10 * total_minutes));
} // returns parking charge
void print_results(double charge)
{
cout << "The charge of parking was: " << charge;
}
int main()
{
double charge,minutes;
int entry_time,exit_time;
cout << "What was the entry time? (Enter in 24 hour time with no colon) ";
cin >> entry_time;
cout << "What was the exit time? (Enter in 24 hour time with no colon) ";
cin >> exit_time;
minutes=elapsed_time(entry_time,exit_time);
charge=parking_charge(minutes);
print_results( charge);
}
Related
Currently, I am taking a C++ course at my local college, and was given a debugging assignment. In the instructions for this assignment, I was told that the only thing really wrong with this code, is that the conditions for the nested if-else-if statements on lines 82-89 are redundant, however, I cannot see another way to get the same results without having those conditions stay the same...any tips or such would be greatly appreciated!
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
const int BASE_COST = 10;
const int LOW_LIMIT = 20;
const int MID_LIMIT = 40;
const int HIGH_LIMIT = 60;
const double LOW_CHECKS = .10;
const double MIDLOW_CHECKS = .08;
const double MIDHIGH_CHECKS = .06;
const double HIGH_CHECKS = .04;
int main()
{
int numOfChecks;
double multiplierValue;
double totalFee;
cout << fixed << showpoint;
cout << setprecision(2);
cout << "Please enter the number of checks you used this month: ";
cin >> numOfChecks;
if(numOfChecks < 0)
{
cout << "Number of checks can't be negative. Program ends.\n";
exit(1); //terminate the program with error code 1
}
//the following line runs only if the program did not terminate, so start over if-else
if(numOfChecks < LOW_LIMIT)
multiplierValue = LOW_CHECKS;
else if(numOfChecks < MID_LIMIT)
multiplierValue = MIDLOW_CHECKS;
else if(numOfChecks >= MID_LIMIT && numOfChecks < HIGH_LIMIT)
multiplierValue = MIDHIGH_CHECKS;
else if (numOfChecks >= HIGH_LIMIT)
multiplierValue = HIGH_CHECKS;
totalFee = BASE_COST + numOfChecks * multiplierValue;
cout << "Your total for this month is $" << totalFee;
_getch();
return 0;
}
This part else if(numOfChecks >= MID_LIMIT && numOfChecks < HIGH_LIMIT) looks redundant. Provided you keep the order of the range checks, it can be simplified to just else if (numOfChecks < HIGH_LIMIT), same as the one following it (which is just not needed) so that the whole piece looks like:
//the following line runs only if the program did not terminate, so start over if-else
if (numOfChecks < LOW_LIMIT)
multiplierValue = LOW_CHECKS;
else if (numOfChecks < MID_LIMIT)
multiplierValue = MIDLOW_CHECKS;
else if (numOfChecks < HIGH_LIMIT)
multiplierValue = MIDHIGH_CHECKS;
else
multiplierValue = HIGH_CHECKS;
Indeed, all the conditions are redundant: use an algorithm :)
Live On Coliru
#include <iomanip>
#include <map>
#include <iostream>
namespace {
using Threshold = unsigned;
using Rate = double;
static Rate constexpr BASE_COST = 10.0;
std::map<Threshold, Rate, std::greater<> > const tariffs {
{ 0, .10},
{20, .08},
{40, .06},
{60, .04},
};
double fee(unsigned numOfChecks) {
auto rate = tariffs.lower_bound(numOfChecks);
return BASE_COST + rate->second * numOfChecks;
}
}
int main() {
unsigned numOfChecks;
std::cout << "Please enter the number of checks you used this month: ";
if (std::cin >> numOfChecks) {
std::cout
<< "\nYour total for this month is $"
<< std::fixed << std::showpoint << std::setprecision(2) << fee(numOfChecks)
<< std::endl;
} else {
std::cout << "Invalid input\n";
return 1;
}
}
Prints e.g.
Please enter the number of checks you used this month: 10
Your total for this month is $11.00
Please enter the number of checks you used this month: 20
Your total for this month is $11.60
Please enter the number of checks you used this month: 30
Your total for this month is $12.40
Please enter the number of checks you used this month: 40
Your total for this month is $12.40
Please enter the number of checks you used this month: 50
Your total for this month is $13.00
Please enter the number of checks you used this month: 60
Your total for this month is $12.40
Please enter the number of checks you used this month: 70
Your total for this month is $12.80
Please enter the number of checks you used this month: 80
Your total for this month is $13.20
Please enter the number of checks you used this month: 90
Your total for this month is $13.60
Please enter the number of checks you used this month: 100
Your total for this month is $14.00
I have a homework assignment that requires me to calculate a percentage chance from an user input of the numbers 1 to 3. However, I'm not sure how to do this.
This is my code, not all of it though:
void SwingAtBall( Ball *tBall ) {
std::cout << "How hard do you want to hit the ball? Please enter a number between 1 to 3." << std::endl;
int tBallHit;
std::cin >> tBallHit;
if ( tBallHit > 3 || tBallHit < 1 ) {
std::cout << "That is not a valid value. Please enter a number between 1 to 3." << std::endl;
std::cin >> tBallHit;
}
}
// Prompt for 1 to 3. There is a (input * 15) percent chance the ball only goes 5 feet. Otherwise the ball is hit a random number between 0 and (input * 150). Print how far it went.
If my understanding is correct that there is an (input * 15) percent chance the ball will go 5 feet and an (100 - (input * 15)) percent chance it will go anywhere from 0 to (input * 150) feet, then the following code will calculate what you are looking for...
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int inp, chance1, dist1, dist2, dist_final;
do {
cout << "Enter integer between 1 and 3: ";
cin >> inp;
} while (inp < 0 || inp > 3);
dist1 = 5;
srand(time(0));
dist2 = rand() % (inp * 150);
chance1 = (inp * 15) / 100;
dist_final = chance1 * dist1 + (1 - chance1) * dist2;
cout << "It went this far: " << dist_final << endl;
// system("pause");
return 0;
}
There's a whole <random> header for things like this.
Simple percentages are easy, you can use integers for that. Pick either 0-99 or 1-100, and use that in a uniform_int_distribution. Sure, 15% is 3/20 so you could also use a uniform_int_distribution(1,20) but that's more obscure.
"0 to (input * 150)" is a uniform_int_distribution(0,input*150).
I was writing a piece of software that allows to the user to compute the average of ten integer numbers. This is the code:
// Algorithm for computing the average of the grades of a class with the controlled iteration of a counter
#include <iostream>
using namespace std;
int main()
{
int total, // sum of all grades
gradeCounter, // n° of inputted grades
grade, // a single vote
average; // average of grades
// initialization phase
total = 0; //sets the total to zero
gradeCounter = 1; //prepares the counter
// elaboration phase
while ( gradeCounter <= 10 ) { // 10 times cycle
cout << "Enter grade: "; // input prompt
cin >> grade; // input grade
total = total + grade; // adds the grade to the total
gradeCounter = gradeCounter + 1; // increases the counter
}
// end phase
average = total / gradeCounter;
cout << "The class average is " << average << endl;
return 0;
}
Now, I was thinking that writing average = total / gradeCounter; would work, since the last variable stored in gradeCounter is 10; but the result given by average = total / 10; is the real average. I don't get why there's this discrepancy.
Can somebody explain this to me?
Actually what is happening is that after each run of your while loop, the value of gradeCounter is 1 more than the number of times the loop has been run(because you have initialized gradeCounter to 1 before start of the loop). You can do any 1 of the following to fix this.
1)
average = total / (gradeCounter-1);
2)
gradeCounter = 0;
while ( gradeCounter < 10 ) {
cout << "Enter grade: ";
cin >> grade;
total = total + grade;
gradeCounter = gradeCounter + 1;
}
while ( gradeCounter <= 10 ) { //10 times cycle
cout << "Enter grade: "; //input prompt
cin >> grade; //input grade
total = total + grade; //adds the grade to the total
gradeCounter = gradeCounter + 1; //increases the counter
}
In this piece of code, when gradeCounter is 10, the while loop runs once more, incrementing gradeCounter to 11. So later, you're really dividing by 11 rather than 10.
while ( gradeCounter <= 10 )
This condition evaluates to false when, in your case, gradeCounter is incremented to 11.
(11 is not less or equal to 10)
So your counter is eleven, not ten after your loop.
while ( gradeCounter <= 10 ) { //10 times cycle
gradeCounter = gradeCounter + 1;
Grade counter will be 11 after the loop ends (last iteration it gets set to 11, then <= 10 is not true and it exits. So you'd be doing a divide by 11 instead of 10.
You should draw a flowchart for this. If gradeCounter <= 10, you go into the loop, and only when gradeCounter >= 11 do you proceed with average = total/gradeCounter
That's why you get a result divided by 11 instead of 10.
I am attempting to construct a mileage calculator. The design is like this,
If a person drives 100 miles or less then the amount they shall be paid 25 cent per mile.
If a person drives in excess of 100 miles, they shall be paid the initial 25 cents for their first 100 miles in addition to 15 cents for every mile over 100 mile...
So an example would be
10 miles would earn the person a dollar, while 250 miles would earn (25 for the first 100 + 22.50 for the second 150) to a grand total of 47.50..
When I hit start without debugging, the program goes to the black screen to put values in. But then I receive an error message.. I am trying to figure out what it means.
I am using microsoft visual studio 2008. C++ coding.
#include <iostream>
using namespace std;
int main()
{
int varOne ;
cout << "Enter your favorite number" << endl;
cin << varOne << endl;
if(varOne <= 100)
cout << (1/4)*(varOne)<< endl;
if (varOne>= 100)
cout << (.15 * (varOne-100)) + (.25 * 100) <, endl;
return 0;
}
Debug Error!
Program ... isual Studio
2008\Projects\practice\Debug\rorioodweorrfhur.exe
Module: ... isual studio
2008\Projects\practice\Debug\rorioodweorfhur.exe
File:
Run-Time Check Failure #3 - The variable 'var1' is being used without being initialized.
(Press Retry to debug the application)
Here are some simple errors I noticed in your code
cin << varOne << endl;
It should be
cin >> varOne ;
Next error
cout << (.15 * (varOne-100)) + (.25 * 100) <, endl;
This should be
cout << (.15 * (varOne-100)) + (.25 * 100) << endl;
Here are some logical errors.
In your If statements, you are checking >= and <= , Check for equality only once. Change
if(varOne <= 100)
to
if(varOne < 100)
Also change
cout<< (1/4)*(varOne) << endl;
to
cout<< (varOne)/4 << endl;
This is because 1/4 will give 0
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 8 years ago.
Improve this question
I'm posting because I'm having issues figuring out why my "total aid available" is not printing the total of the pell grant, stafford loan, and work-study loan. I've tried fixing my function again and again (I used sources online and in reference book, but I don't know if the issue is that my function won't be called or not, since nothing is printing for the total aid available.
Everything else is fine, except that one thing, and it is really bugging me since no matter what changes I make, I'm in the same state. No errors showing either. I'm using microsoft visual studio for the first time as compiler, so I wonder if that is the issue.
Here is what I have:
#include <iostream>
using namespace std;
double pell_Grant(double); // forward declaration on pell_Grant ( which is a function for calculating pell grant)
double sum(double, int const, double); // declaration for sum function which gives total for the total aid available
int main()
{
double workstudy = 0, pellgrant = 5730, grossincome = 0, Total = 0; // variables
int yes;
int const stafford = 9500; //const declaration
cout << "Lemme Forecast Your FAFSA" << endl;
cout << "Enter your adjusted gross income: " << endl; cin >> grossincome; // input for gross income
if (grossincome >= 30000) // if gross income is higher than 30,000 then print message
{
cout << "Sorry Charlie, your income is too high to run this forecaster!";
return 0;
}
cout << "Can someone claim you as a dependent? [1=yes/0=no]: " << endl; // input to claim dependent or not
cin >> yes;
if (yes == 1) // if 1 for yes is selected then pell grant gets reduced by 750, if 0 or no selected, then program goes by standard procedure
{
pellgrant -= 750;
}
workstudy = 1465; // work study must be nationwide avergae 1,465
if (grossincome >= 19000) // if this condition is met then work study is not met and message is printed as follows...
{
cout << "Your Work-Study Award is not available for your income level" << endl;
workstudy = 0;
}
double foo = pell_Grant(grossincome); // value returned from pell_Grant stored here to give total
Total = sum(workstudy + stafford + pellgrant); // sum function is called and stores result in Total
if (workstudy != 0) // if work study loan isn't more than 19,000 then it will be calculated and printed in next statement
{
cout << "Your Work-Study Award (if available)= " << workstudy << endl;
}
cout << "Your Stafford Loan award (if needed)= " << stafford << endl; // prints stafford loan (const called)
cout << "Your Pell Grant= " << pellgrant << endl; // prints pell grant
cout << "Total Aid Available For You=" << Total << endl; // prints total
return (0);
}
double pell_Grant(double x) // pell_Grant function that calculates pell grant which is assigned 5,730
{
// x is gross income which is assigned 5,730. This is money received that does not need to be repaid.
if ((x > 12000) && (x < 20000)) // statement makes sure adjusted gross is bettween 12000 & 20000
{
double a = x / 1000; // for every 1,000 in adjusted gross income... reduce/subtract 400 from it
a *= 400;
x -= a;
}
if (x > 20000) // check if gross income is more than 20000
{
double a = x / 1000; // for every 1,000 in gross income, subtract 500
a *= 500;
x -= a;
}
return x;
}
double sum(double workstudy , int const stafford, double pellgrant) // function for adding up workstudy loan, stafford loan, and pellgrant loan
{
double Total;
Total = workstudy + stafford + pellgrant;
return (Total); // returns total
}
According to its declaration, the method sum() accepts 3 parameters.
double sum(double, int const, double);
But while calling you are passing only 1 parameter:
Total = sum(workstudy + stafford + pellgrant);
Instead, you need to pass 3 parameters, like this:
Total = sum(workstudy, stafford, pellgrant);
But, I don't understand why you aren't getting any errors! You are trying to call a non-existent function. You must get a compiler error.
You are calling your sum() function incorrectly. This is your code:
Total = sum(workstudy + stafford + pellgrant); // sum function is called and stores result in Total
But your sum() function has three parameters. The correct form to call the function would be:
Total = sum(workstudy, stafford, pellgrant); // sum function is called and stores result in Total