Having trouble with my output.

The text file this file is reading is as such...

CS162;Finish Lab 2;09/26/2009
CS161;Finish Lab 3;09/26/2009
CS201;Finish Finals;05/23/2010
CS211;Take Quiz 1;04/22/1999

It outputs this though when I ask it to show me the data.

CS162;Finish Lab 2;09/26/2009
CS161
CS201;Finish Finals;05/23/2010
CS211

I have no clue why it is removing half of my data when I ask it to show me the data in the program.

I'm also having trouble with the find part of the program trying to figure out how I can search for the course name and then output just that.

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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

const int MAXCHAR = 101;
const int CAP = 100;

struct Task
{
	char courseName[MAXCHAR];
	char taskDescription[MAXCHAR];
	char dueDate[MAXCHAR];
}; 

void menu();
char getCommand();
void exeCommand(Task [], int &, char);
void addItem(Task [], int &);
void showList(Task [], int &);
void findTask(Task [], int &);
void loadData(Task [], int &);
void addItem(ifstream &, Task [], int &);
void saveData(Task [], int &);

int main(void)
{
	Task courses[CAP];
	int size = 0;
	char option;
	loadData(courses, size);
	do
	{
		menu();
		option = getCommand();
		exeCommand(courses, size, option);
	}while(tolower(option) != 'q');
}

void menu()
{
	cout << "Welcome to my Task List:\n"
		<< "(a) to add a task\n"
		<< "(s) to show the task list\n"
		<< "(f) to find a task by course name\n"
		<< "(q) to quit\n";
	cout << "Please enter an option: ";
}

char getCommand()
{
	char ans;
	cin.get(ans); 
	switch (tolower(ans))
	{	
	case 'a': 
	case 's':
	case 'f':
	case 'q': 
		cin.ignore(100, '\n');
		return ans;
	default: 
		cout << "Illegal input.";
		cin.clear();
		cin.ignore(100, '\n');
		system("cls");
		menu();
		getCommand();
	}
}

void exeCommand(Task list[], int &size, char ans)
{
	switch(tolower(ans))
	{
	case 'a':
		addItem(list, size);
		saveData(list, size);
		break;
	case 's':
		showList(list, size);
		break;
	case 'f':
		findTask(list, size);
		break;
	case 'q':
		cout << "Thanks for using this program goodbye!" << endl;
		return;
	default:
		return;
	}
}

void loadData(Task list[], int &size)
{
	ifstream inFile;
	inFile.open("task.txt");
	if(!inFile)
	{
		cout << "File did not open! Program terminating!" << endl;
		exit(1);
	}
	while(!inFile.eof())
	{
		addItem(inFile, list, size);
	}
}

void addItem(Task list [], int &size)
{
	char course[MAXCHAR];
	char taskDesc[MAXCHAR];
	char date[MAXCHAR];
	char junk;
	cout << "Enter Course Name (less than 101 characters.): ";
	cin.get(course, MAXCHAR, '\n');
	cin.get(junk);
	cout << "Enter Task Description (less than 101 characters.): ";
	cin.get(taskDesc, MAXCHAR, '\n');
	cin.get(junk);
	cout << "Enter Due Date (mm/dd/yyyy): ";
	cin.get(date, MAXCHAR, '\n');
	cin.get(junk);
	strcpy_s(list[size].courseName, course);
	strcpy_s(list[size].taskDescription, taskDesc);
	strcpy_s(list[size].dueDate, date);
	size++;
}

void addItem(ifstream &inFile, Task list[], int &size)
{
	inFile.get(list[size].courseName, MAXCHAR, ';');
	inFile.ignore(50, ';');
	inFile.get(list[size].taskDescription, MAXCHAR, ';');
	inFile.ignore(50, ';');
	inFile.get(list[size].dueDate, MAXCHAR, ';');
	inFile.ignore(50, '\n');
	size++;
}

void showList(Task list[], int &size)
{
	int i;
	for(i = 0; i < size; i++)
	{
		cout << left << list[i].courseName << ";"
			<< left << list[i].taskDescription << ";"
			<< left << list[i].dueDate << endl;
	}
}

void saveData(Task list[], int &size)
{
	int i = 0;
	ofstream outFile;
	outFile.open("task.txt");
	if(!outFile)
	{
		cout << "File did not open! Program terminating!" << endl;
		exit(1);
	}

	for(i = 0; i < size; i++)
	{
		outFile << list[i].courseName << ';'
			<< list[i].taskDescription << ';'
			<< list[i].dueDate << endl;
	}
}

void findTask(Task list[], int &size)

{
	int i = 0;
	char junk;
	char course[MAXCHAR];
	cout << "This will allow you to search for a task by it's course name." << endl;
	cout << "Enter Course Name (less than 101 characters.): ";
	cin.get(course, MAXCHAR, '\n');
	cin.get(junk);
	if (strcmp(list[i].courseName, course) == 0)
		cout << "found it";
}

Last edited on
Your code won't compile correctly. You must be missing a header or something.

strcpy_s - Undeclared... first use in this function.

Either means you need a header or you are trying to use a function that doesn't exist.
The program runs I have ran it like 10 times in Microsoft Visual C++ 2010 Express.

Not sure why you say it won't run...

if the strcpy_s is giving ya prob's can use strcpy.

Figured it out myself anyways though...
Last edited on
Oh, I use Dev-C++ compiler. It doesn't have a built in library to access strcpy_s. I switched it to strcpy only and it worked. I'm glad you found the answer, sorry I wasn't quicker. I'm a beginner too, but helping others helps me become a better programmer. Debugging is the main part of the job after all.
Topic archived. No new replies allowed.