C ++将值传递给函数中的二维字符数组
C++ Passing values to 2D char array in a function
我正在尝试使用函数对充满单词的字符数组进行排序。我目前遇到的问题是在我的 sortNames 函数中出现错误,"expression must be a modifiable lvalue" 在下面的部分
hold = nameArr[ii];
nameArr[ii] = nameArr[jj];
nameArr[jj] = hold;
我猜这是因为我出于某种原因试图通过数组传递值。我在理解引用和指针等方面苦苦挣扎,我想这也伤害了我。任何帮助都会很棒,提前谢谢你。
这是我当前的代码...
#include <iostream>
#include <string>
using namespace std;
char nameArr[20][15]; // array to store the 20 values
int val = 0; // variable to pass values to the array
int x = 0; // loop counter outside functions
//Function prototypes
void getNames(char (&nameArr)[20][15], int &val);
void sortNames( char(&nameArr)[20][15]);
//getNames Function
void getNames(char (&nameArr)[20][15], int &val)
{
int i = 0; // loop counter
cout << "Awesome, now lets input those names...\n" << endl;
for (i = 0; i < val; i++)
{
cout << "\nNAME " << i+1 << ": " << ' ';
cin >> nameArr[i];
}
cout << "\n\n\nThese are the names that you inserted:\n" << endl;
for (i = 0; i < val; i++)
{
cout << nameArr[i] << "\n" << endl;
}
}
// sortNames function
void sortNames( char(&nameArr)[20][15])
{
int n = 15; // max length of word
int ii = 0; // loop counter
int jj = 0; // other counter
string hold; // holding array
for (int ii = 0 ; ii < n ; ii++)
{
for (int jj = ii + 1; jj < n; jj++)
{
if (nameArr[ii] > nameArr[jj])
{
hold = nameArr[ii];
nameArr[ii] = nameArr[jj];
nameArr[jj] = hold;
}
}
}
}
int main()
{
cout << "NAME SORTER!\n\nPlease enter in the amount of names you wish to enter: " << ' ';
cin >> val;
getNames(nameArr, val);
cout << "\n\n\nAlright, lets sort now..." << endl;
sortNames(nameArr);
cout << "\nHere are the results:\n" << endl;
for (x = 0; x < val; x++)
{
cout << nameArr[x] << "\n" << endl;
}
system("pause");
}
这里的主要问题是您试图对两个固定大小的数组使用赋值运算符,这是不合法的。考虑以下代码:
int a[2] = {0, 0};
int b[2] = {1, 1};
a = b;
这给出了与您遇到的相同的错误。在您提到的行中,您正在对 char[15]
数组做同样的事情。
要解决您的问题,您需要使用指针分配 char
数组 dynamically/work,或者更简单的解决方案是将 char[][]
数组更改为 string[]
数组。
也就是说,您可以在这里清理很多东西:
- 您有一些全局声明的变量,它们只能在 main 或 lower 中定义
- 您可以在
for
循环内而不是事先声明循环计数器,就像在 sortNames
函数中所做的那样
- 在
sortNames
中你声明了一些变量两次
我会在 dwcanilla 的回答中添加一些内容。
您需要将函数原型和 headers 更改为更像这样的东西:
void getNames(char ** & arr, int val);
void sortNames(char ** & arr);
这意味着该函数接受对 c-strings 数组的引用;也就是说,当您在函数中使用数组时,您正在修改您传递的实际数组,而不仅仅是一个副本。此外,我认为您只需为 getNames 按值传递整数就可以了。
其次,全局变量通常不是一个好主意。由于您可以将数组引用直接传递给您的函数,因此您可能希望在 main 中声明 nameArr 和其他全局变量。
第三,在 getNames 中,您将无法使用 cin 直接分配您的 c-strings。
编辑:这是一个更好的方法——
getting console input for Cstrings
最后,< 运算符在 c-strings 上的工作方式与您在排序函数中使用它的方式不同。请改用 strcmp()(并确保包含 cstring header):
if(strcmp(arr[ii], arr[jj]) > 0)
我正在尝试使用函数对充满单词的字符数组进行排序。我目前遇到的问题是在我的 sortNames 函数中出现错误,"expression must be a modifiable lvalue" 在下面的部分
hold = nameArr[ii];
nameArr[ii] = nameArr[jj];
nameArr[jj] = hold;
我猜这是因为我出于某种原因试图通过数组传递值。我在理解引用和指针等方面苦苦挣扎,我想这也伤害了我。任何帮助都会很棒,提前谢谢你。
这是我当前的代码...
#include <iostream>
#include <string>
using namespace std;
char nameArr[20][15]; // array to store the 20 values
int val = 0; // variable to pass values to the array
int x = 0; // loop counter outside functions
//Function prototypes
void getNames(char (&nameArr)[20][15], int &val);
void sortNames( char(&nameArr)[20][15]);
//getNames Function
void getNames(char (&nameArr)[20][15], int &val)
{
int i = 0; // loop counter
cout << "Awesome, now lets input those names...\n" << endl;
for (i = 0; i < val; i++)
{
cout << "\nNAME " << i+1 << ": " << ' ';
cin >> nameArr[i];
}
cout << "\n\n\nThese are the names that you inserted:\n" << endl;
for (i = 0; i < val; i++)
{
cout << nameArr[i] << "\n" << endl;
}
}
// sortNames function
void sortNames( char(&nameArr)[20][15])
{
int n = 15; // max length of word
int ii = 0; // loop counter
int jj = 0; // other counter
string hold; // holding array
for (int ii = 0 ; ii < n ; ii++)
{
for (int jj = ii + 1; jj < n; jj++)
{
if (nameArr[ii] > nameArr[jj])
{
hold = nameArr[ii];
nameArr[ii] = nameArr[jj];
nameArr[jj] = hold;
}
}
}
}
int main()
{
cout << "NAME SORTER!\n\nPlease enter in the amount of names you wish to enter: " << ' ';
cin >> val;
getNames(nameArr, val);
cout << "\n\n\nAlright, lets sort now..." << endl;
sortNames(nameArr);
cout << "\nHere are the results:\n" << endl;
for (x = 0; x < val; x++)
{
cout << nameArr[x] << "\n" << endl;
}
system("pause");
}
这里的主要问题是您试图对两个固定大小的数组使用赋值运算符,这是不合法的。考虑以下代码:
int a[2] = {0, 0};
int b[2] = {1, 1};
a = b;
这给出了与您遇到的相同的错误。在您提到的行中,您正在对 char[15]
数组做同样的事情。
要解决您的问题,您需要使用指针分配 char
数组 dynamically/work,或者更简单的解决方案是将 char[][]
数组更改为 string[]
数组。
也就是说,您可以在这里清理很多东西:
- 您有一些全局声明的变量,它们只能在 main 或 lower 中定义
- 您可以在
for
循环内而不是事先声明循环计数器,就像在sortNames
函数中所做的那样 - 在
sortNames
中你声明了一些变量两次
我会在 dwcanilla 的回答中添加一些内容。
您需要将函数原型和 headers 更改为更像这样的东西:
void getNames(char ** & arr, int val);
void sortNames(char ** & arr);
这意味着该函数接受对 c-strings 数组的引用;也就是说,当您在函数中使用数组时,您正在修改您传递的实际数组,而不仅仅是一个副本。此外,我认为您只需为 getNames 按值传递整数就可以了。
其次,全局变量通常不是一个好主意。由于您可以将数组引用直接传递给您的函数,因此您可能希望在 main 中声明 nameArr 和其他全局变量。
第三,在 getNames 中,您将无法使用 cin 直接分配您的 c-strings。
编辑:这是一个更好的方法—— getting console input for Cstrings
最后,< 运算符在 c-strings 上的工作方式与您在排序函数中使用它的方式不同。请改用 strcmp()(并确保包含 cstring header):
if(strcmp(arr[ii], arr[jj]) > 0)