c++/ writing to file / isupper / IO

Having trouble trying to inspect each character read...if the character is not a Lower Case letter, write the character to a file called upper.txt . If the character is a Lower Case letter write the Upper Case version of the character to the same file upper.txt.
I can use islower(), isupper(), toupper functions in the cctype library.

Here's what I have so far, just kind of stuck at this point.

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

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cctype>

using namespace::std;

int main()
{

	ifstream inFile;
	ofstream outFile;
	char inChar;
	char outChar;

	inFile.open("message.txt");

	if (inFile.fail())
	{
		cout << "Could not open input file" << endl;
		exit(1);
	}

	outFile.open("upper.txt");

	if (outFile.fail())
	{
		cout << "Could not open output file" << endl;
		exit(1);
	}

	

	inChar = inFile.get();
	while (!inFile.eof())
	{
		if (isupper(inChar))
			outFile << inChar;
		else
		{
			outChar = toupper(inChar);
			outFile << inFile.get();
		}
	}

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

}
You need two output files. I see only one output file being created.

Can you elaborate on that ? I'm taking the text from the actual file message.txt, and creating/ putting data into upper.txt . I don't understand why I would need to create 2 output files.
Sorry, I misread your original post. Yes, just one output file. Anyway, onwards.

Line 43. Shouldn't you be writing out the uppercase version of inChar?

I'm not sure at this point.. Do I have to have outFile open before inFile ? I say this because I am getting errors when I switch line 43. I feel like I am a little mixed up maybe.
Here's the logic of what you need to do, with a lowercase letter:

Convert it to upper case
Write it to outfile


What your code currently does with a lowercase letter is this:

Convert it to upper case
Fetch a new letter from the infile and immediately write that to the outfile, completely ignoring the letter you just converted to upper case
I'm striking out over here. What am I missing ? I'm just getting started on c++ if you couldn't already tell. The program just sits there and does nothing right now. Its trying to write something to upper.txt, but it leaves it open and isn't completing the loop ( even if it is wrong).
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
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cctype>

using namespace::std;

int main()
{

	ifstream inFile;
	ofstream outFile;
	char inChar;
	char outChar;

	inFile.open("message.txt");

	if (inFile.fail())
	{
		cout << "Could not open input file" << endl;
		exit(1);
	}

	outFile.open("upper.txt");

	if (outFile.fail())
	{
		cout << "Could not open output file" << endl;
		exit(1);
	}

	while (inFile >> inChar)
	{
                outChar = toupper(inChar);
                outFile << outChar;
	}

	inFile.close();
	outFile.close();
}
wow.. that makes perfect sense now. I appreciate it! so now "My Text File" reads like this "MYTEXTFILE". Is there a good way to include the space's from the original file to read like this "MY TEXT FILE"
Tell the input file stream not to skip whitespace:

inFile >> std::noskipws;
Works great. Thanks a ton !

Here's the final code

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
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cctype>

using namespace::std; 

int main()
{

	ifstream inFile;
	ofstream outFile;
	char inChar;
	char outChar;

	inFile.open("message.txt");

	if (inFile.fail())
	{
		cout << "Could not open input file" << endl;
		exit(1);
	}

	outFile.open("upper.txt");

	if (outFile.fail())
	{
		cout << "Could not open output file" << endl;
		exit(1);
	}

	while (inFile >> std::noskipws >> inChar)
	{
		outChar = toupper(inChar);
		outFile << outChar;
	}

	inFile.close();
	outFile.close();
}
Topic archived. No new replies allowed.