| | |
Help with counter-controlled loop to find average
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2007
Posts: 39
Reputation:
Solved Threads: 0
Hey fellow programmers.
I am just starting to work on C++ and languages in computer programming. So far its not bad yet, but having an issue with this program I am trying to write. The program I am writing is to compute the sum of the minutes exercised per week, as well as the average. The user is asked to input the minutes of exercise per day, and from there am required to use a counter-controlled while loop to figure out the sum and average. I am having issues with my code. I have it so that it displays the sum and average minutes, but it also needs to kick out the number of days exercised and if a 0 is entered, it still gives me seven for days exercised. I am looking for help and ideas how to get this to work right. Thanks for help. My code is below.
I am just starting to work on C++ and languages in computer programming. So far its not bad yet, but having an issue with this program I am trying to write. The program I am writing is to compute the sum of the minutes exercised per week, as well as the average. The user is asked to input the minutes of exercise per day, and from there am required to use a counter-controlled while loop to figure out the sum and average. I am having issues with my code. I have it so that it displays the sum and average minutes, but it also needs to kick out the number of days exercised and if a 0 is entered, it still gives me seven for days exercised. I am looking for help and ideas how to get this to work right. Thanks for help. My code is below.
C++ Syntax (Toggle Plain Text)
//Program to calculate the average number of minutes //excersized per week by an individual. #include <iostream> using namespace std; int main() { int counter; int number; int sum; int limit; limit = 7; sum = 0; counter = 0; while (counter < limit) { cout << "Enter number of minutes excercised per day. Press enter after each entry." << endl; cout << "The first day of the week is Monday." << endl; cin >> number; sum = sum + number; counter = counter + 1; } cout << "The total minutes excersized is " << sum << endl; if (counter != 0) cout << "The average minutes excersized per day during this week is " << sum / counter << endl; else cout << "Zero minutes entered for the week." << endl; if (counter = limit) cout << "The number of days excercised is " << counter << endl; return 0; }
You're incrementing 'counter' each time you loop. You need to add a condition:
And get rid of this:
(Not to mention that you used the assignment operator, not the comparison operator (
while (counter < limit)
{
cout << "Enter number of minutes excercised per day. Press enter after each entry." << endl;
cout << "The first day of the week is Monday." << endl;
cin >> number;
sum = sum + number;
if (number == 0) {
counter = counter + 1;
}
}And get rid of this:
if (counter = limit)
cout << "The number of days excercised is " << counter << endl;==), another reason why it was outputting the wrong number. "Technological progress is like an axe in the hands of a pathological criminal."
As to the final bit always stating 7 days exercised, it's because you've made one of those typo errors we all tend to make from time to time.
You are assigning to counter the value stored in limit, and then evaluating the result of that assignment. It's always going to be 7. You really meant to type
You seem to have a couple logical problems as well. Your loop is limited by counter being less than limit, and counter gets incremented every iteration. You are forcing the user to enter 7 values. So counter will always be 7 (or whatever you set limit to) after the loop.
Counter has no relationship to how many minutes the user may enter - thus this line really doesn't tell a correct story:
To know that, you need to examine the sum variable.
Val
C++ Syntax (Toggle Plain Text)
if (counter = limit) cout << "The number of days excercised is " << counter << endl;
if( counter == limit) didn't you?You seem to have a couple logical problems as well. Your loop is limited by counter being less than limit, and counter gets incremented every iteration. You are forcing the user to enter 7 values. So counter will always be 7 (or whatever you set limit to) after the loop.
Counter has no relationship to how many minutes the user may enter - thus this line really doesn't tell a correct story:
C++ Syntax (Toggle Plain Text)
else cout << "Zero minutes entered for the week." << endl;
Val
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
in addition, this expression
it is also a good idea to get into the habit of initializing a variable at the point of definition; this will protect you from silly errors (using uninitialized variables) and later, when you deal with more complex types, help you write more efficient code. ie. not
but
also, since limit is a constant, declare it as such eg
or
how you write code later is going to be determined to a large extent by the habits you form in the early days; forming good habits now (some people call this programming hygiene) would serve you well in the future,
sum / counter will do integer division; ie. if sum ==23 and counter==7, the result would be 3, not 3.28. you could either make sum a double or write double(sum) / counter to get the fractional part.it is also a good idea to get into the habit of initializing a variable at the point of definition; this will protect you from silly errors (using uninitialized variables) and later, when you deal with more complex types, help you write more efficient code. ie. not
C++ Syntax (Toggle Plain Text)
int counter; int sum; int limit; limit = 7; sum = 0; counter = 0;
C++ Syntax (Toggle Plain Text)
int counter = 0 ; int sum = 0 ; int limit = 7 ;
also, since limit is a constant, declare it as such eg
const int limit = 7 ; or
enum { limit = 7 } ; how you write code later is going to be determined to a large extent by the habits you form in the early days; forming good habits now (some people call this programming hygiene) would serve you well in the future,
Last edited by vijayan121; Oct 14th, 2007 at 12:45 am.
•
•
Join Date: Oct 2007
Posts: 39
Reputation:
Solved Threads: 0
I have tried those suggestions, cleaned up the program, and am now trying to get it to run without asking for each number to be input as the instructor in my class requested. The user should be asked for the minutes, leaving a space between each number and now I cannot get any of it to run right. This is what I currently have. I put in the conditions as joeprogrammer suggested and tried various things to get this to work. I feel like a moron because it is probably something so simple that I am just not getting it to sink in. Anyways, here is my new code for this program. I tried to clean it up a bit so its not so cluttered looking. Thanks in advance for advice on this.
C++ Syntax (Toggle Plain Text)
//Program to calculate the average number of minutes //excersized per week by an individual. #include <iostream> using namespace std; int main() { int counter; int number; int sum; int limit; limit = 7; cout << "Enter number of minutes excercised per day." << endl; cout << "Leave a space between each number of minutes." << endl; cout << "Do not enter a value for zero minutes." << endl; cout << "The first day of the week is Monday." << endl; \ cin >> number; sum = 0; counter = 0; while (counter <= 7) { if (number > 0) { sum = sum + number; counter = counter + 1; } if (number = 0) counter = counter; } cout << "The total minutes excersized is " << sum << endl; if (counter != 0) { cout << "The average minutes excersized per day during this week is " << sum / counter << endl; } cout << "The number of days excercised is " << counter << endl; return 0; }
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
you need to accept seven integers from the user; so you need to move
counter is initially zero; so to execute the loop seven times, you should write
cin >> number; to inside the while loop that executes seven times.counter is initially zero; so to execute the loop seven times, you should write
while (counter < 7) // not <= Last edited by vijayan121; Oct 14th, 2007 at 12:48 am.
•
•
Join Date: Oct 2007
Posts: 39
Reputation:
Solved Threads: 0
I moved cin >> number; inside the loop and now it never stops and runs the rest of the program. I need to only ask for input once, the user leaves spaces between the days, and then calculates it all. With this inside the loop, when I run the program, enter number of minutes, it just goes to the next line on the screen and doesn't seem to run the loop or the rest of the program.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
c++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int counter = 0 ; int sum = 0 ; const int limit = 7 ; int num_days = 0 ; while( counter < limit ) { int number ; cin >> number ; if (number > 0) { sum = sum + number; num_days = num_days + 1; } counter = counter + 1 ; } cout << "The total minutes excersized is " << sum << endl; if( num_days > 0 ) { cout << "The average minutes excersized per day during this week is " << double(sum) / counter << endl; cout << "The number of days excercised is " << num_days << endl; } } /** >c++ exersize.cc && ./a.out 1 0 2 3 4 0 5 The total minutes excersized is 15 The average minutes excersized per day during this week is 2.14286 The number of days excercised is 5 */
Last edited by vijayan121; Oct 14th, 2007 at 1:05 am.
![]() |
Similar Threads
- Alright this loop problem is bugging me (Java)
- Help with calculating avrg from input file. (C++)
- How to find average (Java)
- help! (C++)
- End of file controlled loop (C)
- Need help for simple program, don't have a clue about C (C)
- sentinel controlled while loop (C++)
- help (C++)
Other Threads in the C++ Forum
- Previous Thread: Recursively Reversing a String
- Next Thread: ebooks for c++ programming on linux
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






