Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 10-16-2005, 10:55 PM   #1
Fully Optimized
 
Š88's Avatar
 
Join Date: May 2005
Posts: 4,775
Send a message via MSN to Š88
Default Little help with basic C++

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?
__________________

__________________
Killing under the cloak of war is no different than murder

Kein Alkohol ist auch keine Lösung!
Š88 is offline   Reply With Quote
Old 10-17-2005, 11:58 AM   #2
Baseband Member
 
Griz803's Avatar
 
Join Date: Jun 2004
Posts: 89
Default Re: Little help with basic C++

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.
__________________

__________________
/*What does not kill us, makes us drink far too much caffeine*/
Griz803 is offline   Reply With Quote
Old 10-17-2005, 04:51 PM   #3
Fully Optimized
 
Š88's Avatar
 
Join Date: May 2005
Posts: 4,775
Send a message via MSN to Š88
Default Re: Little help with basic C++

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;
}
__________________
Killing under the cloak of war is no different than murder

Kein Alkohol ist auch keine Lösung!
Š88 is offline   Reply With Quote
Old 10-18-2005, 12:00 PM   #4
Baseband Member
 
Griz803's Avatar
 
Join Date: Jun 2004
Posts: 89
Default Re: Little help with basic C++

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.
__________________
/*What does not kill us, makes us drink far too much caffeine*/
Griz803 is offline   Reply With Quote
Old 10-18-2005, 05:04 PM   #5
Fully Optimized
 
Š88's Avatar
 
Join Date: May 2005
Posts: 4,775
Send a message via MSN to Š88
Default Re: Little help with basic C++

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!
__________________
Killing under the cloak of war is no different than murder

Kein Alkohol ist auch keine Lösung!
Š88 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 05:42 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0