Ask Experts Questions for FREE Help !
Ask
    Bamaboy53's Avatar
    Bamaboy53 Posts: 21, Reputation: -1
    New Member
     
    #1

    Oct 29, 2009, 12:32 PM
    Getting a display for calculating average value
    Below you can see that I have a C++ program that should calculate and display the average value to the user, but after entering numbers for calculation the output will not display average. Help!

    #include<iostream>
    using namespace std;

    class average
    {
    private:
    int arr[100]; // Variable to hold the numbers entered by the user
    int total; //To hold the total value of the numbers entered
    float avg; //To hold the average value of the entered numbers
    int count; //To hold the total number of elements to be entered by the user
    //Constructor
    public:
    average()
    {
    cout<<"How Many Numbers do you want to Enter?"<<endl<<"NUMBER : ";
    cin>>count;
    total = 0;
    }
    //Member Functions
    void get_input()
    {
    cout<<endl<<"Please Enter The Numbers : "<<endl;
    for(int I=0;i<count;i++)
    {
    cout<<"Number "<<(I+1)<<" : ";
    cin>>arr[i];
    }
    }
    void calculate_avg()
    {
    for(int I=0;i<count;i++)
    {
    total += arr[i];
    }
    avg = (float)total/count;
    }
    void display()
    {
    cout<<endl<<"Average of Entered Numbers are : "<<avg<<endl;
    }
    };

    //Main Function
    int main()
    {
    average avrg;
    avrg.get_input();
    avrg.calculate_avg();
    avrg.display();
    return 0;
    }
    Scleros's Avatar
    Scleros Posts: 2,165, Reputation: 262
    Hardware Expert
     
    #2

    Oct 29, 2009, 03:56 PM
    Quote Originally Posted by Bamaboy53 View Post
    Help!
    I copied and pasted your code into a Visual Studio 2005 command-line project and it worked for me with ad-hoc int number entry for sets of 5, 10, and 20 numbers. So, what set of numbers did you enter that didn't provide the correct average?

    A typical gotcha with this type of thing is dividing ints and not properly casting and the result getting truncated somewhere in the operation. Also, program fails ungraciously if something other than an int is entered. Input validation is a critical feature of any real-world application. Best to get in the habit.
    Bamaboy53's Avatar
    Bamaboy53 Posts: 21, Reputation: -1
    New Member
     
    #3

    Oct 29, 2009, 04:03 PM

    I am using a DEV C++ program and using various combinations of numbers and whenever I enter the last set of numbers the output still disappears.
    Bamaboy53's Avatar
    Bamaboy53 Posts: 21, Reputation: -1
    New Member
     
    #4

    Oct 29, 2009, 04:10 PM

    Hey I think I figured this thing out. At the bottom just before return 0;
    }
    I inserted a system ("pause"); to hold the output to display the average.
    Scleros's Avatar
    Scleros Posts: 2,165, Reputation: 262
    Hardware Expert
     
    #5

    Oct 29, 2009, 04:46 PM
    Yeah, when you're running a command-line app in a Window, when the program exits the Window disappears too fast to see the final output. Another option would be to add a user prompt to either re-run the program or exit.
    Bamaboy53's Avatar
    Bamaboy53 Posts: 21, Reputation: -1
    New Member
     
    #6

    Oct 29, 2009, 05:04 PM

    I am fairly new at C++, how would I insert a re-run or user prompt?
    Scleros's Avatar
    Scleros Posts: 2,165, Reputation: 262
    Hardware Expert
     
    #7

    Oct 29, 2009, 10:08 PM
    Quote Originally Posted by Bamaboy53 View Post
    how would I insert a re-run or user prompt?
    One way to do it is to place the whole program in a FORever loop and then present a prompt for a certain key. Read the key and check it. If the key is correct, BREAK out of the FOR loop, otherwise the loop repeats.

    Code:
    int main()
    {
      char cKey;
    
      for( ; ; ) {
        average avrg;
        avrg.get_input();
        avrg.calculate_avg();
        avrg.display();
    
        // Output prompt
        cout << endl << "Press 'Q' to quit or any other key to continue..." << endl;
    
        // Read key		
        cin >> cKey;
    
        // Is key a 'Q'?
        if(cKey == 'Q')
          // Exit FOR
          break;
    
        // Output some spacing to visually separate output of next loop cycle
        cout << endl << endl;
      }
    
      return 0;
    }

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Calculating fifo, lifo, and average cost [ 1 Answers ]

Boarders sells a snowboard Xpert, that is popular with snowboard enthusiasts. Below is information relating to Boarder's purchases of Xpert snowboards during September. During the same month, 121 Xpert snowboards were sold. Date Explanation Units Unit Cost Total Cost Sept 1 Inventory 25 $ 100...


View more questions Search