QuestionQuestion

Parsing Line Input

General Goal:
Create a C++ class and test program that:
• reads one line at a time from an ASCII file
• stores the individual words in an array
• prints the words in reverse order (on "one" line)

Details:
Create the implementation (oneline.cpp) and test program (main.cpp) for the following oneline.h file:
#include <string>
using namespace std;

class OneLine
{
   public:
      OneLine();
      ~OneLine();
      void breakLine();
      void printReverse();
      istream &readLine(istream& is);
      string returnLine();
   private:
      string oneLine;
      char **words;
      int wordCount;
      void resetLine();
};

The following provides a description of each of the functions implemented in oneline.cpp:
OneLine
• initialize oneLine to empty string
• initialize wordCount to 0

breakLine
• create a temporary C String version of oneLine
• use strtok to break the temporary line into words
• allocate enough space to hold all of the words and store them in words
• store a count of the number of words in wordCount
• (this is meant as a general algorithm, you may have to fiddle a little to get this working. Hint: this may involve cycling through all the words with strtok twice)

printReverse
• cycle through the words from wordCount-1 to 0
• print each word as you are cycling

readLine
• call resetLine to free up memory and reset oneLine and wordCount to empty string and zero respectively
• read one line from is (in this case, a file stream) and store it in oneLine
• return is

returnLine
• return oneLine

resetLine
• if allocation has occurred than free up the words
• set oneLine to be an empty string
• set wordCount to zero

~OneLine
• if allocation has occurred than free up the words

The following provides the algorithm of main.cpp:
1. create an ifstream for the file test.txt (please use this file for testing)
2. create a OneLine object
3. while you can still read from the file:
a. call readLine
b. call breakLine
c. call printReverse
4. close the file

For this assignment please use strdup whenever you need to allocate space and copy a string.
If your code is core dumping, you might want to try running it through valgrind with the command valgrind --leak-check=yes your executable. Look for messages of memory leaks. In the end, your code should have no memory leaks.

Output:
The following is a link to the output produced using test.txt:
• output.txt

If your run produces core dumps, or if you want to double check to ensure that you have no memory leaks, you can use valgrind. Use it as below:
• valgrind --leak-check=yes yourexecutable
• script this or try adding >& valgrind.out to the above line to save the output into a file called valgrind.out.

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

#include <iostream>
#include <fstream>
#include <cstring>
#include "oneline.h"
using namespace std;

int main ()
{
    ifstream is;
    is.open("test.txt", std::ifstream::in);
    if(!is.is_open())
   printf("Couldn't open file\n");

    OneLine test;
    test.readLine(is);
    test.breakLine();
    test.printReverse();
    is.close();

    return 0;
}...
$28.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats