display content of array from function call

I want to display the contents of array arr[num]. Whenever I try to, I get funky out of bound negative values displayed where I have the values stored from the payRate() function.
.
.
the for(...) loop at the end of main() is what causes it. I need help with displaying this information to the user of my program. I believe a pointer is required, I don't remember how to implement it into my program however.


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

//this program will give information of all employee's pay rate all at once or individuals only
//
//
#include <iostream>

//function for calculating pay rate
double payRate(int n){
  double hour;
  double wage;
  const double tax = 0.765;
  double temp;
  double arr[n];

  std::cout << "You have: " << n << " employees.\n";

//loop for filling up array
  for (int x = 0; x < n; x++){
    std::cout << " what\'s #: " << x+1 << " employee's hour and wage?\n";
    std::cin  >> hour >> wage;
    temp = (hour * wage) * tax;

    std::cout << temp << " is the wage of employee #" << x+1 << "\n";
    arr[x] = temp;
  }

  for (int c = 0; c < n; c++){
    std::cout << "\n" << arr[c] << " is value of " << c+1 << "\n";
   }
  //return arr[n]; //old
}


int main(){

  int num;

  std::cout << "Enter how many workers you have: ";
  std::cin  >> num;

  payRate(num);
  double arr[num];

  //arr[num];

  for (int c = 0; c < num; c++){
    std::cout << "\n" << arr[c] << "\n"; //problem seemingly occurs here
  }
  return 0;
On lines 46-48 you are printing contents of array declared on line 42.
This array is not filled at any time. The function payRate is filling array that you have declared on line 13; this is a local array to the function.

The values filled in the local array in the function wont reflect in main hence the loop at 46-48 prints garbage/problem occurs.

The loop at 27-30 should print correct values.

To print correct values in main

1) change signature of payRate to void payRate(double *arr)
2) Delete code on line 13
3) Move code on line 42 to line 40
4) change call to payRate as payRate(arr);

Also, I believe that declaring array using a non-constant is non-standard extension. If you need to use dynamic array then use new instead (double *arr = new double[num];), at line 40
Hi,

Avoid using new altogether, use a std::vector instead.

The payRate function doesn't return a value: it should.
This might be assignment where @PopRockCandy needs to use arrays.
The return type of payRate is suggested as void, hence doesn't need to return a value.

Before going to vectors, and if C++14~ capable compiler is available, using unique_ptr and make_unique might be a good learning step.

In any case if he says he doesn't know how to use pointers then I dont know if he is aware of STL/vectors.
@codewalker

This might be assignment where @PopRockCandy needs to use arrays.


That's a good point, although it's a shame teachers still seem to do this backwards: teaching C before C++.

The return type of payRate is suggested as void, hence doesn't need to return a value.


My bad :+)

Before going to vectors, and if C++14~ capable compiler is available, using unique_ptr and make_unique might be a good learning step.


Really? std::vector is really useful, and above all easy for a beginner. Why would you recommend confusing beginners with having to learn about move semantics? Or even pointers at all? Have a look at the video in this post:
http://www.cplusplus.com/forum/lounge/211316/

In any case if he says he doesn't know how to use pointers then I dont know if he is aware of STL/vectors.


Why should knowledge of pointers be a requirement for using std::vector ?
@TheIdeasMan

For last two points:
Your points are valid but thats not how they teach C++, in my experience (unfortunately?!)

Usual course is to teach arrays then double dimension arrays then pointers etc and then in (so called) advanced course they move on to STL and standard containers.

My point is that nobody should teach to practically use naked new (and delete). They should theoretically teach what new and delete are and them immediately teach to use unique_ptr (or shared_ptr). Again they do not teach this but teach only new/delete. So not knowing what (s)he knows I suggested those changes.
@codewalker, @TheIdeasMan

Thank you for your input. This is not a class assignment, but a review for my upcoming semester. I have learned about STL/Vectors and pointers. In my lecture, we have only touched on the idea and basic implementation of vectors.

Codewalker's route of learning C++ is accurate, I have never heard of unique/shared_ptr. Maybe I have, but with a different name.
Topic archived. No new replies allowed.