| | |
Need help with pennies for pay!!!
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 57
Reputation:
Solved Threads: 0
I am writing a program that is supposed to calculate how much a person earns in a month if the salary is one penny for the first day, two pennies for the second day, four pennies for the third day, and so on with the daily pay doubling each day the employee works. The program should ask the user for the number of days the employee worked during the month and should display a table showing how much the salary was for each day worked, as well as the total pay earned for the month. Here is the code i have so far:
Can someone help me figure out what i am doing wrong?
#include <iostream>
using namespace std;
int main()
{
//Declare variables
int days;
int input;
int cents = 1;
int total =0;
cout<<"\nenter number of days worked: ";
cin>>input;
cin.get();
for(int x = 0; x < input; x++)
{
days = cents;
total += days;
cents *= 2;
}
cout<<"\ntotal pay is :"<<total<<" cents";
for(double x = 0; x < input; x++)
{
cout << "\nday"<<x+1<<" = "<<days<<" cents";
}
system("pause");
return 0;
}Can someone help me figure out what i am doing wrong?
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
Working it out on paper will give you a better idea of how to do it. Your input is the number of days, you have a variable called days, so read the input into that variable. Having a variable called input is too vague. Does the number of days change? No. The day may change, but the number of days doesn't. Decide what each variable represents and name it accordingly.
Why would days be equal to cents?
Again, input is not a good name. Replace it with days, or better yet, numDays. cents is a better variable name than input, as is total, but I think you should use variable names like:
One, it is much easier to follow, and two, I think using those variable names will make it much easier to figure out where everything goes.
for(int x = 0; x < input; x++)
{
days = cents;
total += days;
cents *= 2;
}
cout<<"\ntotal pay is :"<<total<<" cents";Why would days be equal to cents?
Again, input is not a good name. Replace it with days, or better yet, numDays. cents is a better variable name than input, as is total, but I think you should use variable names like:
C++ Syntax (Toggle Plain Text)
numDays dailyPayInCents totalPayInCents
One, it is much easier to follow, and two, I think using those variable names will make it much easier to figure out where everything goes.
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
Okay now i'm confused....each time i run my loop i want it to go from day1..day 2...day 3...with the total number of cents doubling each time....my for statements are very squirly
C++ Syntax (Toggle Plain Text)
for (int day = 0; day < numDays; day++) { // adjust money variables and perhaps display something. } // display total
![]() |
Similar Threads
- Where did I go wrong anyone knows? (VB.NET)
- How to search for and get private advertisers for your forum? (Growing an Online Community)
- Google Adsense - The Mirage... (read it before u join) (Advertising Sales Strategies)
- Need help in my C++ Assignment (C++)
- Is this Site 4 Real??? - http://adbux.org/?r=freakinchris (Website Reviews)
- Would you pay to advertise your forum? (Growing an Online Community)
- java help for newbie (Java)
- Need help with small beginner program, please. (C)
Other Threads in the C++ Forum
- Previous Thread: Just out of curiosity...
- Next Thread: How to use VERY Long STL Vectors?
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






