Yet another newbie problem thread

This is a discussion on Yet another newbie problem thread within the C++ Programming forums, part of the General Programming Boards category; I am learning C++ from C++ primer Plus. I am currently doing OOP part exercises, and wrote a class which ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    Question Yet another newbie problem thread

    I am learning C++ from C++ primer Plus. I am currently doing OOP part exercises, and wrote a class which stores quarternal income as an array of doubles and minimum, maximum and average of values in an array. Class has 2 constructors:

    1) Arguments: array of doubles and number of elements. Sets other values according to that array.
    2)Default constructor: Asks for an element(using cin and cout). Checks for invalid input. If everythings fine, it increments number of elements. And loops that. Then it calls the first constructor with those arguments.

    I then create an array of 2 Sales objects. First one is constructed via 1), and second one uses default. After that I print data on screen. First one prints correctly, but second one prints wrong double numbers in X*eY format. All I know is that if I check for array values at the end of 1), they're OK. If I do that at the end of default, they're weird.

    Here is the code:

    Code:
    //sales.h
    #ifndef sales_h_
    #define sales_h_
    
    namespace Sales
    {
    	class Sales
    	{
    	private:
    		double quarters[4];
    		double min;
    		double max;
    		double average;
    	public:
    		Sales();
    		Sales(double *arr, int n);
    		void Show() const;
    	};
    }
    #endif
    Code:
    //def.cpp
    #include "sales.h"
    #include <iostream>
    
    namespace Sales
    	{
    		Sales::Sales(double *arr, int n)
    		{
    			if (n == 0)
    			{
    				quarters[0] = 0;
    				min = 0;
    				max = 0;
    				average = 0;
    			}
    			else
    				{
    					if (n > 4)
    						n = 4;
    					for (int i = 0; i < n; ++i)
    							quarters[i] = arr[i];
    					for (int i = n; i < 4; ++i)
    						quarters[i] = 0;
    
    					min = quarters[0];
    					for (int i = 1; i < n; ++i)
    						if (quarters[i] < min)
    							min = quarters[i];
    					max = quarters[0];
    					for (int i = 1; i < n; ++i)
    						if (quarters[i] > max)
    							max = quarters[i];
    
    					average = 0;
    					for (int i = 0; i < n; ++i)
    						average += quarters[i];
    					average /= n;
    				}
    		}
    
    		Sales::Sales()
    		{
    			double temp[4];
    			int i = 0;
    			while (i < 4)
    			{
    				std::cout << "Enter value for quarter #" << i+1 << std::endl;
    				std::cin >> temp[i];
    				std::cin.get();
    				if (!std::cin.good())//check for invalid input
    				{
    					std::cin.clear();
    					while (std::cin.get() != '\n')
    						continue;
    					break;
    				}
    				if (temp[i] < 0)
    					break;
    				++i;
    			}
    
    			Sales(temp, i);
    		}
    
    		void Sales::Show() const
    		{
    			std::cout << "Sales: ";
    			for (int i = 0; i < 4; ++i)
    				std::cout << quarters[i] << " ";
    			std::cout << std::endl;
    			std::cout << "Minimum: " << min << std::endl;
    			std::cout << "Maximum: " << max << std::endl;
    			std::cout << "Average: " << average << std::endl;
    		}
    }
    Code:
    //program.cpp
    #include "sales.h"
    #include <iostream>
    
    int main()
    {
    	double asd[] = {3.5, 6.5, 8.7};
    	Sales::Sales sal[2] = {Sales::Sales(asd, 3)};
    	sal[0].Show();
    	sal[1].Show();
    	std::cin.get();
    	return 0;
    }
    I'd be grateful if someone takes a look at this code. Sry for messiness and not commenting anything, and my English.
    Last edited by High Overlord; 03-05-2011 at 05:58 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,837
    You are calling a constructor from inside a constructor which is not allowed in the current C++ standard.


    Jim

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    I am a bloody idiot. Thanks Jim.

    Made the changes. Works without the problem now. I created a temporary object using 1) and arguments, and assigned it to *this:

    Code:
    *this = Sales(temp, i);
    I knew it was something trivial.
    Note to self: use temporary objects when calling other constructors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Listening socket and thread problem
    By esaptonor in forum Windows Programming
    Replies: 6
    Last Post: 06-19-2010, 03:04 AM
  2. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  3. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 04:34 AM
  4. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 10:34 PM
  5. Replies: 4
    Last Post: 08-15-2002, 11:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21