Home > Net >  I got the following error : error: ‘name’ was not declared in this scope; did you mean ‘rename
I got the following error : error: ‘name’ was not declared in this scope; did you mean ‘rename

Time:06-19

im creating a sort of menu/list program using parallel array, functions and switch statements. this is the error im getting below.

main.cpp:42:23: error: ‘name’ was not declared in this scope; did you mean ‘rename’? 42 | getEmpDetails(name, surname, hoursWorked); | ^~~~ | rename

the error appears by case 'c'.

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


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


int main()
{
// declaring parrell arrays   
    string name[10]; //for storing Employee names
    string surname[10]; // for storing Employee surname
    int hoursWorked[10]; // for storing employee hours worked
    
    
    //calling the functions
    menuDisplay(); // 
    getEmpDetails(name,surname,hoursWorked); 
    
    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 'C':
        cout << "capture employee details" << endl;
        getEmpDetails(name, surname, hoursWorked);
        break;
        case 'L':
        cout << "list employee details" << endl;
        break;
        case 'A':
        cout << "All Employee Payslips" << endl;
        break;
        case 'S':
        cout <<"Single employee payslips" << endl;
        break;
        case 'E':
        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[],string surname[], int hoursWorked[]){
    //this function is for capturing employee details
    for (int x =0; x < 10; x  ){
        cout << "enter employee name" <<endl; // geting employee name
        cin >> name[x];
        cout << "enter employee surname" << endl; // getting employee surname
        cin >> surname[x];
        cout << "enter number of hours worked" << endl; // getting hours worked
        cin >> hoursWorked[x];
    }
    
}

CodePudding user response:

As the error log suggests, the variables on line 42 have not been declared and they are not in scope within menuDisplay(). One option would be to pass by value/reference to the function

CodePudding user response:

You have declared the arrays in main but you are trying to use them in menuDisplay where they are not declared. That's what the error is saying.

You could pass the arrays to menuDisplay as parameters exactly like you have done with getEmpDetails.

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

int main()
{
// declaring parrell arrays   
    string name[10]; //for storing Employee names
    string surname[10]; // for storing Employee surname
    int hoursWorked[10]; // for storing employee hours worked
    
    
    //calling the functions
    menuDisplay(name,surname,hoursWorked); // 
    getEmpDetails(name,surname,hoursWorked); 
    
    return 0;
    
}

void menuDisplay(string name[],string surname[], int hoursWorked[]){
    ...
}
  •  Tags:  
  • c
  • Related