-3
// Assignment7.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
ifstream infile;
int index = 0;
string singleWord;

infile.open("text.txt");


while (!infile.eof()) //to figure out size of array
{
    infile >> singleWord;
    index++;
}

string* strings = new string[index];

infile.close(); //to restart the file at the first word
infile.open("text.txt");
int count = 0;

while (!infile.eof())      //if not at end of file, continue reading numbers
{
    infile >> strings[count];
    cout << strings[count] << " ";
    count++;
}
infile.close();       //close file

ofstream outfile;
outfile.open("textfixed.txt");
for (int i = 0; i < index; i++) {
    outfile << strings[i] << " ";

}
outfile.close();
delete[] strings;
strings = NULL;

return 0;
}

My program reads a textfile that has 6 sentences setup as such:

Sentence 1

Sentence 2

Sentence 3

Sentence 4

Sentence 5

Sentence 6

I'm learning how to play with strings, and I want to concatenate the 6 sentences so that in a new file(that my program would write to, called newtext.txt) would have it setup like: Sentence 1. Sentence 2. Sentence 3. Sentence 4. Sentence 5. Sentence 6. So for example, if the textfile contained something like:

I like to play soccer.

I enjoy eating apples.

I am 18 years old.

I watch baseball sometimes.

Cooking is a hobby of mine.

My birthday is in July.

My program would take the above^ and in a new file write it out as:

I like to play soccer. I enjoy eating apples. I am 18 years old. I watch baseball sometimes. Cooking is a hobby of mine. My birthday is in July.

I'm just not quite sure how I go about concatenating each word in order to setup as such. If anyone could point me into the right direction, that'd be appreciated. Thanks.

UPDATED OP WITH NEW CODE

I don't get any output from the newly updated code, it's supposed to cout() the new paragraph to console but when I run it the CMD is just black

Community
  • 1
  • 1
Xor
  • 41
  • 1
  • 6
  • 1
    Your first step is to figure out the `while (!infile.eof())` [is always a bug](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). You can start by fixing your code so it reads the file properly. – Sam Varshavchik Nov 18 '16 at 01:40
  • If you are using string, you can use '+' for concatenation. – Wasi Ahmad Nov 18 '16 at 01:41
  • @WasiAhmad I'm trying to figure out how though using the words taken into the array, would it be each index that i'd have to concatenate? – Xor Nov 18 '16 at 01:46
  • @Xor i would suggest you to read files line by line and then do concatenation. You can do the concatenation character by character as well, either way it should work! – Wasi Ahmad Nov 18 '16 at 01:50

1 Answers1

0

I think maybe you're making it too complicated. As you read each line in, write it out to the out file, just don't add a new line until it's done:

int main()
{
    ifstream inFile( "Text1.txt" );
    ofstream outFile( "Text2.txt" );
    string sentence = "";
    while ( getline( inFile , sentence ) )
    {
            outFile << " " << sentence;
            cout << " " << sentence;
    }
    outfile << '\n';
    cout << "\nPress enter to continue";
    while ( cin.get() != 10 )
    {

    }
    return 0;
}
tinstaafl
  • 6,908
  • 2
  • 15
  • 22