English 中文(简体)
B. 改变方案结果的教义
原标题:presence of cout changing the result of the program
  • 时间:2012-01-12 06:04:13
  •  标签:
  • c++
  • cout

我正在撰写一份方案,其中总结了一份文本档案中首批50位数的十位数。 容易,我得到正确答案,但我试图理解一些似乎正在经历的令人厌恶的事情。 第一个计算线如以下方案产出所示:

8549904804355727945614717320-7-60-4-3-2-7-3-4-8-7-7-6-1-840-9-5-2-3-39622981389563771795892386478402037366780930326737553

在对该项权利的意见做出修改后,它就产生了以下结果:

97414719961102146325906539660-4-7-2-4-4-3-4-8-2-8-3-8-2-870-8-5-2-3-39622981389563771795892386478402037366780930326737553

I m having trouble understanding
1) how what I output could somehow affect the future values it output.
2) How the dashes get into the program.

注:产出是所有指示数的总和。

char file[] = "nums.txt";
ifstream ss(file);
string iString;
string sarr[100];
int sum[100];

while(getline(ss, iString,  
 )) {
  sarr[i] = iString;
  i++;
}
ss.close();

cout << sarr[0] << endl << endl; //or cout<<(int)sarr[0][3]-48<<endl<<endl;
for(int i = 99; i >= 0; i--) {
  sum[i] = 0;
  for(int j = 0; j < 100; j++) {
    sum[i] += (int)sarr[j][i];
  }
  sum[i] -= 4800;//since 100 strings and  0  in ascii is 48 subtract 48*100
}

for(int i = 99; i > 0; i--) {
  sum[i-1] += sum[i] / 10;
  sum[i] = sum[i] % 10;
  cout << sum[i];
}
cout << sum[0] << endl << endl;
最佳回答

您说,文本档案的第一行是371072875339021027987998220837590246510135740250<>。 具有50种长的特性,但在此

for(int i = 99; i >= 0; i--) {
  sum[i] = 0;
  for(int j = 0; j < 100; j++) {
    sum[i] += (int)sarr[j][i];
  }
  sum[i] -= 4800;//since 100 strings and  0  in ascii is 48 subtract 48*100
}

您正在使用<代码>i,从99到0,并使用<代码>i,以指数表示。 你的指数偏离了界限,造成不明确的行为。 这是使用硬编码数值而不是使用数据来确定 lo体界限的危险。

当像这种情况这样随机发生时,你与阵列重新做许多工作时,你首先应当想到的是,在某个地方,就将目标排在外。 这可能是联邦调查委员会最经常的原因。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签