Need some help with file editing.

Hello everyone, been coming here a lot this past year since I started learning, but this is the first time I have posted a question.

I have been doing this program for class that asks to convert a files that's filled with sentences that are mixed with upper and lower case letters. The program is supposed to open the file, convert letter to their proper format, and save them in a separate file. The problem is that it always repeats the last character twice, even with the rest of the file coming out right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
	string unfmat;		
	string fmat;
	char ch;				
	ifstream inFile;
	ifstream inFile2;

	cin >> unfmat;

	cin >> fmat;

	ofstream outFile(fmat);

	inFile.open(unfmat);

	if (inFile)
	{
		
		inFile.get(ch);
		outFile.put(toupper(ch));
		
		while (inFile)
		{
		
			inFile.get(ch);
			outFile.put(tolower(ch));
		
			if (ch == '.')
			{
				inFile.get(ch);
				outFile.put(ch);
				
				if (ch == '\n')
				{
					inFile.get(ch);
					outFile.put(toupper(ch));

				}
				if (ch == ' ')
				{

					inFile.get(ch);
					outFile.put(toupper(ch));

				}
			}
		}
	}

	inFile.close();
	outFile.close();

	inFile2.open(fmat, ios::in);

	if (inFile2)
	{
		inFile2.get(ch);
		while (inFile2)
		{
			cout << ch;

			inFile2.get(ch);
		}
		
	}

	inFile2.close();
	
	system("Pause");
	return 0;
}


The Input file looks like this:
SiMple SeNtence HERe.
sEntEnCe SpannInG tWO
lInEs HERE.
tWO sEnTeNCEs. iN tHe Same LiNE.
TWo and halF. SeNTenceS in ThE. samE liNE

The Output comes out like this:
Simple sentence here.
Sentence spanning two
lines here.
Two sentences. In the same line.
Two and half. Sentences in the. Same linee

No matter what the final character is, the program repeats it.

Also on a side note, I have to turn this in through an CodeLab as part of my class and the results are even weirder. The program actually works correctly, but makes a new line 3 times for some odd reason during output.
Last edited on
FYI. I ran it and don't get a problem with the repeating final character. For the extra blank lines at the end I just added a couple of lines of code (37-44), to ignore the rest of the file if the current character is '\n' and the next one is also '\n'. Seems a bit of a bodge, but it does work; I added a bunch of blank lines to the file, and it doesn't print any of them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
   string unfmat, fmat;
   char ch;
   ifstream inFile, inFile2;

   cin >> unfmat;
   cin >> fmat;

   ofstream outFile(fmat);
   inFile.open(unfmat);

   if (inFile) {
        inFile.get(ch);
        outFile.put(toupper(ch));

        while (inFile)
        {
        inFile.get(ch);
        outFile.put(tolower(ch));

          if (ch == '.')
          {
          inFile.get(ch);
          outFile.put(ch);
          }

          if (ch == '\n')
          {
          inFile.get(ch);
           if (ch!='\n')
           {
           outFile.put(toupper(ch));
           }
           else
           {
           break;
           }
          }

          if (ch == ' ')
          {
          inFile.get(ch);
          outFile.put(toupper(ch));
          }
       }
  }

  inFile.close();
  outFile.close();

  inFile2.open(fmat, ios::in);

  if (inFile2)
  {
  inFile2.get(ch);
    while (inFile2)
    {
    cout << ch;
    inFile2.get(ch);
    }
  }

  inFile2.close();

return 0;
}


Input file:
SiMple SeNtence HERe.
sEntEnCe SpannInG tWO
lInEs HERE.
tWO sEnTeNCEs. iN tHe Same LiNE.
TWo and halF. SeNTenceS in ThE. samE liNE




Output:
Simple Sentence Here.
Sentence Spanning Two
Lines Here.
Two Sentences. In The Same Line.
Two And Half. Sentences In The. Same Line

So, not sure where your duplicate extra character comes from as I can't reproduce it, but good luck anyway. Hope you may find the above of use.
Last edited on
Thank you so much for the help Cheddar! It kinda worked but I have to make sure it only capitalizes after a period and nowhere else. The duplicate extra character might have something to do with Visual Studio, I'm gonna look into it.

Hopefully my Professor gives some credit for having it done properly. This online CodeLab is so finicky that it doesn't give completion for not having spacing right for certain strings.
Ah, of course; I should have noticed the unintended capitalization. Pretty sure the code below (just slightly modified from the code above) does everything correctly, with proper capitalization as you wanted, and also ignoring blank lines. I still haven't seen the problem you had with duplicated characters at the end, so I don't think that's a problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
   string unfmat, fmat;
   char ch;
   ifstream inFile, inFile2;

   cin >> unfmat;
   cin >> fmat;

   ofstream outFile(fmat);
   inFile.open(unfmat);

   if (inFile) {
        inFile.get(ch);
        outFile.put(toupper(ch));       // first character of file

        while (inFile)
        {
        inFile.get(ch);
        outFile.put(tolower(ch));

          if (ch == '.')
          {
          inFile.get(ch);
          outFile.put(ch);

            if (ch == ' ')
            {
            inFile.get(ch);
            outFile.put(toupper(ch));
            }
          }

          if (ch == '\n')
          {
          inFile.get(ch);

            if (ch!='\n')
            {
            outFile.put(toupper(ch));
            }
            else
            {
            break;
            }
          }
       }
  }

  inFile.close();
  outFile.close();

  inFile2.open(fmat, ios::in);

  if (inFile2)
  {
  inFile2.get(ch);
    while (inFile2)
    {
    cout << ch;
    inFile2.get(ch);
    }
  }

  inFile2.close();

return 0;
}


- Input file as above

- Output as below:
Simple sentence here.
Sentence spanning two
Lines here.
Two sentences. In the same line.
Two and half. Sentences in the. Same line

Hope that helps you. Good luck.
Last edited on
Sorry for the late reply Chedder, looks like its all good. Think something was wrong with the CodeLab at the time, so my Professor let it pass.
Topic archived. No new replies allowed.