简体   繁体   English

我收到以下错误:错误:“名称”未在此范围内声明; 你的意思是'重命名

[英]I got the following error : error: ‘name’ was not declared in this scope; did you mean ‘rename

im creating a sort of menu/list program using parallel array, functions and switch statements.我使用并行数组、函数和 switch 语句创建了一种菜单/列表程序。 this is the error im getting below.这是我遇到的错误。

main.cpp:42:23: error: 'name' was not declared in this scope; main.cpp:42:23:错误:“名称”未在此范围内声明; did you mean 'rename'?您指的是 'rename' 吗? 42 | 42 | getEmpDetails(name, surname, hoursWorked); getEmpDetails(姓名,姓氏,工作时间); | | ^~~~ | ^~~~ | rename改名

the error appears by case 'c'.错误以大小写“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];
    }
    
}

You have declared the arrays in main but you are trying to use them in menuDisplay where they are not declared.您已在main中声明了数组,但您试图在未声明它们的menuDisplay中使用它们。 That's what the error is saying.这就是错误所说的。

You could pass the arrays to menuDisplay as parameters exactly like you have done with getEmpDetails .您可以将数组作为参数传递给menuDisplay ,就像您使用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[]){
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM