You're incrementing 'counter' each time you loop. You need to add a condition:
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;
(Not to mention that you used the assignment operator, not the comparison operator ( == ), another reason why it was outputting the wrong number.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
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.
if (counter = limit)
cout << "The number of days excercised is " << counter << endl;
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 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:
else
cout << "Zero minutes entered for the week." << endl;
To know that, you need to examine the sum variable.
Val
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
in addition, this expression sum / counter will dointeger 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
int counter;
int sum;
int limit;
limit = 7;
sum = 0;
counter = 0;
but
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,
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
you need to accept seven integers from the user; so you need to move
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 <=
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
#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
*/
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287