[英]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.我正在尝试使用并行 arrays 和 function 来获取我正在处理的此菜单/列表的用户输入。 the program should accept the name, surname and hours worked in the same function. the error im facing is down below.该程序应接受在同一个 function 中的姓名和工作时间。我面临的错误在下面。 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*'} " “main.cpp:38:28: 错误:无法将‘std::string’{又名‘std::__cxx11::basic_string’}转换为‘std::string*’{又名‘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];
}
}
void getEmpDetails(string[],string[], int[]);
This function is declared as taking three pointers as parameters.这个 function 被声明为以三个指针为参数。 In both C and C++ function parameters that get declares as arrays are actually pointers, this is really在 C 和 C++ function 中声明为 arrays 的参数实际上是指针,这真的是
void getEmpDetails(string *,string *, int *);
You attempt to call this function as follows:您尝试按如下方式调用此 function:
getEmpDetails(name[SIZE],surname[SIZE],hoursWorked[SIZE]);
This name
is in array.这个name
在数组中。 If X
is in array, in C and C++ X[n]
means the nth
value in the array.如果X
在数组中,则在 C 和 C++ 中X[n]
表示数组中的nth
值。 So the above code attempts to pass a single value in the array to this function. The SIZE
th value.所以上面的代码试图将数组中的单个值传递给这个 function。第SIZE
个值。 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.现在,如果您输入 go 并重新阅读错误消息,它就很有意义了。 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.编译器抱怨您试图将数组中的单个值string
传递给需要string *
参数的 function。 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.您在这里的明显意图是将 arrays 或指向 arrays 的指针传递给这个 function,而不是每个数组中的单个值。 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:查看这个 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.提示: name[SIZE]
不存在。 Attempting to set it to anything will result in a crash.尝试将其设置为任何值都会导致崩溃。 Ask yourself what exactly you want to happen here.问问自己,你到底想在这里发生什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.