C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 300,579 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,232 people online right now. Registration is fast and FREE... Join Now!




Help with File IO

 

Help with File IO, Using toupper for Proper Case

Rating  5
wr3ckin_cr3w

2 Jul, 2009 - 06:23 PM
Post #1

New D.I.C Head
*

Joined: 2 Jul, 2009
Posts: 4

CODE
#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;

    [b]//format the text into proper case
    while (charInputName[intCharLength]<= strlen(charInputName))
    {
        if (charName[intCharLength] == '\n')
        {
        charName[intCharLength] = toupper(charName[intCharLength]);
        }
    intCharLength++;    
    }    [/b]
    
    //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!

This post has been edited by wr3ckin_cr3w: 2 Jul, 2009 - 08:19 PM

User is offlineProfile CardPM
+Quote Post


GWatt

RE: Help With File IO

2 Jul, 2009 - 10:23 PM
Post #2

//
Group Icon

Joined: 1 Dec, 2005
Posts: 2,496



Thanked: 61 times
Dream Kudos: 525
My Contributions
I would suggest using a boolean variable to record whether or not you are at the beginning of the sentence. Basically, every time you reach a period set the variable to true, and after the first alphabetic character change it to false again.


CODE

#include <cctype> // needed for the character type functions


string poem = // Whatever it is
bool startOfSentence = true;  // use this to determine whether
// or not you need to change the case
for (int i = 0; i < poem.length(); i++)
{
    if (isalpha(poem[i] ) )
    {
        if (startOfSentence)
            startOfSentence = false; // the next alphabetic character
// will be changed, but the starting one will be left alone
        else
            poem[i] = tolower(poem[i]);
    }
    else if (poem[i] == '.') //end of this sentence, start of the next one.
        startOfSentence = true;
}

User is offlineProfile CardPM
+Quote Post

PsychoCoder

RE: Help With File IO

2 Jul, 2009 - 10:27 PM
Post #3

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,714



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
Take a look at this snippet on this very topic smile.gif
User is offlineProfile CardPM
+Quote Post

c.user

RE: Help With File IO

3 Jul, 2009 - 12:15 AM
Post #4

D.I.C Regular
***

Joined: 14 Jun, 2009
Posts: 299



Thanked: 24 times
My Contributions
CODE


#include <iostream>

using namespace std;

void FirstUpOtherDown(string *l);

/*
void FirstUpOtherDown(string &l);
*/

/* makes first character of string to upper
   other characters to lower */
int main(void)
{
    string line = "aBCD";
    
    cout << line
         << endl;
    
    FirstUpOtherDown(&line);
    /* FirstUpOtherDown(line); */ /* this looks like line can't be changed */
    
    cout << line
         << endl;
        
    return 0;
}

/* FirstUpOtherDown:  make first character from l to upper
                      other characters to lower */
void FirstUpOtherDown(string *l)
{
    (*l)[0] = char(toupper((*l)[0]));
    for (int i = 1; i < (*l).length(); i++)
        (*l)[i] = char(tolower((*l)[i]));
}

/*
void FirstUpOtherDown(string &l)
{
    l[0] = char(toupper(l[0]));
    for (int i = 1; i < l.length(); i++)
        l[i] = char(tolower(l[i]));
}
*/

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/8/09 08:22AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month