File IO 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
#include <fstream>
#include <iostream>
#include <cctype>
using namespace std;

int main()
{
	//Variables
	fstream TextFile;
	const int intSIZE = 180; 
	int intCharLength = 0;
	char charInputName[intSIZE];
	char charName[intSIZE];
	
	//Open the file
	TextFile.open("ALLCAPS.txt", ios::in); 
	if (!TextFile)
	{
		cout << "ERROR: Unable to open file./n";
		return 0;
	}

	//Program title
	cout << "\t\t\t***Chapter 12: Sentence Capitalizer***\n\n";

	//Read the text
	TextFile.getline(charInputName, intSIZE, '/n');

	//convert the text into lowercase
	while (charInputName[intCharLength]<= strlen(charInputName))
	{
	charName[intCharLength] = tolower(charInputName[intCharLength]);
	intCharLength++;
	}

	intCharLength = 0;

	//format the text into proper case
	while (charInputName[intCharLength]<= strlen(charInputName))
	{
		if (charName[intCharLength] == '\n')
		{
		charName[intCharLength] = toupper(charName[intCharLength]);
		}
	intCharLength++;	
	}	
	
	//display the text
	for (int intCount = 0; intCount <= strlen(charInputName); intCount++)
	{
	cout << charName[intCount];
	}

	cout << endl;

	TextFile.close();
	system ("PAUSE");
}


I'm trying to read 6 sentences from a file that are all in upper case and convert it into proper case and output that into a file. The code in question is bolded above.

Example:
Input File:

JACK WENT UP THE HILL.
HE FOUND JILL IN THE WELL.
HE LAUGHED AND LEFT JILL.
JILL CRIED ALL NIGHT.
JACK CAME BACK AND RESCUED JILL.
JACK AND JILL ARE NOW MARRIED.

Output File:

Jack went up the hill.
He found jill in the well.
He laughed and left jill.
Jill cried all night.
Jack came back and rescued jill.
Jack and jill are now married.

The only output i'm able to get is the 6 sentences, but they're all lower case. Thanks!
1
2
3
4
5
6
7
8
9
	//format the text into proper case
	while (charInputName[intCharLength]<= strlen(charInputName))
	{
		if (charName[intCharLength] == '\n')
		{
		charName[intCharLength] = toupper(charName[intCharLength]);
		}
	intCharLength++;	
	}	


I guess you check wrong condition in while loop.
Change it and retry
 
while (intCharLength < strlen(charInputName))


Last edited on
Yeah, that didn't work, but thanks though!
You have to change whole code , i guess .
You code should include something like
1
2
3
4
5
6
7
8
9
// Read lines from file till the end of file
	for( TextFile.getline( charInputName , intSIZE , '.' ) ; !TextFile.eof() ;
         TextFile.getline( charInputName , intSIZE , '.' ) )
	{
         charName[0] = charInputName[0] ;  // Keep First letter Capital
         for( int i = 1 ; i < strlen( charInputName ) ; i++ )   
              charName[i] = tolower( charInputName[i] );  // convert  lower case rest of the string
         outP << charName ; // Write new sentence to outputfile
    }


You are going to get through all this fine.
One cultural issue to consider.
I think it may be inefficient to have a loop (for int i; i < strlen(charInputName);i++)
as strlen may need to be recomputed with each loop. I see programmers do this quite a bit - and it is troubling (I am an old timer!)
I'd welcome comment about whether optimizing compilers can really get rid of the O(n) cost of strlen().

One problem with C++ is this dualality between legacy C and forward looking C++.
You might find your code easier to maintain if you go forward to C++ more enthusiastically..

http://www.cppreference.com/wiki/string/getline

shows how to read safer "strings", rather than char[], You can ask the length of a string every time in your loop, and get a constant-time response from charInputNane.length() (whereas strlen can be as slow as order-number-characters in string (O(N))

Or, you can go backwards to more C-esque view of this problem and use pointers

char* sptr = charInputName;
sptr++; // Careful about lines with no characters in them.
while (*sptr)
{
*sptr = tolower(*sptr);
++sptr;
}
Topic archived. No new replies allowed.