gpt4 book ai didi

C++ 字符串不会正确输出值

转载 作者:行者123 更新时间:2023-11-28 06:03:27 26 4
gpt4 key购买 nike

该程序的目标是创建一个函数 (combineStr),该函数将字符串连接多次。

#include <iostream>
using namespace std;

string combineStr(string input, int times) {
string output = "";
for(int i = 0; i < times; i++){
output += times;
}
return output;
}

int main(){
string input;
int times;
cout << "Enter a string: ";
cin >> input;

cout << "Enter a number of times: ";
cin >> times;
if(times == 0){
return 0;
}

string output = combineStr(input,times);

cout << "The resulting string is: " << output << endl;
}

出于某种原因,当我编译和运行程序时,它只是输出“结果字符串是:”而没有重复的字符串。帮忙?

最佳答案

修改循环内的语句。

output += times; to output += input;

string combineStr(string input, int times) {
string output = "";
for(int i = 0; i < times; i++){
output += input;
}
}

关于C++ 字符串不会正确输出值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32836919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com