c++ - fstream <unable to read memory>. Trouble printing all items in list -


i'm working on menu driven program has users keep track of assignment tasks , due dates. program deals text file titled "tasks.txt", , user has 3 options interacting text file: enter new task, display tasks in file, or find task course. program runs relatively nicely except doesn't seem able interact file.

below output/input file: tasks.txt

cs162;finish project 2;04/10/2015 cs162;finish project 3;04/20/2015 cs162;finish project 4;05/10/2015 cs162;finish project 5;05/20/2015 

here's code:

#define _crt_secure_no_warnings #include <cstring> #include <fstream> #include <iostream>  using namespace std;  //global constants const int cap = 100; const int maxchar = 201; // task description can max of 200 characters in case long description (ie: specific instructions)  //task struct struct task {     char coursename[maxchar];     char taskdescrip[maxchar];     char duedate[maxchar]; };  //function prototypes void openfile(ifstream &infile); void loaddata(ifstream &infile, task tasks[], int &listsize); void execommand(char option, task tasks[], int &listsize); void displaymenu(); char readoption(); void getusertask(task &temptask); void addusertask(task tasks[], int &listsize, task temptask); void printtasks(task tasks[], const int listsize); void writefile(ofstream &outfile, task tasks[], const int listsize);   //main function int main() {     int listsize = 0; //keep track of list size     ofstream outfile; //output file stream variable     ifstream infile; //input file stream variable     task tasks[cap]; //array of tasks     char option; // user menu input option     openfile(infile); //open tasks.txt     loaddata(infile, tasks, listsize); //load data while in file array of tasks         {         displaymenu();         option = readoption();         execommand(option, tasks, listsize);     } while (tolower(option) != 'q');     return 0; }  //open file void openfile(ifstream &infile) {     infile.open("tasks.txt");     if (!infile)     {         cout << "file not open" << endl;         exit(0);     } }  //load data file array // parameters:  void loaddata(ifstream &infile, task tasks[], int &listsize) {     while (!infile.eof())     {         infile.get(tasks[listsize].coursename, maxchar, ';');         infile.ignore(200, ';');         infile.get(tasks[listsize].taskdescrip, maxchar, ';');         infile.ignore(200, ';');         infile.get(tasks[listsize].duedate, maxchar, ';');         infile.ignore(200, '\n');         listsize++;     }     infile.close(); }  void displaymenu() {     cout << "welcome tasktracker v.1" << endl;     cout << "(a) add task" << endl;     cout << "(l) list tasks" << endl;     cout << "(f) find tasks course" << endl;     cout << "(q) quit" << endl << endl; }  char readoption() {     char option;     cout << "please make selection: ";     cin.get(option);     cin.ignore(100, '\n');     return option; }  void execommand(char option, task tasks[], int &listsize) {     task temptask;// created object temptask     switch (tolower(option))     {     case 'a':         cout << "you chose add task!" << endl;         getusertask(temptask);//used avideo parameter getvideo         addusertask(tasks, listsize, temptask);//temptask          cout << endl << endl;         break;     case 'l':         cout << "you chose print tasks!" << endl;         printtasks(tasks, listsize);         cout << endl << endl;         break;     case 'f':         cout << "you chose find item course!"<< endl;          cout << endl << endl;         break;     case 'q':         return;     default:         cout << "invalid input!" << endl;     } }  void getusertask(task &temptask) {     cout << "please enter course name {less 101 characters): ";     cin.get(temptask.coursename, maxchar);//user enters course name     cin.ignore(100, '\n');//ignores next 100 characters or until newline      cout << "please enter brief description of task (less 101 characters): ";     cin.get(temptask.taskdescrip, maxchar);//user enters task description     cin.ignore(100, '\n');//ignores next 100 characters or until newline      cout << "please enter due date of task. (mm/dd/yyy): ";     cin.get(temptask.duedate, maxchar);//user enters task description     cin.ignore(100, '\n');  }  void addusertask(task tasks[], int &listsize, task temptask) {     tasks[listsize++] = temptask; }  //prints task list void printtasks(task tasks[], const int listsize) {     (int = 0; < listsize; i++)     {         cout << tasks[i].coursename << ';' << tasks[i].taskdescrip << ';' << tasks[i].duedate << endl;     } }  //writes file @ end of program void writefile(ofstream &outfile, task tasks[], const int listsize) {     outfile.open("tasks.txt");     (int = 0; < listsize; i++)     {         outfile << tasks[i].coursename << ';' << tasks[i].taskdescrip << ';' << tasks[i].duedate << endl;     }     return; } 

the following output in debugger:

welcome tasktracker v.1 (a) add task (l) list tasks (f) find tasks course (q) quit  please make selection: l chose print tasks! cs162;finish project 2;04/10/2015 cs162 cs162;finish project4;05/10/2015 cs162  welcome tasktracker v.1 (a) add task (l) list tasks (f) find tasks course (q) quit 

i have been peeling on code hours , searching online similar cases, seems fstream able read , load course name every line, skips task description , due date every other task. can please point me in right direction?

the add option of menu seems okay loading new user input tasks buffer, writefile function thats supposed save tasks in buffer output file doesn't seem work either.

i'll continue solution appreciated!

your input processing marred. suggest more along these lines:

void loaddata(ifstream &infile, task tasks[], int &listsize) {     while (infile.get(tasks[listsize].coursename, maxchar, ';') &&         infile.ignore(200, ';') &&         infile.get(tasks[listsize].taskdescrip, maxchar, ';') &&         infile.ignore(200, ';') &&         infile.get(tasks[listsize].duedate, maxchar, '\n'))     {         infile.ignore(200, '\n');         listsize++;     }     infile.close(); } 

produces following output:

welcome tasktracker v.1 (a) add task (l) list tasks (f) find tasks course (q) quit  please make selection: l chose print tasks! cs162;finish project 2;04/10/2015 cs162;finish project 3;04/20/2015 cs162;finish project 4;05/10/2015 cs162;finish project 5;05/20/2015 

in short, request third field terminating ';' (which not on sample lines) skipping next line until found requested delimiter, including intermediate newline , first field of next line along ride. remainder of now-current line discarded follow-up ignore.

best of luck.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -