尽管是一个项目欧拉程序,下面的代码实际上并不关心它了。 我要添加50 100 1位数字,而我在阵列加数[100] [50]分配每个号码的每个数字给一个元素。 然后我会单独添加了每个数字/位,并且延续额外的数字。 正在从命名的文本文件中读出的数字Input.txt
,它仅仅包含了所有的数字。 http://projecteuler.net/problem=13
遇到麻烦分配字符的字符串阵列的元件( string numbers[100][50]
从文件输入流)( <fstream>
)。 问题是在评论中更完整地描述:
“[在第一循环]这个循环分配一个编号以字符串数组中的每个串即使第二数目(50)不会做任何事情(它似乎是由的std :: string被覆盖;参见变量声明),它需要那里的循环工作同“逻辑”的循环;“J”没有做任何事情,但需要存在的循环工作“?
并且还(对于第二循环)“这个循环填补了‘加数[100] [50]’阵列从相应的字符串的数组元素。如果我尝试调用‘char_to_int()’与阵列”的数字[I] [j]的“编译器会抱怨输入是不是正确的数据类型。添加变量‘K’使得一个运行回路的工作,但最终崩溃在第二循环(使用”数字[i] [j ] [K]“)。所以,我想 ”char_to_int((编号[i] [j])。c_str())“,但是编译器抱怨 ”常量字符*“ 是与不相容的 ”焦炭“。添加指针做出决议问题( “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;
}
该代码还没有完成; 我决定不回事,因为我(显然)需要首先解决这个问题。