Thread: someone please help me

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    11

    someone please help me

    I am having problem with this project. can some one possibly show me i would be very grateful to you. i cant get it to read a whole line then output the whole line, and im not to sure how to break up a ling line into multiple lines. here is the project.

    Design and implement a Visual C++ .NET program that numbers the lines of a text file and keeps some statistics about the text file. Your program should read lines of text from the file source.txt and output each line to the screen and to the file output.txt. Each line of output should be preceded by a line number. Print the line number at the beginning of the line, right justified in a field of three spaces. Follow the line number by a colon, then one space, then the line of text. You should remove any spaces at the beginning of a line (hint: read the input file one character at a time using <your_ifstream>.get(ch) and use<your_ifstream>.putback(ch)). Lines that are longer than 50 characters should be broken into multiple lines. Once you reach 50 characters in a line, insert a newline at the end of the current word (as soon as you read a space). This new line should not get a line number, but should be indented properly. Your program should also keep the following statistics about the text file: the number of lines, the number of words (assume words are separated by spaces and newlines, in other words, punctuation is part of the word), The length of the longest word and the length of the shortest word. These statistics should be output to the file stats.txt.
    Last edited by themexican; 10-17-2005 at 11:37 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    First, I would ignore the hint since it is very inefficient to read from a file char by char. Instead, read in a whole line at a time from the file into, say, a string variable. Then, you can access the chars in the string variable one at a time. Use a for loop to read from the string variable as long as the loop counter is less than the length of the string. Inside the loop, use an if statement to see if the loop counter has reached 50 yet. If it has, break out of the loop. The loop counter will mark where you stopped reading when you terminated the loop.

    You will have to check to see if the last char you read when you broke out of the loop was a space or not. If it wasn't a space, then you will have to keep reading until the char you read equals a space.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    do you mind giving me an example, i think i understand what you are talkin about but it would be very helpful if you can give me an example.

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    to read in a line do something like:

    Code:
    #include <string>
    
    ifstream f_in ("myfile.txt");
    
    string line;
    while(getline(f_in, line, '\n'))
    {
       //do stuff
    }
    f_in.close();
    Although, reading your post you will be going against what your teacher has actually suggested:- which is to read in a char at one time. Which is, like 7stud said inefficient.

    Moreover, you will need to work with strings, which is a handy alternative to char arrays. The STL has been cited to have a steep learning curve though. It's up to you?


Popular pages Recent additions subscribe to a feed