let's solve this geeks

Hello Chaps,

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

The summary of this question is exist at the end in BOLD

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
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
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;

  	}
	
}

bool check1(int id, string priority) //priority (color type)recv the Red, Green or Orange

{
	std::string idString; // <-- We need to convert `id` to a std::string
	{
		std::stringstream ss;
		ss << id;
		ss >> idString;
        int counter=0;
	 }
	for(unsigned int i = 0; i < RECORDS; ++i)
	{
		if((p2[i][0] == "OK") && (P2[i][1] == priority) && (p2[i][4] == idString))
		{

                     counter++;
			}
	    }
	
	if((counter==1) || (counter==2) ){//thats work but it is strongly wrong ! as 1 means manythings here ! lets say I call in function check (1, green)
// then file2 will return true as the counter is 2 but if file 2 has  one 
//statement OK GREEN > start 1 will return true bcz the counter value is 1 as well which is wrong ! to be more clear see below 


		return true;	}
	else {
		return false;
					}
}


IF wefound
OK GREEN > start 1
OK GREEN > start 1
counter==2, Then return true

IF wefound OK NO like in
OK GREEN > start 1
NO GREEN > start 1
counter==1, Then return true// that's confusing what if there is only 1 statement OK GREEN > start 1 like in file1 !!


IF wefound NO OK like in
NO GREEN > start 1
OK GREEN > start 1
counter==1, Then return False // note in code above it return true so i need to distinguish between this case and the previous one 

IF wefound
(counter==0 ) whch means NO NO then return Fasle 
Last edited on
Normally people won't outright give you answers anyways, but name calling and stereotyping isn't going to help you.
Topic archived. No new replies allowed.