Help with I/O

I am going to say this outright this is an assignment for school and my teacher has said i can come in for help but i do not have any transportation due to hurricane harvey. that being said i need to edit this code here to do the following(in teachers words not mine):

We will continue working with the Letter Counting Program from last week. (Just when you thought it was safe to go back in the water!)

1. First I want you to modify the program to prompt the user for the name of the file to output.

2. Then, change the program to modify the output file by making each sentence a new paragraph (inserting two carriage returns between every sentence. :) Don't over-think this, but you must work through and understand how the program works now in order to modify it. Remember, you want the carriage returns between every SENTENCE, not every LINE.

3. Also, it would be nice if you could shave off any unnecessary spaces (such as those before the sentence begins).

7. Finally, don't let the screen just flash by - add the code to tell the user what is happening.

Take your time with this program and get it right. As I mentioned before, there is MUCH to be learned from it!

I need help with steps 2 and 3 above I have the rest covered Thanks in Advance!

//*************************************************************
// Author: D.S. Malik
//
// Program: Line and Letter Count
// This program reads a text, outputs the text as is, and also
// prints the number of lines and the number of times each
// letter appears in the text. An uppercase letter and a
// lowercase letter are treated as being the same; that is,
// they are tallied together.
//*************************************************************

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

using namespace std;

void initialize(int& lc, int list[]);
void characterCount(char ch, int list[]);
void copyText(ifstream& intext, ofstream& outtext, char& ch,
int list[]);
void writeTotal(ofstream& outtext, int lc, int list[]);

int main()
{
//Step 1; Declare variables
int lineCount;
int letterCount[26];
char ch;
ifstream infile;
ofstream outfile;

infile.open("textin.txt"); //Step 2 Attempts to Open Desired file

if (!infile) //Step 3 If program cannot find/open file it outputs "Cannot open the input file."
{
cout << "Cannot open the input file."
<< endl;
return 1;
}

outfile.open("textout.out"); //Step 4 Converts the file to write to and output

initialize(lineCount, letterCount); //Step 5 Calls on function initialize to initialize linecount and lettercount

infile.get(ch); //Step 6 reads and stores characters in ch

while (infile) //Step 7 while function that copies infile to outfile
{
copyText(infile, outfile, ch, letterCount); //Step 7.1 Copies data between in/outfiles and reads characters and counts the characters
lineCount++; //Step 7.2 Counts lines
infile.get(ch); //Step 7.3 reads and stores characters in ch
}

writeTotal(outfile, lineCount, letterCount); //Step 8 writes to the outfile to be shown in text.out

infile.close(); //Step 9 closes infile
outfile.close(); //Step 9 closes outfile

return 0;
}

void initialize(int& lc, int list[])
{
int j;
lc = 0;

for (j = 0; j < 26; j++)
list[j] = 0;
} //end initialize

void characterCount(char ch, int list[])
{
int index;

ch = toupper(ch); //Step a Makes each individual letter A capital letter and counted

index = static_cast<int>(ch)
- static_cast<int>('A'); //Step b gives each letter a value

if (0 <= index && index < 26) //Step c index's letters to be called in write total
list[index]++;
} //end characterCount

void copyText(ifstream& intext, ofstream& outtext, char& ch,
int list[])
{
while (ch != '\n') //process the entire line
{
outtext << ch; //output the character
characterCount(ch, list); //call the function
//character count
intext.get(ch); //read the next character
}
outtext << ch; //output the newline character
} //end copyText

void writeTotal(ofstream& outtext, int lc, int list[]) // writes totals at bottem of page
{
int index;

outtext << endl << endl;
outtext << "The number of lines = " << lc << endl;

for (index = 0; index < 26; index++)
outtext << static_cast<char>(index + static_cast<int>('A'))
<< " count = " << list[index] << endl;
} //end writeTotal
Teacher says im overthinking things by wanting to add another while statement and trying to get the ., !, ? read differently but does not give me anything to go on do i just add to the existing while statement?
closed account (48T7M4Gy)
Here's a start:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>

using namespace std;

int part_2(ifstream &intext, ofstream &outtext);

int main()
{
    ifstream infile;
    ofstream outfile;
    
    
    return 0;
}

int part_2(ifstream &intext, ofstream &outtext)
{
    const int MAX = 200;
    int line_count = 0;
    

    return line_count;
}
Last edited on
closed account (48T7M4Gy)
Remember, you want the carriage returns between every SENTENCE, not every LINE.
This is a bit more subtle, but not impossibly so. A sentence generally starts with a capital letter and as a bonus ends with a fullstop/period.

Two choices ... :)
I see what you did there but how would i go about reading a period? would i use: char *line = '.'; and sub it where i need it?
closed account (48T7M4Gy)
Well this is where you might go back to reading the file character by character. You have to count characters anyway. I’ll give you a couple of tips.

1. If you have a character char x. x is a period if x==‘.’
2. Checkout noskipws as a function so when you read character by character spaces aren’t ignored.
3. A sentence is made up of an array of all characters, including spaces with a period and ‘\0’ terminator at the end. You can build up the sentence character by character.

See how you go. Use lots of cout’s to track your progress by modifying the above.
thanks I think i get it now ill let you know how it goes
Nope im still stuck cant seem to wrap my head around this.
closed account (48T7M4Gy)
OK, maybe this helps. You need to expand this. Checkout the display and the output file. (BTW I'm assuming both are .txt files)

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

size_t part_2(std::ifstream &intext, std::ofstream &outtext);

int main()
{
    std::ifstream infile;
    std::ofstream outfile;
    
    infile.open( "yeltsin.txt");
    outfile.open("textout.txt");
    
    std::cout
    << "*** "
    << part_2(infile, outfile)
    << " sentences read - see file ***\n";
    
    infile.close();
    outfile.close();
    
    return 0;
}

size_t part_2(std::ifstream &intext, std::ofstream &outtext)
{
    int letter_count = 0;
    char letter;
    
    while( intext >> letter)
    {
        std::cout << letter <<'\n'
        outtext << letter;
        letter_count++;
    }

    return letter_count; // actually you want sentence count so count periods!
}
So what you are saying is i can use linecount to count the empty spaces after the peroid and use cout << linecount << endl << endl; but wouldnt it count the end of the line and not after the period?
closed account (48T7M4Gy)
Every sentence has a period at its end (denoted ‘.’). Every line has a carriage return (denoted ‘\r’ )

My bad for mistakenly focussing on counting lines.

All you have to do is read the input file character by character and write each character to the output file - ie copy the input file - with two exceptions. First, if an existing carriage return is encountered than ignore it, don’t copy it. Second, if a period is detected then copy the period across but add two carriage returns.
so like this:

void copyText(ifstream& intext, ofstream& outtext, char& ch,
int list[])
{
while (ch == '.') // added code
{
cout << endl << endl;
} // end added code
while (ch != '\n') //process the entire line
{
outtext << ch; //output the character
characterCount(ch, list); //call the function
//character count
intext.get(ch); //read the next character
}
outtext << ch; //output the newline character
} //end copyText

or did i edit the wrong function or should i nest it somehow?
Last edited on
Also can you tell me how you are putting you code in a neat box? Cause i think that would make my programs easier to read when asking for help here.
I also very much appreciate your patience with me as i know I've got to be annoying and i apologize for that.
closed account (48T7M4Gy)
Select your code and press the <> button in the tool box on the right. You can go back and use the edit button instead of re posting
Last edited on
closed account (48T7M4Gy)
As far as your code is concerned you only need one while loop and two if’s inside. The while is there to read the file until the end, ie no more characters. The if’s to do the sentence/paragraph/carriage return stuff

1
2
loop while characters to read
    if character is a period
Last edited on
so more like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

void copyText(ifstream& intext, ofstream& outtext, char& ch,
int list[])
{
while (ch != '\n') //process the entire line
{
      if (ch == '.')
           cout << endl << endl;
      if (ch == '\n')
           cout << "\b";
outtext << ch; //output the character
characterCount(ch, list); //call the function
//character count
intext.get(ch); //read the next character
}
outtext << ch; //output the newline character
} //end copyText

im not sure how not to read the \n character any ideas or will the \b work here?
closed account (48T7M4Gy)
Well the test is really, what did you get when you run it?

However, you might like to consider

You now have to write some code for the general case and the other case I mentioned where it is not the end of a sentence but the end of the line.

Don’t forget to print stuff out to display the results of what you‘re doing. You can even add a message cout << “Bingo period encountered\n”; in that if.
Last edited on
closed account (48T7M4Gy)
Run this and see whether it makes sense.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 
// GENERAL CASE
        else
        {
            std::cout << ch;
            outtext << ch;
            if(ch != ' ')
            {
                letter_count++;
            }
        }
        
        previous = ch;
    }
    
    return letter_count;
} */
Last edited on
I am still doing this program but my teacher isnt in any hurry for it and i have other classes to do i will be back friday or sat
Topic archived. No new replies allowed.