hi please help me to solve this challenging Question


Hello Chaps,

Plz do ur best to help me I am a bit frustrated, can we solve this ??

I have like this in a file1.txt

OK Green > start 1                
OK Green > start 2                
No Green > start 3               

OK orange > start 1               
No orange > start 2               
No orange > start 3             

OK red > start 1                  
OK red > start 2                  
OK red > start 3  




File2.txt has this

OK Green > start 1 
No Green > start 2
Ok Green > start 3
No orange > start 1
Ok orange > start 2
No orange > start 3
OK red > start 1
OK red > start 2
No red > start 3
OK Green > start 1// note: the content of file1 again start from here
Ok Green > start 2
No Green > start 3
OK orange > start 1
No orange > start 2
No orange > start 3
OK red > start 1
OK red > start 2
OK red > start 3



how to make a function check() using std:map to do this :

for ex-

if (2 statements in file2.txt[18 commands] (Green Green) and (OK OK) return true

if both(Green Green)in file2.txt and (OK NO) return True

if both(Green Green) in file2.txt and (No Ok) return False

if both(Green Green) in file2.txt and (No No) return False

How can we do a compatible check for file1 as well as long it works properly with file2 ?? can be ;)


I think the associative map should have the (color + start_id) as the key, and the number of "OK" instances as the value.


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
void readfile(int id)
{
const unsigned int FIELDS = 5;
const unsigned int RECORDS = 18;//change here
std::string P2[RECORDS][FIELDS];
	
	char filename[ 1024 ];
	sprintf( filename, "file%d.txt", id );	//1
	ifstream myfile (filename);
	
  	if (myfile.is_open())
  	{
		std::cout << "Opened " << filename << " for reading." << std::endl;
		int i = 0;
		while (!myfile.eof()) // 
    		{
    			myfile >> P2[i][0] >>
    				  P2[i][1] >>
    				  P2[i][2] >>
    				  P2[i][3] >>
    				  P2[i][4];
    			++i;
    		}
	}
	else
	{
     		std::cout << "There was a problem opening the file " << filename << " for reading." << std::endl;

  	}
	
}
Last edited on
Stop using streams. You're programming in a windows environment I would suggest using the native Windows API:

CreateFile
ReadFile
CloseHandle

Read your entire file into one memory buffer. As far as parsing your text file, use strtok() to tokenize the file into lines, and then process each line further tokenizing by spaces or however you want to do it. If you need to store all of this information into an array or matrix (an array of arrays), then you will need to probably find out how many lines there are in the text file, and dynamically allocate memory big enough to hold it.
Last edited on
Stop using streams.
Why?
Mainly because I don't personally like them and I prefer to use Windows API with multi-byte charset. I find unicode to be an annoyance.

If you want to ignore what I said about streams... you can't stream something like that into a multidimensional array and expect the computer to automatically tokenize and parse it for you. You need to code a parser by using string tokenization.
1. Unicode is the simplest of them all. Why is it annoying? Annoying is to have to convert from ANSI/Multi-byte to Unicode every time you want to pass a string to, say, a COM object. Now that's annoying!
2. Don't use strtok() unless you absolutely have to. Instead, if available, use strtok_s(). I say if available because I don't remember if it is Microsoft-specific.
3. I also am a fan of the Windows API, but in here you'll find people that like to write portable code, so dismissing streams like that is probably not going down well for some, hehe.
you can't stream something like that into a multidimensional array and expect the computer to automatically tokenize and parse it for you.

Yes, you can. Because that's exactly what it does.
I don't use standard C++ streams either, but to unnecessarily recommend using unportable WinAPI functions for something basic like reading a file and to recommend C functions like strtok as an "alternative" to C++ streams is ridiculous.

find out how many lines there are in the text file, and dynamically allocate memory big enough to hold it.

Or, you know, you could use a vector like a normal person.
Topic archived. No new replies allowed.