Little help with basic C++

Ð88

Fully Optimized
Messages
4,775
Alright so this should be simple for someone who knows how to program C++ good. I have made a program for my intro to C++ class but I can't seem to debug it. The problem lies with the declared variables. The program takes user inputed numbers and finds the average. When they hit zero, the average is printed on the screen then it asks if the user would like to restart and find the average of some new numbers. It basically has two loop statements in the program but I have to use the variable for the average twice but the second time it is used must be independent from the first time(because of course when i output the average for the second, third, fourth etc set of numbers it will always display the first average that was found. How would I make it so the average will calculate differently for each new set of numbers?
 
Well, from what you say, there is already a test that initiates the calculation. It should recalculate everytime that the loop is active and the user enters zero. As it recalculates, it should overwrite the variable that the result is assigned to with the new value. So, basically, I don't see your problem or don't understand what the bug is. Post some code, in code tags please and I'll see if I can find it. Or state exactly what is and isn't happening for you.
 
All right here's the entire code that I've written. Keep in mind that if you notice any syntax errors below ignore it, its just from my typing it out. The program compiles (you can even compile it yourself to see the problem). I don't know if there is a simpler way to write what I have done but all Im worried about right now is just getting it to work properly. There are comments below. I just simply dont know how to write the code properly to get it to do what I want it to.

#include<iostream>
using namespace std;

int main {
int number;
float numberSum = 0;
int numberCount = 0;
char answer;

cout << "Enter a number (0 to stop): ";
cin >> number;

for (number; number != 0; numberCount++)
{
numberSum += numberl
cout << "Enter a number (0 to stop): ";
cin >> number;
}

float avg = numberSum/numberCount;
cout << "The average value is "<< avg; //the first average is given here

cout << "\nDo you wish to start again (Y/N)? ";
cin >> answer;
while (answer == 'y')
{
cout << "Enter a number (0 to stop): ";
cin >> number;

for (number; number != 0; numberCount++)
{
numberSum += number;
cout << "Enter a number (0 to stop): ";
cin >> number;
}

float avg = numberSum/numberCount;
cout << "The average value is " << avg; //I need a way to calculate the
//average differently that is
//independent from the first

cout << "\nDo you wish to start again (Y/N)? ";
cin >> answer;
}
if (answer != 'y')
cout << "Good-Bye."<<endl;

return 0;
}
 
Well, that makes things clearer anyway. You need to think about the situation in terms of how to make it do it more than twice to be able to do it the most efficient way. Here is my psuedocode solution.
Code:
          declare variables
          start loop 1
          start loop 2
                         get a number
                         if it is not equal to zero then
                                 add it to the sum
                                 increment the counter
                         if it is equal to zero then
                                 break out of loop 2
          end loop 2
          calculate the average
          display the average
          reset the sum, count and etc. here
          display continue prompt for user's choice
          get response
          if "Y" then continue loop 1
          if "N" then end loop 1


This is only the pseudocode, not source code. You should see what the logic needs to be to solve the problem, not just for two loops but for as many times as the user wants to run it. If this doesn't help after you've made an honest attempt to revise code yourself, I'll take the time to revise and comment your code fully and post it here. Let me hear from you via another post.
 
The program did continually repeat despite the mess of code that i wrote up....but anyways....THANK YOU SO MUCH! I have been working trying to solve this problem for hours and that pseudocode you wrote up made things 100% clear. I was able to write up the code for that like nothin; compiled it; tested it; worked awesome! haha yeah your way is much simpler and unlike mine, it works. Anways thanks again!
 
Back
Top Bottom