C++ infile problem

My problem is simple - read an input file that has 3 lines of text : This is a test - This is also a test -- This is just another test - each on its own line. Then I need to read through the strings and for each lowercase t change it to upper. I get the code to work for the most part - the only problem I have now is if I run it the way the code is at this point, it prints 15 lines for each , I cannot get the program to print to the screen or the output file each having its own line.


#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <algorithm>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
ifstream inFile;
inFile.open("A06input.txt"); // Open for reading

ofstream outFile;
outFile.open("A06output.txt"); // Open for writing

string s;
int strLen = 0;

while (getline(in,s)) {

strLen = s.length();
for (int i = 0; i< strLen; i++) {
replace( s.begin(), s.end(), 't', 'T' );

out << s[i];
cout << s[i]<<endl;


}
}

inFile.close();

return 0;




}


This code doesn't compile, so it's hard to see how you've be having issues with output.
1
2
3
4
5
6
7
8
9
10
while (getline(in,s))
{
    strLen = s.length();
    for (int i = 0; i< strLen; i++)
    {
        replace( s.begin(), s.end(), 't', 'T' );
        out << s[i];
        cout << s[i]<<endl;
    }
}
Why do you replace in a loop? You can just call replace once and then output the entire string once.
I revamped the code a bit - removed the replace and added an if loop per the instructions from my teacher - but what is happening is this - I have an input file that has 3 lines :

This is a test
This is another test
This is yet another test

the program is supposed to change all lower case t's to upper case T's and then print out just as the input file is with each set of strings on its own line - what is happening is I get it to print and change the t's, however it shows up as one long string on one line - I have tried using an endl; and all that happens is the program breaks each string up into single characters each on its own line - if you have visual studio the code will compile - not sure about other compilers


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


using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
ifstream in("A06input.txt"); // Open for reading
ofstream out("A06output.txt"); // Open for writing
string s;

int strLen = 0;

while (getline(in, s)) {

strLen = s.length();
for (int i = 0; i< strLen; i++) {
if (s[i] == 't') s[i] = 'T';

out <<s[i];
cout << s[i];

}
}

system("pause");
return 0;




}
if you have visual studio the code will compile - not sure about other compilers


The original code would not compile on any compiler.

1
2
3
4
5
6
7
8
9
10
11
12
13
while (getline(in, s)) {

    strLen = s.length();
    for (int i = 0; i< strLen; i++) {
        if (s[i] == 't') s[i] = 'T';

        out <<s[i];
        cout << s[i];

    }
    out << '\n' ;          // ***
    cout << '\n' ;         // ***
}


I have tried using an endl; and all that happens is the program breaks each string up into single characters each on its own line


Only output a newline at the end of a line.

I think I figured out the problem here - the cout and out statements are both inside the for loop causing the program to print each iteration of the string - I think if I take it out of the for loop it should fix the issue.
Topic archived. No new replies allowed.