program for getting and setting tasks

so i've completely re-written this program. entering tasks works and searching for tasks also works. When displaying all of the tasks in the file however the program loops infinitely. Im not sure why it does I'm pretty sure i did the algorithm right but I have also been staring at it forever so need a new pair of eyes. so heres the code
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
39
//this is the header file for labThree

#ifndef ADD_H
#define ADD_H

//class TaskList does different operations on the task list
class TaskList
{

public:	
	void userInterface(); //displays all of the choices
	void makeChoice();
	void enterTasks(); //asks user to enter task name, description and due date
	void searchForTaskName(); //searches "task.txt" for specific tasks
	void displayAllTasks(); //displays all of the tasks in "task.txt"
	
};

//class Task sets and gets the different tasks
class Task
{

public:
	//accessors
	void getTaskName();  //gets the name of the task from the user
	void getDescription(); //gets the description
	void getDueDate(); //gets the due date

	//mutators
	void setTaskName(); //outputs the task name to "tasks.txt"
	void setDescription(); //outputs the description to "task.txt"
	void setDueDate(); //outputs the due date to "task.txt"

	char taskName[100];
	char taskDescription[5000];
	char taskDueDate[10];
};

#endif 


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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//this is the implementation file for labThree

#include "tasks.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//creating the user interface
void TaskList::userInterface()
{
	//prints user interface to the screen using cout
	cout	<< "Welcome to your task organizer!\n\n"
			<< "*******************************************************************************\n\n\n"
			<< "\t\t1. Enter your task or assignment.\n"
			<< "\t\t2. Display all of the tasks that are in the file.\n"
			<< "\t\t3. Find a task by Course.\n"
			<< "\t\t4. Quit\n\n\n"
			<< "*******************************************************************************\n\n\n";
}

//-----------------------------------------------------------------------------------------------------------
void TaskList::enterTasks()
{
	Task task;	
	char correctInfo;
	int n = 0, y = 1;
	
	//gather information
	task.getTaskName();
	task.getDescription();
	task.getDueDate();

	cout << "\nWould you like to store this information(y/n)? ";
	cin  >> correctInfo;

	cout << "\n\n";
	
	if(correctInfo = y)
	{
		cout << "\n\n";
		task.setTaskName();
		task.setDescription();
		task.setDueDate();

		makeChoice();
		cout << "\n";
	}
	else
	{
		makeChoice();
	}
}

//-----------------------------------------------------------------------------------------------------------
void TaskList::displayAllTasks()
{
	ifstream openFile;
	string getData;

	//open tasks.txt
	openFile.open("tasks.txt");
    if(openFile.is_open())
    {
        //print everything to end of file 
		while(!openFile.eof())
        {
			getline(openFile, getData);
			cout << getData << endl;
        }
		//close tasks.txt
		openFile.close();
    }

}

//-----------------------------------------------------------------------------------------------------------
void TaskList::searchForTaskName()
{
	
	string searchStr, line;
	int lineNumber = 0;
	
	cout << "\nPlease enter the name of the task you want to find: ";
	cin  >> searchStr;
	cout << "\n";
	
	ifstream file("tasks.txt");

    while(getline(file, line))
    {
		if(line.find(searchStr) != string::npos)
		for(lineNumber = 0; lineNumber <= 2; lineNumber++)
		{			
			cout << line << '\n';
			if(lineNumber!=3)
				getline(file, line);
		}
		makeChoice();
    }

	cout << "\n";
}

//-----------------------------------------------------------------------------------------------------------
void TaskList::makeChoice()
{

	int choice;
	TaskList list;
	
	do
	{
	
		list.userInterface();
		
		cout << "Please enter a number between 1 and 4: ";

		cin  >> choice;
		//error control
		while(!cin)
		{
			cin.clear();
			cin.ignore(100, '\n');

			cout << endl << "You've entered an illegal integer, please try again!\n\n";
			cout <<			"Please Enter a Number(1,2,3,4): ";
			cin  >> choice;
		}
		cin.ignore(100, '\n');

		while(choice != 4)
		{
			switch(choice)
			{
				case 1:
					list.enterTasks();
					break;
				case 2:
					list.displayAllTasks();
					break;
				case 3:
					list.searchForTaskName();
					break;
				default:
					break;
			} //end switch
		}
	}
	while(choice != 4);

}

//-----------------------------------------------------------------------------------------------------------
void Task::getTaskName()//gets the name of the task from the user
{
	
	cout << "*****************************************************************************\n";
	cout << "\n\nPlease enter the name of your course: ";
	cin.getline(taskName, 100, '\n');
}

//-----------------------------------------------------------------------------------------------------------
void Task::getDescription() //gets the description
{
	cout << "Please enter the description of your assignment: ";
	cin.getline(taskDescription, 5000, '\n');
}

//-----------------------------------------------------------------------------------------------------------
void Task::getDueDate() //gets the due date
{
	cout << "Please enter the Due Date(9/26/2012): ";
	cin.getline(taskDueDate, 10, '\n');
	cout << "\n";

	cout << "\n" << taskName		<< "\n" 
		 <<			taskDescription	<< "\n"
		 <<			taskDueDate		<< "\n\n";
}

//-----------------------------------------------------------------------------------------------------------
void Task::setTaskName() //outputs the task name to "tasks.txt"
{
	ofstream outData;
	//open tasks.txt
		outData.open("tasks.txt", ios::app);
	
	//print data to file tasks.txt
		outData << taskName << "\n";
		
		outData.close();
}

//-----------------------------------------------------------------------------------------------------------
void Task::setDescription() //outputs the description to "task.txt"
{
	ofstream outData;
	outData.open("tasks.txt", ios::app);

	outData << taskDescription << "\n";

	outData.close();
}

//-----------------------------------------------------------------------------------------------------------
void Task::setDueDate() //outputs the due date to "task.txt"
{
	ofstream outData;
	outData.open("tasks.txt", ios::app);

	outData << taskDueDate << "\n\n";
	
	outData.close();

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
//trying to do my assignment using classes

#include "tasks.h"
#include <iostream>
using namespace std;

int main()
{
	TaskList TaskList;
	int choice;
	TaskList.makeChoice();
	return 0;
}

Last edited on
Topic archived. No new replies allowed.