0

I am new at c++ programming. In the code below, i wanted the user to enter the number of employees and the amount of sales for each employee. Then the program will write the corresponding amounts of sales for each employee.Although the compiler didn't give errors, it didn't work. Can you help me to find where the error is? Thanks in advance.

#include <iostream>
using namespace std;

int enterSales(int max){
    int sales[max];
    for (int idx=0;idx<max;idx++){
        cout<<"Enter the amount of sales for person #"<<idx<<": ";
        cin>>sales[idx];
    }
    return sales[max];
}

float showSalesComm(int max){
    int sales[]={enterSales(max)};
    for (int idx=0;idx<max;idx++){
        cout<<"The amount of sales for person #"<<idx<<": ";
        cout<<sales[idx]<<endl;
    }
    return 0;
}

int main () {

    int max;
    cout<<"Enter the number of employees.";
    cin>>max;
    showSalesComm(max); 

    return 0;
}
  • `int sales[max];` - Variable Length Arrays are *not* a standard C++ feature (although some compilers unfortunately accept them as an extension). You want to be using `std::vector`. Also, your function just returns `int` - how would you ever expect to be able to return an array (or `std::vector`) there? – Jesper Juhl Aug 26 '18 at 12:54
  • 1
    Enable compiler warnings. Disable compiler extensions. The compiler would have pointed out the mistake. – Eljay Aug 26 '18 at 12:56
  • 1
    Use `std::vector sales(max):` if you want your code to behave as you believe it should, and not have surprises because of pointer issues. Also, return a `std::vector`, not just an `int`. Also, `int sales[]={enterSales(max)};` then becomes `std::vector sales = enterSales(max);` – PaulMcKenzie Aug 26 '18 at 13:15

1 Answers1

1

You can use std::vector<int> instead of arrays. In C/C++ arrays decomposes into pointers while being passed in functions. Making things difficult.

Using std::vector<int> will take care of allocating and deleting and most importantly you can returns them from functions (copy construction) and no issues with temporary or what-so-ever.

Here is how to do it.

#include <iostream>
#include <vector>
using std::vector;
using std::cout;
using std::cin;
using std::endl;

vector<int> enterSales(int max){
    int temp;
    vector<int> a;
    for (int idx=0;idx<max;idx++){
        cout<<"Enter the amount of sales for person #"<<idx<<": ";
        cin>>temp;
        a.push_back(temp);
    }
    return a;
}

void showSalesComm(int max){
    vector<int> sales=enterSales(max);
    for (int idx=0;idx<max;idx++){
        cout<<"The amount of sales for person #"<<idx<<": ";
        cout<<sales[idx]<<endl;
    }
}

int main () {

    int max;
    cout<<"Enter the number of employees.";
    cin>>max;
    showSalesComm(max); 
    return 0;
}

Actually you have a lot of mistakes in your code. Returning temporary and Index out of bound etcetra. Disable compiler extension and it will show you the warnings.

coder3101
  • 3,920
  • 3
  • 24
  • 28