Help with C++ project

So I'm working on a program that acts as a dictionary. It needs to be able to read in a file that has a list of words and their definitions and then it needs to sort it using qsort, print it, add a word and def, and delete a word and def. I'm still pretty new to C++ and programming in general. Yes this is a homework assignment and I've tried to figure it out myself but my teacher is very unhelpful. My teacher would also like us to incorporate classes but I'm having a very hard time grasping the concept of classes.

It would be great if anyone could show me some areas that are wrong and what needs to be done to fix it. And if it's not asking too much make me a class using a part of my code to give me an idea of what it should look like and what it does.

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


#include <iostream>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <stdio.h>

using namespace std;

string ans, word, def, deleteline, line;
int pick, add(), del(), print(), sort();

bool checkword(char* filename)
{
    int offset;
    string line, search;
    ifstream myfile;
    myfile.open("/Users/Alex/Documents/CISP 400/Project1./Project1/dictionary.txt");
	sort();
    if(myfile.is_open())
    {
        while(!myfile.eof())
        {
            getline(myfile,line);
            if ((offset = line.find(deleteline, 0)) != string::npos) 
            {
				cout << "found '" << deleteline << " \n\n"<< line  <<endl;
				return true;
            }
            else
            {
				
                cout << "Not found \n\n";
				
            }
        }
        myfile.close();
    }
    else
		cout<<"Unable to open this file."<<endl;
	
    return false;
}

int add()
{
    int count;
    cout << "Please enter 3 different words followed by their definition.\n";
	//Loop for how many entries
    count = 1;
    while (count<4)
	{
		cout << "Enter the word you would like to add\n";
		cin.ignore();
		getline(cin, word);
		cout << "Enter the definition of this word\n";
		getline(cin, def);
		//Add the words and def to the end of the file
		fstream myfile;
		myfile.open ("/Users/Alex/Documents/CISP%20400/Project1./Project1/dictionary.txt", fstream::in | fstream::out | fstream::app);
		myfile<<word <<" - " << def <<endl;
		myfile.close();
		count ++;
    }
	cout << "\nThank you, your words and definitions have been added.";
	cin.ignore();
	return 0;
}

int del()
{
	print();
	string check;
	//See in old and new file
	ifstream myfile;
	myfile.open("/Users/Alex/Documents/CISP%20400/Project1./Project1/dictionary.txt");
	ofstream temp;
	temp.open("/Users/Alex/Documents/CISP%20400/Project1./Project1/temp.txt");
	sort();
	cout << "What word would you like to delete?";
	cin >> deleteline;
	cout<< "Are you sure you want to delete this: yes or no.";
	cin>> check;
	if (check == "yes")
	{
	checkword("/Users/Alex/Documents/CISP%20400/Project1./dictionary.txt");
	//Copy all lines except the targeted one
	while (getline(myfile,line))
	{
		if (line != deleteline)
		{
			temp << line << endl;
		}
	}
			temp.close();
			myfile.close();
	//Remove and rename old and new file
			remove("/Users/Alex/Documents/CISP 400/Project1./Project1/dictionary.txt");
			rename("/Users/Alex/Documents/CISP%20400/Project1./Project1/temp.txt","/Users/Alex/Documents/CISP%20400/Project1./Project1/dictionary.txt");
			cout <<endl<<endl<<endl;
	}
	else 
	{
		del();
	}

}




int print()
{
    string readfile();
    {
		//Read in file
        string line;
        ifstream myfile ("/Users/Alex/Documents/CISP 400/Project1./Project1/dictionary.txt");
		myfile.open("/Users/Alex/Documents/CISP 400/Project1./Project1/dictionary.txt");
        if (myfile.is_open())
        {
			//Print file
            while ( getline (myfile,line) )
            {
                cout << line << endl;
            }
            myfile.close();
        }
        
        else cout << "Unable to open file";
        
        return 0;
    };

}

int sort()
{
    //Empty vector holding all names from file
	vector<string> names;
	
	//Read names from file
	ifstream myfile("/Users/Alex/Documents/CISP 400/Project1./Project1/dictionary.txt");
	myfile.open("/Users/Alex/Documents/CISP 400/Project1./Project1/dictionary.txt");
	if(!myfile.is_open())
		cout << "Unable to open file\n";
	
	//Sort Names
	string word;
	while(getline(myfile, word))
		names.push_back(word);
	
	sort(names.begin(), names.end());
	
	//Loop to print names
	for (size_t i = 0; i < names.size(); i++)
		cout << names[i] << '\n';
    return 0;
}

int main()
{    
    ans = "yes";
    while (ans == "yes")
    {
        cout << "\nTo add a word press 1: \n To delete a word press 2: \n To search a word press 3: \n To print a list press 4:";
        cin >> pick;
		fstream myfile;
		myfile.open("/Users/Alex/Documents/CISP 400/Project1./Project1/dictionary.txt");
        switch (pick)
        {
            case 1:;
                add();
            case 2:;
                del();
            case 3:;
                checkword("/Users/Alex/Documents/CISP 400/Project1./Project1/dictionary.txt");
            case 4:;
                sort();
        }
        cout << "Would you like to run this again: 'Yes or No'";
        cin >> ans;
    }
	return 0;
}

Last edited on
Well.... you should have a class Entry and a class Dictionary.

Entry should have at least 2 things:

string name
string definition

possibly more like:

string partOfSpeech
string language
etc

Dictionary should have:
A container of Entry's, possibly a vector
A file loading system
A find, sort, delete, and add function(one for each)

etc

I am sure you can think of more
It looks "map" would be far better storage for words and definitions rather than "vector".
Topic archived. No new replies allowed.