Dynamically Allocating Array Space

I've been trying to understand how to dynamically allocate array space in my free time and I've started to work on a simple program that will store temperatures in an array that I will dynamically create space for.I've g

Main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int dayNumber;
	double fahrenheit = 0;
	double cTemperature = 0;
	const double MAXIMUM_TEMPERATURE = 60;// constants for mix/max
	const double MINIMUM_TEMPERATURE = -90 ;
	const int MAXIMUM_DAYS = 365;
	const int MINIMUM_DAYS = 1;
	double *ptrTemperatures;
	
	cout << "How many days would you like to enter? ";
	dayNumber = myValidation::GetValidInteger(MINIMUM_DAYS, MAXIMUM_DAYS);
	try
	{
		double *ptrTemperatures = new double[dayNumber];
	}
	catch(exception e)
	{
		cout << "Failed to allocate memory: " << e.what() << endl;
	}
	cout << "\n\nTEMPERATURE REPORTER\n____________________________\n Please Enter the temperature for each day.";
	
	for(int dayCount = 0; dayCount < dayNumber; dayCount++){
		cout << "Celsius Temperature for Day " << (dayCount + 1) << ": ";
		ptrTemperatures[dayCount] = myValidation::GetValidDouble(MINIMUM_TEMPERATURE, MAXIMUM_TEMPERATURE);
	}
	
	
	

	
	delete[] ptrTemperatures;
	return 0;


Input validation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 
	int GetValidInteger(const int MIN, const int MAX)
	{
	       
	       double validNumber = 0.0; // holds the user input
	       
	       validNumber = GetValidDouble(MIN, MAX); // Get user input as a double
		   
	       if(validNumber > (int)validNumber)      // If user input is not a whole number
	       {
	           // report the problem to the user.
	           cerr << "\nInvalid input. Please try again and enter whole number.\n";
	           validNumber = GetValidInteger(MIN, MAX); // try again using recursion.
	       }
	       return (int) validNumber; // returns a valid value to the calling function.
	}
	
	// GetValidDouble function definition
	double GetValidDouble(const double MIN, const double MAX)
	{
	       
	       double validNumber = 0.0; // holds the user input
	       
		   cin >> validNumber;       // try to get input
	       if(cin.fail())            // if user input fails...
	       {
	           // reset the cin object and clear the buffer.
			   ClearInputBuffer();
	           // report the problem to the user.
	           cerr << "\nInvalid input. Please try again and enter a numeric value.\n";
	           // Try again by calling the function again (recursion)
	           validNumber = GetValidDouble(MIN, MAX);
	       } 
	       else if(validNumber < MIN || validNumber > MAX)// if value is outside range...
	       {
	           // report the problem to the user.
	           cerr << "\nInvalid input. Please try again and enter a value between "
	                << MIN << " and " << MAX << ".\n";
	           // Try again by call the function again (recursion)
	           validNumber = GetValidDouble(MIN, MAX);
	       }
	       return validNumber; // returns a valid value to the calling function.
	}
Last edited on
Topic archived. No new replies allowed.