To me part of your instructions don't make a lot of sense. Part of the purpose of stringstreams is to parse strings into other numbers and strings so that things like atoi() are not required. Also in C++ it is considered a better practice to declare your variables closer to first use instead of in one large lump at the beginning of the program.
#include <fstream>
#include <iostream>
#include <sstream>
usingnamespace std;
int main() {
ifstream input("input.txt");
if(!input)
{
std::cerr << "Error opening input file.\n";
return(1);
}
ofstream output("output.txt");
if(!output)
{
std::cerr << "Error opening output file.\n";
return(2);
}
string line;
// Read the entire line into a string.
while (getline(input, line)) {
istringstream is(line);
int numbers[3];
char delimiter;
// Parse the string with a stringstream.
is >> numbers[0];
for(int i = 1; i < 3; ++i){
is >> delimiter >> numbers[i];
}
string text;
// Get the text line from the file.
getline(input, text);
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += numbers[i];
}
// Create a stringstream to hold the repeated text.
ostringstream os;
os << text;
for (int i = 1; i < sum; i++) {
os << ", " << text;
}
// Output the stringstream to the file and the console.
cout << os.str() << endl;
output << os.str() << endl;
}
return 0;
}