cout 的存在改变了程序的结果
我正在编写一个程序,对文本文件中的 100 个 50 位数字的前十位数字进行求和。很简单,我得到了正确的答案,但我试图理解一些似乎正在发生的奇怪的事情。当第一个 cout 行出现在程序输出下方时:
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
我无法理解
1)我输出的内容如何以某种方式影响它输出的未来值。
2)破折号如何进入程序。
注意:输出是所有字符串反向求和。
char file[] = "nums.txt";
ifstream ss(file);
string iString;
string sarr[100];
int sum[100];
while(getline(ss, iString, '\n')) {
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;
I am writing a program which sums the first ten digits of one-hundred 50 digit numbers which are in a text file. Easy enough, and I got the right answer, but I'm trying to understand some weird things that appear to be going on. When the first cout line is included as it appears below the program outputs:
8549904804355727945614717320-7-60-4-3-2-7-3-4-8-7-7-6-1-840-9-5-2-3-39622981389563771795892386478402037366780930326737553
When it is changed to the comment on the right of it, it outputs:
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.
Note: the output is the sum of all the strings in reverse.
char file[] = "nums.txt";
ifstream ss(file);
string iString;
string sarr[100];
int sum[100];
while(getline(ss, iString, '\n')) {
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您说文本文件的第一行是
37107287533902102798797998220837590246510135740250
,长度为 50 个字符,但在这里您正在循环使用
i
从 99 到 0 并使用i
来索引该字符串。您的索引超出范围并导致未定义的行为。这是使用硬编码值而不是使用数据来确定循环边界的危险。当发生类似这样的随机事情并且您正在对数组进行大量工作时,您应该查找的第一件事是索引超出某处的范围。这可能是 UB 最常见的原因。
You say the first line of your text file is
37107287533902102798797998220837590246510135740250
, which is 50 characters long, but hereYou are looping with
i
going from 99 to 0 and usingi
to index that string. Your indices are going out of bounds and causing undefined behaviour. This is the danger of using hardcoded values instead of using the data to determine the bounds of the loop.When random things like this happen and you're doing a lot of work with arrays, the first thing you should look for is indexing out of bounds somewhere. That is probably the most frequent cause of UB.