Text Editor / AI

HELP!




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
// Editor.cpp : Defines the entry point for the console application.
/*
// Add these includes into stdafx.h
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <process.h>
#include <time.h>
#include <conio.h>
#include <sstream>
#include<vector>
using namespace std;
*/
#include "stdafx.h"
 
#define DEBUG
 
// Answer class
class CAnswers {
        public:
                vector<string> answer;
                CAnswers(void);
                CAnswers(string ans);
                ~CAnswers(void);
                int size(void);
} Answers;
CAnswers::CAnswers (void) {
}
CAnswers::CAnswers (string ans) {
        stringstream ss(ans);
        string temp_answer;
        while (getline(ss, temp_answer,','))
                 answer.push_back(temp_answer);
}
CAnswers::~CAnswers(void){
}
int CAnswers::size(void) {
        return answer.size();
}
 
// Question class
class CQuestion {
        string _question;
 
        public:
                CAnswers* answers;
                CQuestion(void);
                CQuestion(string q_a);
                ~CQuestion(void);
                string question(void);
} Question;
CQuestion::CQuestion (void) {
}
CQuestion::CQuestion (string q_a) {
        string ans;
 
        stringstream ss(q_a);
        getline(ss, _question,':');
        ss >> ans;
        answers = new CAnswers(ans);
}
string CQuestion::question (void) {
        return _question;
}
CQuestion::~CQuestion(void){
        delete answers;
}
 
//Editor class
class CEditor {
        vector<CQuestion> questions;
        ofstream myFile;
        int age, address;
        char selection, character[1];
        string name, response;
        HANDLE np;
        unsigned notepadID;
       
        public:
                CQuestion* q;
                CEditor(void);
                ~CEditor(void);
                int menu(void);
                void question(void);
                void reload(void);
                int time(void);
                static unsigned int __stdcall notepadThread(void* arg);
                void notepad(void);
  } Editor;
CEditor::~CEditor(void){
        delete q;
}
CEditor::CEditor (void) {
        np = 0;
}
unsigned int __stdcall CEditor::notepadThread(void* arg) {
        system("notepad.exe AI.txt");
        _endthreadex(0);
        return 0;
}
void CEditor::notepad(void) {
        _beginthreadex( NULL, 0, &CEditor::notepadThread, np, 0, &notepadID );
}
int CEditor::menu (void) {
        system ("CLS");
        cout<<"Menu:\n9 = Basic Information.\n8 = DATALOG\n7 = Reload \n6 = Time\n0 = Leave\n";
        cin>>selection;
 
        switch (selection)
        {
                // Open text in notepad
                case '8' : {
                        notepad();
                        break;
                }
                // Exit program
                case '0' : {
                        cout<<"- Please close Notepad to exit - ";
                        return 0;
                }
                // Ask questions and place answers into AI.txt
                case '9' : {
                        question();
                        break;
                }
                // Simulate reloading program,
                case '7' : {
                        reload();
                        break;
                }
                // Clock
                case '6' : {
                        do {
                                time();
                                cout<<"Press Enter to exit";
                                Sleep(750);
                        } while(_getch() != '\n');
                        break;
                }
                // Invalid response
                default : {
                        cout<<"Nope"<<endl;
                        break;
                }
        }
        return 1;
}
void CEditor::question (void) {
        // Open file and move to end of file.
        myFile.open("AI.txt",ios_base::app);
        myFile<<"\n----- new entry -----\n";
        // Clear console and ask questions.
        system ("CLS");
        cout<<"AI: What is your name?"<<endl;
        cin>>name;
        system ("CLS");
        myFile<<"Datalog"<<"\nName: "<<name<<endl;
 
        cout<<"AI: How are you feeling?"<<endl<<name<<": ";
        cin>>response;
        system ("CLS");
        if (response == character) {
                cout<<"Invalid answer";
        }
        myFile<<name<<" is feeling "<<response<<endl;
        cout<<"AI: What is your age "<<name<<"?"<<endl<<name<<": ";
        cin>>age;
        system ("CLS");
        myFile<<name<<"'s age is "<<age;
        myFile.close();
        if (! myFile)
        {
                cout<<"Error";
        }
}
void CEditor::reload (void) {
        system ("CLS");
        cout<<"Reloading";
        Sleep(1000);
        cout<<".";
        Sleep(1000);
        cout<<".";
        Sleep(1000);
        cout<<".";
}
int CEditor::time (void) {
                struct tm newtime;
        char am_pm[] = "AM";
        __time64_t long_time;
        char timebuf[26];
        errno_t err;
 
                // Clear the screen
                system("CLS");
        // Get time as 64-bit integer.
        _time64( &long_time );
        // Convert to local time.
        err = _localtime64_s( &newtime, &long_time );
        if (err)
        {
            printf("Invalid argument to _localtime64_s.");
            exit(1);
        }
        if( newtime.tm_hour > 12 )        // Set up extension.
                strcpy_s( am_pm, sizeof(am_pm), "PM" );
        if( newtime.tm_hour > 12 )        // Convert from 24-hour
                newtime.tm_hour -= 12;    // to 12-hour clock.
        if( newtime.tm_hour == 0 )        // Set hour to 12 if midnight.
                newtime.tm_hour = 12;
 
        // Convert to an ASCII representation.
        err = asctime_s(timebuf, 26, &newtime);
        if (err)
        {
           printf("Invalid argument to asctime_s.");
           exit(1);
        }
        printf( "%.19s %s\n", timebuf, am_pm );
        return 1;
}
 
int _tmain(int argc, _TCHAR* argv[])
{
        CEditor editor;
        editor.q = new CQuestion("How are you?:Good,Bad");
        cout<<editor.q->question()<<endl;
        for(int i=0;i<editor.q->answers->size();++i) {
                cout<<editor.q->answers->answer[i]<<endl;
        }
        system("pause");
		while(editor.menu());
        //while(editor.menu());
        return 0;
}
Last edited on
What's the problem?
The program I have created is an Editor. However, I plan to make it into an AI. It is written in c++. I have used notepad to contain the data it will need to hold questions and responses. I have several goals I would like to accomplish. I need help specifically in allocating memory. I have yet to understand what I can write that will allow it to regulate data properly. Thanks!
Last edited on
Is there anyone else who can help me out?
You know there's code brackets right? Does anyone read the welcome thread?
Sorry guys. It's been updated :P
Last edited on
closed account (3CXz8vqX)
So...you've read through the Dynamic Memory Allocation tutorials right?
Not yet ravenshade. I'll take a look at those, but I also need further help with the program.
I also need help with vectors. I know this is spaghetti code, but please know this can help me better understand where I can go with my programming in the future. Thanks, kiknwing11.
Topic archived. No new replies allowed.