Question

I have been trying to make a task tracker console application that is able to perform three different functions --> add a task, show the task list, and search task list by classname. My code for adding a task works fine, but im trying to work out how to display the task list properly, but it keeps outputting only part of the tasks.

for example if you add the task CS162;Lab Due;04/20/2014 and the task MTH251;Quiz;04/22/2014. If you close the file and look at it, they were written to tasks.txt fine, but when i go to read them back, the output looks like: Due04/20/2014 MTH251Quiz which is only a fraction of the desired output. I also need to search by name but I think I can figure that out easily after I figure out the other issue

my textfile looks like this:

CS162;Lab Due;04/20/2014
MTH251;Quiz;04/22/2014

and my code looks like this:

#include <iostream>
#include <fstream>
#include <iomanip>


using namespace std;

ofstream outFile;
ifstream inFile;

const int MAXCHAR = 101;
const int MAXLINE = 256;

struct task
{
    char course[MAXCHAR];
    char desc[MAXCHAR];
    char date[MAXCHAR];
};

int main()
{
    task track;
    bool quit = false;
    while (quit == false)
    {
        char choice;
        cout << "Welcome to my Task List: \n";
        cout << "<a> to add task\n";
        cout << "<s> to show the task list\n";
        cout << "<f> to find a task by course name\n";
        cout << "<q> to quit\n";
        cin >> choice;
        cin.ignore(100, '\n');
        if (choice == 'a' || choice == 'A')
        {
            outFile.open("tasks.txt", fstream::app);
            cout << "Enter Course Name (less than 101 characters): ";
            cin.get(track.course, MAXCHAR, '\n');
            cin.clear();
            cin.ignore(100, '\n');

            cout << "Enter Task Description (less than 101 characters): ";
            cin.get(track.desc, MAXCHAR, '\n');
            cin.clear();
            cin.ignore(100, '\n');

            cout << "Enter due date (mm/dd/yyyy): ";
            cin.get(track.date, MAXCHAR, '\n');
            cin.clear();
            cin.ignore(100, '\n');

            char confirm;
            cout << "\nAre you sure you want to add " << track.course << ";" << track.desc << ";" << track.date << "? (y/n)";
            cin >> confirm;
            if (confirm == 'y' || confirm == 'Y')
            {
                cin.clear();
                cin.ignore(100, '\n');
                outFile << track.course << ";" << track.desc << ";" << track.date << "\n";
                cout << "Task has been added\n";
            }
            else if (confirm == 'n' || confirm == 'N')
            {
                cin.clear();
                cin.ignore(100, '\n');
            }
            outFile.close();
        }
        else if (choice == 's' || choice == 'S')
        {
            inFile.open("tasks.txt");
            char str[MAXCHAR];
            while (inFile >> str)
            {


                inFile.getline(track.course, MAXLINE, ';');
                inFile.getline(track.desc, MAXLINE, ';');
                inFile.getline(track.date, MAXLINE, ';');
                cout << track.course << track.desc << track.date;


            }

            inFile.close();
            cin.clear();
            cin.ignore(100, '\n');

        }

        else if (choice == 'f' || choice == 'F')
        {
            char course[MAXCHAR];
            cout << "Enter Course Name: ";
            cin >> course;

            inFile.open("tasks.txt");
            while (!inFile)
            {



            }
            inFile.close();

        }
        else if (choice == 'q' || choice == 'Q')
        {
            cin.clear();
            cin.ignore(100, '\n');
            quit = true;
        }

    }

}

edit: I now have the "show task list" working properly, but can somebody explain to me why using something like this doesnt work for the "search course"??

else if (choice == 'f' || choice == 'F')
    {
    char course[MAXCHAR];
    cout << "Enter Course Name: ";
    cin >> course;

    inFile.open("tasks.txt");
    while (inFile)
    {
    inFile.getline(track.course, MAXLINE, ';');
    inFile.ignore(100, '\n');
    while (strcmp(course, track.course) != 0)
            {
        cout << track.course;

            }
        }
    }
Was it helpful?

Solution

while (inFile >> str) {
  inFile.getline(track.course, MAXLINE, ';');
  inFile.getline(track.desc, MAXLINE, ';');
  inFile.getline(track.date, MAXLINE, ';');
  std::cout << track.course << track.desc << track.date;
}

The first line will read all chars into str until it hits the first whitespace, so if your input is

CS162;Lab Due;04/20/2014
MTH251;Quiz;04/22/2014

it will read up to and including Lab into str.

The next three lines will read until they hit ;, so your track struct will look as follows

course = "Due"
desc   = "04/20/2014\nMTH251" // note the newline
date   = "Quiz"

Changing the while loop and changing the last extractor should fix this:

while (inFile) {
  inFile.getline(track.course, MAXLINE, ';');
  inFile.getline(track.desc, MAXLINE, ';');
  inFile.getline(track.date, MAXLINE, '\n');
  if (inFile)
    std::cout << track.course << track.desc << track.date;
}

although it's worth mentioning that there are a number of things you can do here to simplify the code, including using std::string instead of char arrays, like this for instance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top