2012-04-04 120 views
0

尽管是一个项目欧拉程序,下面的代码实际上并不关心它。我想添加50个100位数字,并将每个数字的每个数字分配给数组addends [100] [50]中的一个元素。然后,我会分别将每个数字/位置相加,并携带多余的数字。这些数字正在从名为Input.txt的文本文件中读取,它只包含所有数字。 http://projecteuler.net/problem=13std :: string数组元素访问

我无法从文件输入流(<fstream>)将字符分配给字符串数组(string numbers[100][50])的元素。这个问题在评论中有更完整的描述:

“[for the 1st loop]这个循环为字符串数组中的每个字符串赋一个数字,尽管第二个数字(50)没有任何作用被std :: string覆盖;见变量声明),它需要在循环中才能工作,循环的逻辑是相同的;“j”除了循环之外什么都不需要,工作?”如果我尝试使用数组“”来调用“char_to_int()”,那么我们可以使用数组“数字[i] [j]“,编译器会抱怨输入的数据类型不正确,添加一个变量”k“会使循环工作一次,但最终会在第二个循环中崩溃(使用”所以我尝试了“char_to_int((numbers [i] [j])。c_str())”,但编译器抱怨“const char *”与“char”不兼容。添加一个指针可以解决问题(“char_to_int(*((numbers [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; 
} 

该代码没有完成;我决定反对,因为我(显然)需要首先解决这个问题。

+0

是否有理由不使用像矢量,地图等C++ STL而不是数组(并在以后使用迭代器访问它们)? – enthusiasticgeek 2013-12-03 12:49:39

回答

3

它编译并除去stdafx.h包括和限定char_to_int

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])); 

运行良好。

A std::string本身包含一个字符数组,因此您只需要一个std::string s的一维数组。然后,可以通过[]索引访问一个字符串的字符,

numbers[i][j] 

得到的第i个字符串的第j个字符(字节,而)阵列numbers英寸

+0

谢谢!它确实有效。但是,当我尝试查看数组中第一个以外的字符串时,我的“watch”窗口不显示它们。我正在使用Visual Studio Express 2010. – 2012-04-05 00:27:04

+0

这很奇怪。我可以''数字[我] << endl;'没有问题(但后来,我在Linux上使用gcc)。你如何尝试显示字符串? – 2012-04-05 00:30:53

+0

在“调试模式”中,Visual Studio告诉我(通过监视窗口)数字[0]是3,数字[1]是7,等等。 – 2012-04-05 02:38:28