newtothis
newtothis

Reputation: 7

i got the following error in my code. can some help me fix it

im trying to use parallel arrays and function to get user input for this menu/list im working on. the program should accept the name, surname and hours worked in the same function. the error im facing is down below. please help me fix it

"main.cpp:38:28: error: cannot convert ‘std::string’ {aka ‘std::__cxx11::basic_string’} to ‘std::string*’ {aka ‘std::__cxx11::basic_string*’} "

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

//globl const for array size
const int SIZE = 10;

// functions 
void menuDisplay();
void getEmpDetails(string[], string[], int[]);

int main()
{
    // declaring parrell arrays   
    string name[SIZE]; //for storing Employee names
    string surname[SIZE]; // for storing Employee surname
    int hoursWorked[SIZE]; // for storing employee hours worked

   //calling the functions
    menuDisplay();
    getEmpDetails(name[SIZE], surname[SIZE], hoursWorked[SIZE]);

    return 0;
}

void menuDisplay() {
    char choice;

    do { //this makes the menu repeat itself

        cout << "[C]apture Employee details" << endl;
        cout << "[L]ist Employee details" << endl;
        cout << "[A]ll Employee Payslips" << endl;
        cout << "[S]ingle Employee Payslips" << endl;
        cout << "[E]xit" << endl;
        cin >> choice;

        switch (choice) {
        case 0:
            cout << "capture employee detail" << endl;
            break;
        case 1:
            cout << "list employee details" << endl;
            break;
        case 2:
            cout << "All Employee Payslips" << endl;
            break;
        case 3:
            cout << "Single employee payslips" << endl;
            break;
        case 4:
            cout << "Exit" << endl;

        }

    } while (choice == 'C' || choice == 'L' || choice == 'S' || choice == 'E' || choice == 'A'); //for selecting the right options on the menu
    cout << "invaild choice, please choice either C,L,A,S,E" << endl; // if the wrong option is selected this appears
}


void getEmpDetails(string name[SIZE], string surname[SIZE], int hoursWorked[SIZE]) {
    //this function is for capturing employee details
    for (int x = 0; x < SIZE; x++) {
        cout << "enter employee name" << endl;
        cin >> name[SIZE];
        cout << "enter employee surname" << endl;
        cin >> surname[SIZE];
        cout << "enter number of hours worked" << endl;
        cin >> hoursWorked[SIZE];
    }
}

Upvotes: 0

Views: 163

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118292

void getEmpDetails(string[],string[], int[]);

This function is declared as taking three pointers as parameters. In both C and C++ function parameters that get declares as arrays are actually pointers, this is really

void getEmpDetails(string *,string *, int *);

You attempt to call this function as follows:

 getEmpDetails(name[SIZE],surname[SIZE],hoursWorked[SIZE]);

This name is in array. If X is in array, in C and C++ X[n] means the nth value in the array. So the above code attempts to pass a single value in the array to this function. The SIZEth value. Which, to add insult to injury, doesn't even exist so that's undefined behavior right out of the gate.

Now, if you go and reread the error message it makes perfect sense. The compiler is complaining that you're trying to pass a string, that single value in the array, to a function that expects a string * parameter. That's obviously wrong.

Your obvious intent here is to pass the arrays, or pointers to the arrays, to this function, and not a single value from each array. If so, this is, simply:

 getEmpDetails(name, surname, hoursWorked);

This should answer the question of the compilation error. But that won't solve all of your problem. Looking at what's in this function:

    for (int x =0; x < SIZE; x++){
        cout << "enter employee name" <<endl;
        cin >> name[SIZE];

If you followed my explanation above, the problem here should now be obvious, as well as what it's obvious solution. Hint: name[SIZE] does not exist. Attempting to set it to anything will result in a crash. Ask yourself what exactly you want to happen here.

Upvotes: 1

Related Questions