Writing a program that reads a set of strings into an array, and takes all the characters until whitespace occurs. Also, the strings are being read from the last to the first one from the array. My output looks really strange when I compile the code and read the output text file. Is there an error within the code here?
#include <iostream>
#include <stream>
using namespace std;
string substring(string a, int b, int c) {
string result = "";
for(int i = b; i<b+c; i++) {
result+=a[i];
}
return result;
}
int main() {
ifstream in_stream("HW3Test.txt");
ofstream output_stream("HW3output.txt");
string result[100];
int i=0;
while(!in_stream.eof()) {
getline(in_stream, result[i]);
i++;
}
for(int j = i; j>=0; j--) {
int counter1 = 0;
while(result[j][counter1]!=' ') {
counter1++;
}
output_stream<<substring(result[j], 0, counter1);
}
output_stream.close();
in_stream.close();
return 0;
}