Bool Function(int, string) help it does not work as i want

Hello Guys,

I have like this in a file :

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



I put each them in a array of strings called permitted group[9][5] defined globally the function readfile works fine as below.

Now the question is how can you do a function to check(I already tried !)
if(first value in the Permit_group1=="OK") &&
if(the second value in the Permit_group1=="str") && str: as in the parameters
if(the fifth value in the Permit_group1=="id") id: as in the parameters
?? how can I do that for all the 9 rows of the array


lets say I called check(1, green)

1
2
3
4
bool check(int id, string str){


}


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
void readfile(int fileid)
{
	
ifstream myfile (filename);
	
	int i=0;
  	if (myfile.is_open()){
    cout << "Opened " << filename << " for reading." << endl;
		while (!myfile.eof())
    		{
     			
				Permit_group1[i][0]=s1;
				Permit_group1[i][1]=s2;
				Permit_group1[i][2]=s3;
				Permit_group1[i][3]=s4;
				Permit_group1[i][4]=s5;
				i++;

    		}
	}
	else{
     		cout << "There was a problem opening the file "
			<< filename
             << " for reading."
             << endl;

  	}

	myfile.close();

	for(int c=0;c<9;c++){
		for(int j=0;j<5;j++){
		
		cout<<Permit_group1[c][j]<<"   ";
		}
		cout<<endl;
	}

}

Last edited on
closed account (3hM2Nwbp)
Are you supposed to write this in C or C++? I ask because I see conventions required in C being used as well as non-type-safe varardic methods.
In C++
closed account (3hM2Nwbp)
Be sure to understand what you're using out of this.

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
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
const unsigned int FIELDS = 5;
const unsigned int RECORDS = 9;

std::string Permit_group1[RECORDS][FIELDS];

void readfile(unsigned int fileID)
{
	std::string fileName;
	{
		std::stringstream ss;
		ss << fileName << fileID << ".txt";
		ss >> fileName;
	}
	std::ifstream myFile(fileName.c_str());
  	if (myFile.is_open())
  	{
		std::cout << "Opened " << fileName << " for reading." << std::endl;
		int i = 0;
		while (!myFile.eof()) // <-- Not a good idea unless you're dynamically allocating Permit_group1
    		{
    			myFile >> Permit_group1[i][0] >>
    				  Permit_group1[i][1] >>
    				  Permit_group1[i][2] >>
    				  Permit_group1[i][3] >>
    				  Permit_group1[i][4];
    			++i;
    		}
	}
	else
	{
     		std::cout << "There was a problem opening the file " << fileName << " for reading." << std::endl;

  	}
//	myfile.close(); Happens automatically when myFile goes out of scope
}

bool check(int id, const std::string& str)
{
	std::string idString; // <-- We need to convert `id` to a std::string
	{
		std::stringstream ss;
		ss << id;
		ss >> idString;
	}
	for(unsigned int i = 0; i < RECORDS; ++i)
	{
		if(Permit_group1[i][0] == "OK" && Permit_group1[i][1] == str && Permit_group1[i][4] == idString)
		{
			return true;
		}
	}
	return false;
}

int main()
{return 0;}
Thanks Luc lieber, you are right .

Thanks again for ur help
u r a good programmer
Last edited on
By the way how can I read many files like file0.txt
file1.txt in your code here

1
2
3
4
5
6
7
8
9
10
void readfile(unsigned int fileID)
{
	std::string fileName;
	{
		std::stringstream ss;
		ss << fileName << fileID << ".txt";
		ss >> fileName;
	}
	std::ifstream myFile(fileName.c_str());//"file"+fileName.c_str() ??
  	if (myFile.is_open())
Last edited on
closed account (3hM2Nwbp)
Have you read the documentation for the C++ features that you're using? One should understand their tools (Copy&Paste coding won't cut it when learning the basics).

std::string - http://www.cplusplus.com/reference/string/

std::ifstream - http://www.cplusplus.com/reference/iostream/ifstream/

--
To answer your questions:

1) const char*const* to std::string

Take a look at std::string's constructors

2) Read files with different names - there are many ways to skin a cat. Find one you're comfortable with. Problem solving is a major part of programming.

1
2
3
4
5
6
7
8
9
10
11
12
void readFile(const std::string& fileName)
{
    std::ifstream myFile(fileName.c_str());
    // ...
}

int main()
{
    readFile("file0.txt");
    readFile("file1.txt");
    return 0;
}
Ok Thanks, a lot for that, was very useful .
q1:does make deference if i had i++ in line 30 instead of ++i ??
q2:How do you do a pseudo code for this code in your way:
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
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
const unsigned int FIELDS = 5;
const unsigned int RECORDS = 9;

std::string Permit_group1[RECORDS][FIELDS];

void readfile(unsigned int fileID)
{
	std::string fileName;
	{
		std::stringstream ss;
		ss << fileName << fileID << ".txt";
		ss >> fileName;
	}
	std::ifstream myFile(fileName.c_str());
  	if (myFile.is_open())
  	{
		std::cout << "Opened " << fileName << " for reading." << std::endl;
		int i = 0;
		while (!myFile.eof()) // <-- Not a good idea unless you're dynamically allocating Permit_group1
    		{
    			myFile >> Permit_group1[i][0] >>
    				  Permit_group1[i][1] >>
    				  Permit_group1[i][2] >>
    				  Permit_group1[i][3] >>
    				  Permit_group1[i][4];
    			++i;
    		}
	}
	else
	{
     		std::cout << "There was a problem opening the file " << fileName << " for reading." << std::endl;

  	}
//	myfile.close(); Happens automatically when myFile goes out of scope
}

bool check(int id, const std::string& str)
{
	std::string idString; // <-- We need to convert `id` to a std::string
	{
		std::stringstream ss;
		ss << id;
		ss >> idString;
	}
	for(unsigned int i = 0; i < RECORDS; ++i)
	{
		if(Permit_group1[i][0] == "OK" && Permit_group1[i][1] == str && Permit_group1[i][4] == idString)
		{
			return true;
		}
	}
	return false;
}

int main()
{return 0;}


Last edited on
Topic archived. No new replies allowed.