Help With Class Interactivity!

I'm trying to write a program that deals with information from two separate classes. There's an emphasis on restricting access to data between the two, and instead relying on functions to bounce back and forth between the two.

The current program is designed to allow the user to store practice questions into the system and eventually write them to an external file.

I have everything working thus far, however I'm running into an issue when I try to fix the chronology of questions when you delete one.

I.e. if you have 5 questions in the system and you delete question 3, I wanted to have questions number 4 and 5 incremented up into that position.

I had this working fine when not using multiple classes, but a single structure, I just can't import that code to a way that works and wanted to get some help.

I'm also not quite to the point of using linked lists, and restricted from using the string class.

Let me know if you are able to help, or have any solutions in general to the setup I have with the classes

header.h
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <iostream>
#include <cstring>
#include <cctype>
#include <ctime>

using namespace std;

const int MAX = 10;

//Class creation for a question.
class Question
{
    public:
        Question();
        ~Question();

        void input(int & counter);
        void display();
        void read(char prompt[], int size, char output[]);
        void clearTemp();
        void storeTemp(int & counter);
        void clearSave(int & counter, int delChoice);
        void displayTemp();
        bool compareSubject(int & counter, char subject[]);
        void timeStamp();

    private:
        char choice;
        int qNum;
        char * qSub;
        char * qText;
        int qDay;
        int qMon;
        int qYr;
        int tempNum;
        char tempSub[100];
        char tempText[100];

};

//Class creation for a multiple questions.
class List
{
    public:
        List();
        ~List();

        void input(int & counter);
        void display(int & counter);
        void dis_question(int & counter);
        void compareSubject(int & counter, char subject[]);

    private:
        Question q[MAX];
        int delChoice;
        int disChoice;
        char searchSubject[100];
        bool exit;
};

//FUNCTION PROTOTYPES
...

[code]
#include "header.h"

Question::Question()
{
    choice = 0;
    qNum = 0;
    qDay = 0;
    qMon = 0;
    qYr = 0;
    tempNum = 0;
    tempText[0] = '\0';
    tempSub[0] = '\0';
}

Question::~Question()
{
    cout << "Destructor called." << endl;
    delete [] qText;
    delete [] qSub;
}

void Question::input(int & counter)
{
    cout << "--Question Number " << counter+1 << "--" << endl;
    tempNum = counter+1;
    read("Enter question subject:\n", 100, tempSub);
    read("Enter question text:\n", 100, tempText);

    displayTemp();

    cout << "Would you like to save this question? (Y/N) ";
    cin >> choice;
    cin.ignore(100, '\n');

    if ('N' == toupper(choice))
        clearTemp();
    else
        timeStamp();
        storeTemp(counter);
}

void Question::timeStamp()
{
    time_t t = time(0);
    struct tm * now = localtime(&t);

    int day = now->tm_mday;
    int month = now->tm_mon + 1;
    int year = now->tm_year + 1900;

    qDay = day;
    qMon = month;
    qYr = year;
}

void Question::display() //Display
{
    cout << "Question Number " << qNum << endl;
    cout << "Subject: " << qSub << "\t| " << qText << endl;
    cout << "Date Added (MM DD YYYY) " <<
            qMon << " " << qDay << " " << qYr << endl;
}

void Question::read(char prompt[], int max_size, char output[])
{
    cout << prompt;
    cin.get(output, max_size, '\n');
    cin.ignore(100, '\n');
}

void Question::displayTemp()
{
    cout << "You entered:" << endl;
    cout << "Question Number " << tempNum << endl;
    cout << "Subject: " << tempSub << "\t| " << tempText << endl;
}

void Question::clearTemp()
{
    tempNum = 0;
    tempText[0] = '\0';
    tempSub[0] = '\0';
}

void Question::clearSave(int & counter, int delChoice)
{
    cout << "Are you sure you want to delete this question? (Y/N) ";
    cout << "\nDEBUG delChoice = " << delChoice << endl;
    cin >> choice;
    if ('Y' == toupper(choice))
    {
    qNum = 0; //Reset question number.
    qDay = 0; qMon = 0; qYr = 0; //Reset date.
    qSub[0] = '\0'; //Reset subject.
    qText[0] = '\0'; //Reset text.
    cout << "Question deleted." << endl; //Prompt user.
    --counter; //Decrement counter.
    }
}

void Question::storeTemp(int & counter)
{
    qNum = tempNum;

    qSub = new char[strlen(tempSub)+1];
    strcpy(qSub, tempSub);
    qText = new char[strlen(tempText)+1];
    strcpy(qText, tempText);
    ++counter;
}

bool Question::compareSubject(int & counter, char subject[])
{
    int compare = 0;
    compare = strcmp(subject, qSub);
    if (compare == 0) //If compare is a match.
        return true; //Display question information.
    return false;
}





List::List() //Constructor
{
    disChoice = 0;
    delChoice = 0;
    searchSubject[0] = '\0';
    exit = false;
}

List::~List()
{

}

void List::input(int & counter)
{
    q[counter].input(counter); //Question::input()
}

void List::display(int & counter)
{
    for (int i = 0; i < counter; ++i)
    {q[i].display();} //Question::display()
}

void List::compareSubject(int & counter, char subject[])
{
    for (int i = 0; i < counter; ++i)
        if(q[i].compareSubject(counter, subject))
           q[i].display();
}

//Function menu to display questions to the user.
void List::dis_question(int & counter)
{
    do { //Begin do-while loop.
    dis_menu(); //Display menu.
    cin >> disChoice; //Save choice.
    cin.ignore(100, '\n'); //Clear buffer.

    switch (disChoice) //Begin switch for display choice.
    {
        case 1: //Display all stored questions.
        display(counter);
        cout << "Press enter to return to the menu."; //Pause before exit.
        cin.get(); //Pause.
        continue; //Continue

        case 2: //Prompt user for search term.
        cout << "Enter the subject to search for: " << endl;
        cin.get(searchSubject, 100, '\n');
        cin.ignore(100, '\n');
        compareSubject(counter, searchSubject);
        cout << "Press enter to return to the menu."; //Pause before exit.
        cin.get(); //Pause.
        continue; //Continue

        case 3: //Delete question menu.
        display(counter);
        cout << "Which question would you like to delete?" << endl;
        cin >> delChoice;
        q[delChoice-1].clearSave(counter, delChoice);
        cout << "Press enter to return to the menu."; //Pause before exit.
        cin.get(); //Pause.
        continue; //Continue

        case 0:
        exit = true; //Set exit condition true.
        break;
    }
    } while (!exit && counter > 0); //Run loop while exit condition is false.
}


Function implementation - not really necessary.
 
#include "header.h" 

Last edited on
Main
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
#include "header.h"

int main()
{
    int counter = 0;
    int choice = 0;
    bool exit = false;
    List q; //Sets up an array of questions.

    welcome();

    do { //Begin Do Loop for Main Menu Switch
        main_menu(); //Call to menu function, displaying the main menu.
        cin >> choice; //Store menu choice in choice integer.
        cin.ignore(100, '\n'); //Clear input buffer.

        switch (choice) //Initiate switch based on choice.
        {
            case 1: //Add question menu.
                do{q.input(counter);}while (repeat()); //Runs while repeat
                continue; //Continue through loop.
            case 2: //Display question menu.
                if (counter == 0)
                    //If no questions exist, prompt user, exit.
                    cout << "There are no questions stored." << endl;
                else
                    //If questions exist, proceed to menu.
                    q.dis_question(counter);
                continue; //Continue through loop
            case 3: //External data file menu
                external_menu();
                continue; //Continue through loop
            case 4: //Info about the function.
                about(); //Call to about function.
                continue; //Continue through loop.
            case 0: //Exit menu.
                cout << "Goodbye...." << endl;
                exit = true; //Set loop exit condition true.
                break; //Break loop.
            default: //If number higher than range was entered.
                cout << "Please select a valid option." << endl;
                continue;
        } //End switch.
    } while (!exit); //End do-while when exit condition is true.

    //cin.get(); //Pause before ending program.
    return 0;
}
Topic archived. No new replies allowed.