Dynamic Memory Allocation - Help please

Hello, ok, so here is my problem/issue I'm having with this code. This is a program that asks a user for days worked, pay rate and hours worked per day.
I'm supposed to create a pointer to a dynamically allocated array of the size equal to days worked.

I sorted coded everything before figuring out the pointer..I got stuck figuring out the math portion to get a gross total(1st issue)

Now, I am sort of working backwards and trying to figure how/where to build the pointer to the dynamically allocated array. (you will notice a big chunk of commented out code, this was my rough draft prior to adding in the pointer. I wanted to just get the basics in first then piece everything out.)

What I need help with.
1. building the pointer to a dynamically allocated array of the size equal to days worked
2. help with the math formula to calculate the gross pay


Here is what I have, if anyone could give me hand with I would appreciate it so MUCH!

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	int *daysworked = nullptr,
	int hours;
	double payrate(0.0);
	int count;
	int grosspay;
	int totalhours;
		  	

	
	cout << "Enter the amount of days worked this pay period: ";
	cin >> count;

	daysworked = new int [hours];

	cout << "What is your hourly pay rate? :";
	cin >> payrate;

	for (count = 0; count < *daysworked; count++)
	{
		cout << "Enter hours worked for day " << count + 1 << ": ";
		cin >> hours;
	}


/*
	while (count < size && hours[daysworked])
	{
		count++;
		hours[count - 1] = daysworked;
	}
	cout << "What is your hourly pay rate? ";
	cin >> payrate;

	for (int index = 0; index < count; index++)
	{
		cout << "Enter hours worked for day "<< index+1<< ": ";
		cin >> hours[size];
	}
		

	for (count = 0; count < daysworked; count++)
	{
		totalhours = daysworked + hours[size];
		grosspay = totalhours * payrate;
	}

	cout << "Your gross pay will be: $";
	cout << grosspay << endl;
*/
	return 0;
}
Last edited on
hi,
here are your answers:
first, hours is not initialized and you are allocating memory to daysworked from uninitialized variable (default is 0)
second, hours is a single int, not an array to ints but daysworked is a pointer, so according to its size, it can be used like arrays
so, you should do daysworked[hours-1] (it starts from 0)
Topic archived. No new replies allowed.