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.

//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:

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.

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

in addition, this expression 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

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,

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.

//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;
}

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 <=

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.

#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
*/

Oh, duh, like I said, I am a moron. I see exactly what you were saying now and understand what it is all doing. Thank you so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.