Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 字符串数组元素访问_C++_Visual Studio 2010_Multidimensional Array_Stdstring - Fatal编程技术网

C++ 字符串数组元素访问

C++ 字符串数组元素访问,c++,visual-studio-2010,multidimensional-array,stdstring,C++,Visual Studio 2010,Multidimensional Array,Stdstring,尽管是一个Project Euler程序,但下面的代码实际上并不太关心它。我想加上50个100位数字,并将每个数字的每一位数字分配给数组中的一个元素加数[100][50]。然后,我将每个数字/位置分别相加,并将多余的数字带入。这些数字是从名为Input.txt的文本文件中读取的,它只包含所有数字 我无法从文件输入流()将字符分配给字符串数组(字符串编号[100][50])的元素。注释中对该问题进行了更完整的描述: “[对于第一个循环]此循环为字符串数组中的每个字符串分配一个数字。即使第二个数字(

尽管是一个Project Euler程序,但下面的代码实际上并不太关心它。我想加上50个100位数字,并将每个数字的每一位数字分配给数组中的一个元素加数[100][50]。然后,我将每个数字/位置分别相加,并将多余的数字带入。这些数字是从名为
Input.txt
的文本文件中读取的,它只包含所有数字

我无法从文件输入流(
)将字符分配给字符串数组(
字符串编号[100][50]
)的元素。注释中对该问题进行了更完整的描述:

“[对于第一个循环]此循环为字符串数组中的每个字符串分配一个数字。即使第二个数字(50)没有任何作用(它似乎被std::string覆盖;请参阅变量声明),它也需要在那里才能使循环工作。循环的“逻辑”相同;“j”没有任何作用,但需要在那里才能使循环工作?”

而且,(对于第二个循环)“这个循环从对应的字符串数组元素中填充“addends[100][50]”数组。如果我试图用数组“numbers[I][j]”调用“char_to_int()”,编译器会抱怨输入的数据类型不正确。添加变量“k使循环运行一次,但最终在第二次循环中崩溃(使用“number[i][j][k]”)。所以我尝试了“char_to_int((数字[I][j]).c_str())”,但编译器抱怨“const char*”与“char”不兼容。添加一个指针可以解决这个问题(“char_to_int(*((数字[i][j]).c_str())”),但该程序稍后仍会崩溃。“为了提高可读性,我取出了一些无关紧要的代码

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int char_to_int(char chInput);

int main()
{

    int placeholder;    //so console doesn't close immediately upon finish
    int sum[102] = {0}; // 100+2, 100 places + 2 places from carrying over
    int addends[100][50] = {0};
    string numbers[100][50];
    ifstream input("Input.txt");

    /* This loop assigns a number to every string in the string array. Even
     * though the second number (50) doesn't do anything (it seems to be
     * overridden by std::string; see variable declaration), it needs to be
     * there for the loop to work. Same "logic" for the loop; "j" doesn't do
     * anything but needs to be there??? Confused :-\
     */
    for (int i = 0; i < 100; i++)
        for (int j = 0; j < 1; j++)
            getline(input, numbers[i][j]);

    /* This loop fills in the "addends[100][50]" array from the corresponding
     * string array element. If I try to call "char_to_int()" with the array
     * "numbers[i][j]", the compliler complains that the input isn't of the
     * right data type. Adding a variable "k" makes the loop work for one run,
     * but eventually crashes on the second loop (using "numbers[i][j][k]").
     * So I tried "char_to_int((numbers[i][j]).c_str())", but the compiler
     * complains that "const char *" is incompatible with "char". Adding a
     * pointer resolves the issue ("char_to_int( *( (numbers[i][j]).c_str() ) )"),
     * but the program still crashes on the second loop through.
     */
    for (int i = 0; i < 100; i++)
        for (int j = 0; j < 50; j++)
            for (int k = 0; k < 1; k++) //used when the variable "k" was being used
                addends[i][j] = char_to_int( (numbers[i][j]).c_str() );

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int char_至int(char chInput);
int main()
{
int占位符;//因此控制台不会在完成后立即关闭
int sum[102]={0};//100+2100位+2位结转
整数加数[100][50]={0};
字符串编号[100][50];
ifstream输入(“input.txt”);
/*此循环为字符串数组中的每个字符串分配一个数字。偶数
*虽然第二个数字(50)没有任何作用(似乎是
*由std::string覆盖;请参见变量声明),它需要
*循环的“逻辑”是一样的,“j”不起作用
*除了需要在那里以外的任何东西???困惑:-\
*/
对于(int i=0;i<100;i++)
对于(int j=0;j<1;j++)
getline(输入,数字[i][j]);
/*该循环从相应的数组填充“addends[100][50]”数组
*字符串数组元素。如果我尝试用数组调用“char\u to_int()”
*“数字[i][j]”,compliler抱怨输入不符合要求
*正确的数据类型。添加变量“k”使循环运行一次,
*但最终在第二个循环中崩溃(使用“数字[i][j][k]”)。
*所以我尝试了“char_to_int((数字[I][j]).c_str()”,但是编译器
*投诉“const char*”与“char”不兼容。添加
*指针解决了这个问题(“char_to_int(*(数字[i][j]).c_str())”,
*但是程序在第二个循环中仍然崩溃。
*/
对于(int i=0;i<100;i++)
对于(int j=0;j<50;j++)
for(int k=0;k<1;k++)//在使用变量“k”时使用
加数[i][j]=char_to_int((数字[i][j]).c_str());
返回0;
}

代码还没有完成;我决定不继续,因为我(显然)需要先解决这个问题。

它可以编译并运行良好

string numbers[100];

for (int i = 0; i < 100; i++)
        getline(input, numbers[i]);

for (int i = 0; i < 100; i++)
    for (int j = 0; j < 50; j++)
            addends[i][j] = char_to_int( (numbers[i][j]));

获取数组
numbers

中第i个字符串的第j个字符(而不是字节),该数组可以编译并正常运行

string numbers[100];

for (int i = 0; i < 100; i++)
        getline(input, numbers[i]);

for (int i = 0; i < 100; i++)
    for (int j = 0; j < 50; j++)
            addends[i][j] = char_to_int( (numbers[i][j]));

获取数组中第i个字符串的第j个字符(而不是字节)。谢谢!它确实有效。但是,当我尝试查看数组中第一个字符串以外的字符串时,我的“watch”窗口不会显示它们。我使用的是Visual Studio Express 2010。这很奇怪。Visual Studio告诉我,我可以在“调试模式”下查看字符串(通过观察窗口)数字[0]是
3
number[1]
7
,依此类推。嗯,每个
number[i]
应该有50位长,因为它是
getline()的结果
。如果
不能,它会显示什么?谢谢!它确实有效。但是,当我尝试查看数组中第一个字符串以外的字符串时,我的“watch”窗口不会显示它们。我使用的是Visual Studio Express 2010。这很奇怪。我可以
在“调试模式”下不能显示,Visual Studio(通过watch窗口)告诉我
数字[0]<代码> >代码> 3 < /COD>,<代码>数字(1)< /> >代码> 7代码>,等等。每个代码< >数字[i] <代码>应该是50位数,因为它是<代码> GETLILE()/CUT>的结果。如果你的代码“CUT”有理由不使用C++、STL之类的向量、地图等来代替数组(稍后使用迭代器访问它们),它会显示什么?有没有理由不使用像向量、地图之类的C++ STL来代替数组(稍后使用迭代器访问它们)?