I am to use one loop to accumulate numbers (and later get average of) and to be a sentinal control (when the user wants to quit).
I think it makes better sense to use 2 loops (nested) but the instructor has insisted on one loop.
Could anyone give me a hint how this could be done. I am a first semester programming student.
thanks in advance.

It is good that you are thinking in terms of separating out functions in your code.

In this case, there is no compelling reason to do it either way. Each loop must iterate through the exact same sequence of numbers.

If you were to use two separate loops though, you would need to store the numbers obtained by the first loop somewhere (such as in a vector or list or an array), then loop through them again to calculate their average.

Write your (one) loop as if you were calculating the average. Only instead of getting the next number from an array or something just get it from the user. When the user gives you the sentinel quit the loop, finish calculating the average, and print it.

Hope this helps.

commented: glad to see you posting again. +12

You could try something like this: (pseudocode)

int input, average, total, count;

while(count++) 
{
ask for input ;
if (input == 0) break; // 0 quits te program
total += input;
average = total / count;
}
 
printf ("Total = %d, Average =%d", total, average;

You'll have to add some errorchecking for the input etc, but I hope this helps.

You could try something like this: (pseudocode)

int input, average, total, count;

while(count++) 
{
ask for input ;
if (input == 0) break; // 0 quits te program
total += input;
average = total / count;
}
 
printf ("Total = %d, Average =%d", total, average;

You'll have to add some errorchecking for the input etc, but I hope this helps.

Yuck. And bad psuedo-code too. Psuedo code should not be written in C/C++. And haven't you ever heard of formatting?

set count to 0
set total to 0
ask for input
while input <> 0 
{
    increment count
    add input to total
    ask for input
}
calculate average from total and count
 
display total and average

If you're going to use a while loop, use it as intended unless you need an endless loop. And don't waste time calculating the average inside the loop.

I greatly appreciate the help.
It just feels the solution is in the tip of my brain but I can't pry it out.

I would like to share what I have, I liked the naming some provided over my own (better descriptive). Yes, I am very big on commenting. There are errors,(haven't figured them all out, any hints would be great), this was to be an user stopped loop; endless.

///cheryl slivinski
//Assignment 7
//due 10/26/2007
//Purpose: compute average of sum of inputs with a function, accumulator, and loop.




#include <iostream>
//function
double average (double total, int count, double avgnum);
//sum is the sum of the numbers
//count is the number of numbers entered

int main(){
  double avgnum, total, input;

do (int count = 0; count++)
{
//prompt and input
  cout<<"Enter a real number of -99.9 to stop:"
  cin>>input;

  total+=input;          }

                    while (num != -99.9)
      //test user response
      //call function
      double average (total, count, avgnum);
      cout <<"The sum of the numbers:"<<total<<endl;

      cout<<"The average is:"<<avgnum<<endl;

      return 0;
                     )

      //function for average
      double average (double total, int count, double avgnum)
      {
      avgnum= total/count;
                return avgnum;
                  }

One other thing, we hadn't eveb touched vectors and arrays yet, Duoas, had to look that up in the book, looks faster than the way we're doing it.

One other thing, we hadn't eveb touched vectors and arrays yet, Duoas, had to look that up in the book, looks faster than the way we're doing it.

That's the problem with some of the help on boards like this. Many experienced programmers can't tell from someone's code that the chance of higher level language constructs are probably beyond their current course level :icon_wink:

One other thing, we hadn't eveb touched vectors and arrays yet, Duoas, had to look that up in the book, looks faster than the way we're doing it.

Er, I expected you to look it up, but the point was that you don't need to use any of it. You will get to them before too much longer in the semester. But it is never faster to use data structures when you don't need to...

You have a very good start. You need to watch for two things: syntax and indentation.

The do...while loop only takes a condition at the end of the loop. All the stuff with the count variable must be moved elsewhere (before and inside the loop). Also, you need to watch your termination conditions. As it is, (if it worked, that is), count will always be larger than the number of numbers entered by one. Think about it. (What if the very first number entered was -99.9? Count would be 1 when it should be 0. What if the very second number entered was -99.9?) A related problem is that when the user does enter -99.9, it is added to the total.

With that in mind, try using a loop like:

int count = 0;
while (true) {
  cout<<"Enter a real number of -99.9 to stop:"
  cin >> input;
  if (input == -99.9) break;
  total += input;
  count++;
  }

Finally, look up functions in your textbook again. You should be getting the average out of your function like this:

avgnum = average( total, count );

Also, what if count is zero? Make sure you check that before trying to divide by zero.

Hope this helps.

Yuck. And bad psuedo-code too. Psuedo code should not be written in C/C++. And haven't you ever heard of formatting?

Yes, I have, and yes I use it something went wrong with the formatting (CTRL_C CTRL_V). Pseudocode might be bad, but it is clear what I was trying to say and it is a solution. I thought that was what this forum was about last time I checked?
Moderating is one thing, insulting people another.

Yes, I have, and yes I use it something went wrong with the formatting (CTRL_C CTRL_V).

Isn't it possible to fix it before you hit SUBMIT? That's what I use PREVIEW for.

Pseudocode might be bad, but it is clear what I was trying to say and it is a solution.

Bad is bad. I simply pointed out why so you could learn.

I thought that was what this forum was about last time I checked?
Moderating is one thing, insulting people another.

Yes, helping people with good code or, preferably, good pseudo code is exactly what this forum is about. Posting bad code (logically and/or formatted) is of little help to those that are trying to learn.

If you feel pointing out mistakes or bad code is an insult, sorry.

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.