Write, compile and run a C++ program named that reads in one integer that represents a total number of hours and then displays the equivalent number of weeks, days and hours, all properly labeled.

Have your program only do the calculation if the user enters a number less than 30,000 hours.

Sample run:

Input a whole number of hours less than 30,000:

7683

There are 45 weeks, 5 days and 3 hours in 7683 hours.

3
Contributors
11
Replies
12
Views
9 Years
Discussion Span
Last Post by Narue

You have just purchased a stereo system that costs \$1,000 on the following credit plan: no down payment, an interest rate of 18% per year (and hence 1.5% per month) and monthly payments of \$50.

The monthly payment of \$50 is used to pay the interest and whatever is left over is used to pay part of the remaining debt. Hence, the first month you pay 1.5% of \$1,000 in interest . Thatâ€™s \$15 in interest. So, the remaining \$35 is deducted from your debt, which leaves you with a debt of \$965.00.

The next month you pay interest of 1.5% of \$965.00, which is \$14.48 and you deduct \$50 - \$14.48 which is \$35.52 from the amount you owe.

Have your program print out the month, the amount of interest paid, and the amount of the debt that is paid and the debt that remains in a nice table form.

Make sure to include a line in the table for the final month that you pay.

(Your remaining debt should be zero for that line!)

Seeing as how your posts follow the pattern of "Do it for me because I'm too special to learn", I've merged them into one thread so they can be more easily dismissed by the regulars.

I just wanted to check my code again someone else's because I keep getting error messages. but thats ok, I will continue to work on it and hopefully i can fix it! Thanks for your time. you can delete the thread if you'd like.

``````#include<iostream>
using namespace std;

int main()
{
int number_of_hours;
int weeks;
int days;
int hours;

cout<<"Input a whole number of hours less than 30,000:\n";      //prompt

cin>> number_of_hours;
if number_of_hours<30,000
cout<< number_of_hours << "divided by" <<168
<< "equals" << (weeks)
<< "with a remainder of" << (number_of_hours%168)
cout<< number_of_hours%168 << "divided by" <<24
<< "equals" << (days)
<< "with a remainder of" << ((number_of_hours%168)%24)
cout<< hours = ((number_of_hours%168)%24

else

cout<< "\nweeks= " <<weeks;
cout<< "\ndays= " <<days;
cout<< "\nhours= " <<hours<<endl;

cin>>number_of_hours;

return 0;
}``````

>I just wanted to check my code again someone else's because I keep getting error messages.
If you're getting errors, post your code, post the errors, and we can tell you what's wrong. Asking for code to compare with is silly because nobody will solve the problem the same way you did. The "I want to compare my code with yours" tactic is most often employed by students who want to cheat on their homework but are too cowardly to admit that they're cheating. I'm not saying that's you; in fact, I'm pretty sure it's not, but if it walks like a duck...

Ok, I understand. I'm sorry for eveything. Thanks for your time though.

>I just wanted to check my code again someone else's because I keep getting error messages.
If you're getting errors, post your code, post the errors, and we can tell you what's wrong. Asking for code to compare with is silly because nobody will solve the problem the same way you did. The "I want to compare my code with yours" tactic is most often employed by students who want to cheat on their homework but are too cowardly to admit that they're cheating. I'm not saying that's you; in fact, I'm pretty sure it's not, but if it walks like a duck...

> if number_of_hours<30,000
Re-read your C++ book or lecture notes for the syntax of an if statement.

And also study how to use more { } to clarify which statements belong to which control structure (if, while, etc).

>if number_of_hours<30,000
Check your book again. The condition is surrounded by parentheses, and until you understand the rules for single statements in a block, it's best to always use braces. Finally, C++ doesn't expect commas in a literal value. In fact, if you use it, you'll end up being surprised:

``````if ( number_of_hours < 30000 ) {
// Stuff
}
else {
// Other stuff
}``````

>cout<< number_of_hours << "divided by" <<168
><< "equals" << (weeks)
><< "with a remainder of" << (number_of_hours%168)
Don't forget to terminate your statements with a semicolon.

>cout<< hours = ((number_of_hours%168)%24
I highly recommend not mixing assignments into other expressions. You'll probably end up surprised unless you're very familiar with how the code is parsed. This is ideal:

``````hours = (number_of_hours%168)%24;
cout<< hours;``````

But in your case, you need to make sure the parentheses match and that the assignment is wrapped in parens so cout's << operator sees it properly:

``cout<< (hours = (number_of_hours%168)%24);``

You should also take care to print newlines so your output is readable, but I imagine you can figure out how to implement that. Here's your fixed if statement:

``````if ( number_of_hours < 30000 ) {
cout<< number_of_hours << "divided by" <<168
<< "equals" << (weeks)
<< "with a remainder of" << (number_of_hours%168);
cout<< number_of_hours%168 << "divided by" <<24
<< "equals" << (days)
<< "with a remainder of" << ((number_of_hours%168)%24);
cout<< (hours = (number_of_hours%168)%24);
}
else {
cout<<"Bye\n";
}``````

>I'm sorry for eveything. Thanks for your time though.
Stop being so timid! Just because I'm telling you like it is doesn't mean I'm not going to help you. Geez.

I made the changes but I still get weird answers like... -91289823 for weeks and days. Is my equation right? Iknow the problem has to do with integer division and %.

Failing to initialize weeks and days to a predictable value tends to do things like that.