need some help

Hi. I have to do an exercise in C++, I hope someone can help me. Here is the exercise:

Write a C++ Console application working in coloured text and graphical regime, which is doing the following things:

1. Including 3 characters long ordered strings from a text file;
2. Processing and graphically presenting the operation "sorting" of the included strings (for instance abt, dhj, boo, chw should be abt, boo, chw, dhj);
3. Processing and graphically presenting the operation "search" of a string (and showing up message if found or not in the text file);
4. Check-up if 2 (two) manually entered strings are equal (if they are, mark both in blue/green/yellow or whatever u want. If not, show a message that they are not equal);
5. Writing the data and results from 2. (new order) to a new text file.

For example, file named bla.txt contains:
1- afh
2- bow
3- khw
4- bjq
5- jqc
6- jwn etc..

It can be made with arrays, pointers or whatever else is possible. Thanks in advance..
Last edited on
Hi Ezio, welcome tto cplusplus forums.

Now it's against the rules for us to do your homework for you, so can you post any code that you have. We can point you in the right direction, but you have to do somethings yourself.

Write a C++ Console application working in coloured text and graphical regime


Does this mean the different colors one finds in an IDE editor? I am pretty sure your lecturer is not asking you do colors & grphics with your code.

2. Processing and graphically presenting the operation


Do you mean print things on the screen?

I am sure your lecturer has given you quite of bit of info to write some code and some good ideas on how to do it.

Looking forward to helping you out.
Here is the code, it's half C# and half C++, sorry for that.. I compile and run it successfully but after entering the lines to compare it crashes. I tried to debug it but I don't know where the problem is exactly and how to fix it. It seems to be a problem with the arrays.

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
#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
#include <Windows.h>
#include <dos.h>
using namespace std;
string line;
char lineouttofile[1000];
int brtz = 0;
HANDLE hConsole;
char **Mas;
int ReadFile ()
	{
		char imefile[10];
		cout<< "Name of the file: ";
		cin >> imefile;
		ifstream myfile (imefile);
		if (myfile.is_open())
			{
			while (! myfile.eof() )
				{
				getline (myfile,line);
				cout << line << endl;
				}
			myfile.close();
		}
		else cout << "Unable to open file!" << endl;
		return 0;
	}
int WriteFile ()
	{
		int ii = 0;
		for (int i = 0; i < line.length()-3; i+=4)
		{
			lineouttofile[i] = Mas[ii][0];
			lineouttofile[i+1] = Mas[ii][1];
			lineouttofile[i+2] = Mas[ii][2];
			lineouttofile[i+3] = ';' ;
			ii++;
		}
		ofstream myfile ("example.txt");
		if (myfile.is_open())
			{
		myfile << lineouttofile;
		myfile.close();
			}
		else cout << "Unable to open file!" << endl;
		return 0;
	}
void GetText2Masiv ()
	{
		int textlen = line.length();
		for (int i = 0; i < textlen; i++)
			{
			if (line[i] == ';')
			brtz++;
			}
		Mas=new char *[brtz];
		if (! Mas) {printf("Error!!! Not enough memory\n");
		return;}
		for ( i=0; i < brtz; i++)
			{
			Mas[i] = new char [3];
			if (! Mas[i]) { printf("Error!!! Not enough memory\n"); return;}
			}
		int k=0;
		for ( i = 0; i < brtz; i++)
		{
			Mas[i][0] = line[k];
			Mas[i][1] = line[k+1];
			Mas[i][2] = line[k+2];
			k+=4;
		}
	}
void ShowMasiv()
	{
		for (int i = 0; i < brtz; i++)
			{
			cout << i+1 << "- ";
			for (int j=0;j<3;j++)
			cout << Mas[i][j] ;
			cout << endl;
			}
	}
void IsEqual()
	{
		int pyrvatr;
		int vtoratr;
		cout << "Choose 2 strings to compare " <<endl;
		cout << "Choose the first one (it's number)" ;
		cin >> pyrvatr;
		pyrvatr = pyrvatr--;
		cout << "Choose the second one (it's number)" ;
		cin >> vtoratr;
		vtoratr = vtoratr--;
		if ((Mas[pyrvatr][0] != Mas[vtoratr][0]) || (Mas[pyrvatr][1] != Mas[vtoratr][1]) || (Mas[pyrvatr][2] != Mas[vtoratr][2]))
		cout << "Strings are not equal"<<endl;
		if ((Mas[pyrvatr][0] == Mas[vtoratr][0]) && (Mas[pyrvatr][1] == Mas[vtoratr][1]) && (Mas[pyrvatr][2] == Mas[vtoratr][2]))
		cout << "Strings are equal"<<endl;
	}
void FindTroika()
	{
		hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
		char tyrs[10];
		cout << "Enter a string for searching " <<endl;
		cin >> tyrs;
		if (strlen(tyrs) > 3)
			{
				cout << "Enter up to 3 symbols "<<endl;
		exit(0);
	}
	bool finded = false;
	for (int i = 0; i < brtz; i++)
		{
			if (Mas[i][0] == tyrs[0] && Mas[i][1] == tyrs[1] && Mas[i][2] == tyrs[2])
				{
					finded = true;
					SetConsoleTextAttribute(hConsole, 3);
					cout << Mas[i][0] << Mas[i][1] << Mas[i][2] << endl;
				Sleep(500);
				}else
					{
						SetConsoleTextAttribute(hConsole, 6);
						cout << Mas[i][0] << Mas[i][1] << Mas[i][2] << endl;
						Sleep(500);
					}
		}
		SetConsoleTextAttribute(hConsole, 7);
		if ( finded )
		cout << "The string you were searching is found! " <<endl;
		else cout << "The string you were searching is not found! " <<endl;
	}
void SortTroika ()
	{
		hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
		system("cls");
		int color = 3;
		int elem1;
		int elem2;
		SetConsoleTextAttribute(hConsole, 5);
		ShowMasiv();
		cout << endl;
		char temp[3];
		for (int i = brtz; i >= 0; i--)
			{
				for (int j = 0; j < i-1; j++)
					{
						if ( int(Mas[j][0]) > int(Mas[j+1][0]) )
							{
								color = 2;
								elem1 = j;
								elem2 = j+1;
								temp[0] = Mas[j][0];
								temp[1] = Mas[j][1];
								temp[2] = Mas[j][2];
								Mas[j][0] = Mas[j+1][0];
								Mas[j][1] = Mas[j+1][1];
								Mas[j][2] = Mas[j+1][2];
								Mas[j+1][0] = temp[0];
								Mas[j+1][1] = temp[1];
								Mas[j+1][2] = temp[2];
								for (int ki = 0; ki < brtz; ki++)
									{
										if (ki == elem1 || ki == elem2)
										color = 2;
										else
										color = 3;
										SetConsoleTextAttribute(hConsole, color);
										cout << ki+1 << "- ";
										for (int kj=0; kj<3; kj++)
										cout << Mas[ki][kj];
										cout << endl;
									}
								}
							cout << endl;
							Sleep(1500);
					}
			}
		SetConsoleTextAttribute(hConsole, 7);
		cout << endl;
	}
int main()
	{
		ReadFile();
		GetText2Masiv();
		ShowMasiv();
		IsEqual();
		FindTroika();
		SortTroika();
		ShowMasiv();
		WriteFile();
		for (int is=0; is < brtz; is++)
		delete []Mas[is];
		delete []Mas;
		return 0;
	}


TheIdeasMan the program should just mark text with different colours. For instance when comparing 2 equal lines they should become green or blue etc.. When searching a string, if it presents in the TXT file it should get another colour. That's what I mean by the word "graphical". The lecturer love colourful programs, that's it. For your second question, in point 2 and 3 just to show the current result with colourful text of the operations done. Here is an example

http://imageshack.us/photo/my-images/209/92367902.jpg/
Last edited on
Well having read your description of the problem, I wasn't expecting to see all this code !

Sorry about the tone of my reply, but it sounded like someone doing their very first assignment, and that is obviously not the case here. My apologies.

You have Mas as a pointer to pointer to char. In GetText2Masiv you have :

Mas=new char *[brtz];


which is an array of pointers to a single char. Then you have

Mas[i] = new char [3];


Which is an array of 3 chars


Would it be better to declare it as a 2d array and initialise it with C++ strings?

hope that helps
TheIdeasMan thanks for the support but I totally gave up with this after a long time trying to get it work. I have to give it tomorrow that's why I asked directly if someone can do it or fix this little problem just to finish it. I am not sure what exactly you mean and probably will take me a lot of time to finish it and I don't have time to spare with it so if s.o can finish it I'll be grateful.
Would it be better to declare it as a 2d array and initialise it with C++ strings?


It shouldn't take too long to do that.

There could be other problems in your code.

maybe you need to pull an all nighter!
I should go from scratch. Can anyone give me the inbuilt functions for the operations I mentioned in C++?
I wouldn't start again if it has to be in tmrw. Save this version as backup so you still have something.

I will ask for an extra day (27th June). As far as I see only the first function is working fine or maybe this array is the main problem and breaking the rest functions.. I know it would be better by using the built in functions in C++ as they will prevent any loops but I am not familiar with them.
Hi Ezio,

Here is another thought hope it's not too late:

sounds like you need an array that holds strings of length 3, like this


abc,
def,
ghi,
jkl



However, I think you a trying to do a 2d array like this


a,b,c,
d,e,f,
g,h,i,
j,k,l



The only thing then would be your comparison function. There are comparison already built in to C and C++. In C it is the function strcmp, and in C++ here is an example using std::string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// comparing apples with apples
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str1 ("green apple");
  string str2 ("red apple");

  if (str1.compare(str2) != 0)
    cout << str1 << " is not " << str2 << "\n";

  if (str1.compare(6,5,"apple") == 0)
    cout << "still, " << str1 << " is an apple\n";

  if (str2.compare(str2.size()-5,5,"apple") == 0)
    cout << "and " << str2 << " is also an apple\n";

  if (str1.compare(6,5,str2,4,5) == 0)
    cout << "therefore, both are apples\n";

  return 0;
}


Here is the example for strcmp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* strcmp example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char szKey[] = "apple";
  char szInput[80];
  do {
     printf ("Guess my favourite fruit? ");
     gets (szInput);
  } while (strcmp (szKey,szInput) != 0);
  puts ("Correct answer!");
  return 0;
}


Hopefully this will help you now for your assignment, if not good knowledge for the future.

Cheers
Topic archived. No new replies allowed.