2
好日子大家,为什么我的代码只能正常工作一次?
我不得不做出这样的假设是做如下的程序:
1)要求用户输入一个字符串。
2)要求用户输入一个整数(我们称之为'n')。
3)将字符串中的每个字符替换为字母前面的字符'n'。
4)将新字符串打印到控制台。
例如: 如果string = abc AND integer = 1,结果将是bcd。
如果string = Hello和integer = 4,结果将是Lipps。
if string = Welcome-2-C++ AND integer = 13结果将是Jrypbzr-2-P ++(特殊字符,如$,+,/保持不变)。
我已经写了一些代码,正常工作:
(code removed)
除了它只能一次。这就是控制台的样子:
Please insert a string to convert:
abc
Please enter the modification integer:
2
The resulting string is: cde
Please insert a string to convert:
abc
Please enter the modification integer:
2
The resulting string is:
Please insert a string to convert:
正如你所看到的,第二次程序运行时,没有结果。
为什么我的程序在第一次运行时才能正常工作?
P.S.我已经做了一些我自己的调试,看起来程序运行第二次跳过了“for循环”。 (?)
编辑:我认为这可能与内存分配有关?