我想用字符数组中的字符(但我使用字符串构建它)来遍历并打印字符,如果字符==迭代字符串。但是,第一个打印的字符与所需的字符串不同。最后它有时会增加一个意想不到的字符。迭代通过char字符数组出错
预期输出:
我是史蒂夫
输出:
〜我Stevef
我试图让控制台打印输出与迭代的字符串,我的意思是这样的:
A
第一,控制台打印A
,然后我做回车,你可以在网上看到14和转向B
,直到输出所需字符串的第一个字符。 如果输出的字符与字符相同,则将其放入数组cmem
。并继续迭代第二个角色,依此类推。
下面的代码:
#include <iostream>
using namespace std;
//starts here!
int main(){
char hlw[] = "I am Steve.";
char j; //to be iterated.
int hlwstrlen = strlen(hlw);
char cmem[hlwstrlen]; //memorize the correct char.
char convertedChar; //converted char
//iterating begin
for (int ch = 0; ch <= hlwstrlen; ch++){
for (int aschr = 32; aschr <= 126; aschr++){
convertedChar = static_cast<char>(aschr); //this converts to an ascii from an int.
cout << convertedChar << "\r";
if(convertedChar == hlw[ch]){
cout << convertedChar << "\r";
cmem[ch] = convertedChar;
for(int i = 0; i <= ch; i++){
cout << cmem[i];
}
continue;
}
}
}
cout << endl;
return 0;
}
注:对不起,如果我不能完全格式化代码。我用我的手机打字。
忽略最后三种情况,现在我看到您对注释做出响应,说您要对输出进行流式处理,所以不需要使用您使用的临时字符串。 –