Caleb Moriarty
Caleb Moriarty

Reputation: 31

C++ string wont output value properly

The goal of this program is to create a function (combineStr) that concatenates a string by a number of times.

#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;
}

For some reason when I compile and run the program, it simply outputs "The resulting string is: " without the repeated string. Help?

Upvotes: 0

Views: 75

Answers (1)

Gangadhar
Gangadhar

Reputation: 10516

Modify the statement inside loop.

output += times; to output += input;

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

Upvotes: 1

Related Questions