errors

Im a bit lost. dont understand the errors Im getting from this. Anyone can help?

error C2059 (line with for loop)
error C2228 (next line with while... .empty and with .back)
1
2
3
4
5
6
7
8
 void key()
{
  for (int level; string shortName ); {
    while (!level.empty() && level.back() >= level)
      levels.pop_back(), add.erase(hangy.size()-2);
    level.push_back(level);
    add += levelContent1 + ".";
}
Last edited on
It might help if you told is what those errors actually are. Why would you withold useful information from us, if you want us to help?
ya sure.... the problem is that its in german XD thats why i just wrote the errors and the line from the code...

the error C2059 was a synatx error with a )
the error C2228 was about .empty and .back. this should have a class
1) A for statement is composed of three parts, not two as you've written it. You need to go back to your books and learn how to write them properly.

2) The semicolon you've placed on line 3 between the closing bracket and the opening brace probably shouldn't be there. It will cause your for loop to do nothing. It's the equivalent of writing:

1
2
3
4
5
6
7
  for (int level; string shortName )
  {
    ;   // <----- EMPTY STATEMENT
  }
  {
    while (!level.empty() && level.back() >= level)
    // ... 


3) The close brace on line 8 matches the open brace on line 3. In the code you've posted there's no closing brace to match the open brace on line 2. Looking at your indentation, it looks as though the one on line 8 is supposed to end the function, and you've forgotten to end the block that you open at the end of line 3.

4) You've defined level as an int on line 3. How does it make sense to try and call methods such as empty, back and push_back on it?
Last edited on
thx...... got it now... no more errors but its not working how i hoped.....

example:
i read a file like this...
1
2
3
4
5
6
7
8
Level shortName Addresse 
1          A         123 
2          B         234 
3          C         345 
3          D         456 
4          E         567 
3          F         678 
4          E         789


and want a output like this....
1
2
3
4
5
6
7
8
Key        Address
A          123
A.B        234
A.B.C      345
A.B.D      456
A.B.D.E    567
A.B.F      678
A.B.F.E    789 


want to create the key out the info from the file. on this way i would have a unique key. but i dont know how to create it....any help on this?
i already have this code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
while(pointer_mychar != NULL) /* if pointer_mychar is not empty */
            {
                if (i>=MinColumnSize && i<  MaxColumnSize) /*Reading colums which is defined at the beginning. MinColumnSize =0 and MaxColumsSize = 6 */  
                { 
                    if (pairfind == 0){  
                        tagType = pointer_mychar; /* First column is the tagType */
                        pairfind = 1;
                    } 
	   else if (pairfind == 1) {  
                        level = pointer_mychar; /* Second column is the level */
                          pairfind = 2;
                    } else if (pairfind == 2) {  
                        shortName = pointer_mychar; // third column is the short Name   */
                     string levelContent1; /* Content from level 1 */
                     string levelContent2; /* Content from level 2 */
	     string levelContent3; /* Content from level 3 */
	     string levelContent4; /* Content from level 4 */
                     string levelContent5; /* Content from level 5 */
                     string levelContent6; /* Content from level 6 */
                     if (level == "1") {
	          levelContent1 = pointer_mychar; /* if level 1 then load the content (shortName) to pointer_mychar */
                      } else if ( level == "2") {
	         levelContent2 = pointer_mychar; /* if level 2 then load the content (shortName) to pointer_mychar */
		.....					             



the levelContent 1-6 (because of 6 levels) loads actually the shortName. now i need a function which connects the levelContents in the correct order.....
Is this what you want to do?
do you want to store the key pair in file or just in memory?

try looking up
http://www.cprogramming.com/tutorial/stl/stlmap.html

1
2
3
4
5
6
7
8
9
10
if (level == 1 && shortname==A)
{
// a here is key and 123 is the address.
    A = 123;
}

if(level == 2 && shortname == B)
{
  A.B =  234;
} 
Last edited on
first i read the and then a want to store the key and the address in a map function. but the prob ist, that i dont have the key.... the key has to be created by reading the map and creating the key by combining level and shortname. like in the example.

sooo the shortname and level is in memory and when i have the key,.... i store it in a map.....


the loop u gave... for that i need to know the shortname right? but i dont know it....

i thought you had the level and shortname when you read the file together with the address.

in the example you have given you will have problems because you have identical values.

if you just comparing the level and shortname

1
2
4  E  //567
4  E  //789 





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
#include <string>
#include <set>
#include <exception>
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <vector>

#include <boost/filesystem.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/foreach.hpp>

using namespace boost::filesystem;
using namespace std;

struct job
{
	std::string data_path;
	int address;
	void load(const std::string &filename, std::vector<string> &key, std::vector<int> &address);
	void save(const std::string &filename, std::vector<string> &key, std::vector<int> &address);
};

int main()
{
	// example data
	static const int arr[] = {1,2,3,3,4,3,4};
	vector<int> Level (arr, arr + sizeof(arr) / sizeof(arr[0]) );
	
	static const char letter[]={'A','B','C','D','E','F','E'};
	vector<char> shortname;
	for( int i =0; i < sizeof(letter); i++)
	{
		shortname.push_back(letter[i]);
	}

	static const int add[] = {123,234,345,456,567,678,789};
	vector<int> address (add, add + sizeof(add) / sizeof(add[0]) );
	
	vector<string> key;

	// display output
	cout << "Level " << "shortname " << " address " << " generated key" << endl;
	for (int i =0; i < address.size(); i++)
	{
		stringstream temp;
		temp << Level[i] << shortname[i];
		key.push_back(temp.str());
		cout << Level[i] << "      " << shortname[i] << "         " << address[i] << "       " << temp.str() << endl;		
	}

	// create instance of job
	job *ds = new job;

	// path name of where data will be stored.
	std::string path("C:\\temp\\hello.txt");

	// call save method
	ds->save(path.c_str(),key,address);
		
	// clear vector
	address.clear();

	// call load method
	ds->load(path.c_str(),key,address);

	cout << endl;
	cout << "generated key " << "      address" << endl;

	// you will have a problem displaying 4E will be overwritten by the last value 

	// display loaded data
	for (int i =0; i < address.size(); i++)
	{
		
		cout << key[i] << "          " << address[i] << endl;
	}

	int x;
	cin >> x;
}

void job::save(const std::string &filename, std::vector<string> &key, std::vector<int> &address)
{
	using boost::property_tree::ptree;
	ptree pt;

	// loop through the key and address vector 
	for(int i=0; i < key.size(); i ++)
	{
		pt.put(key[i],address[i]);
	}	

	// write to file
	write_ini( filename, pt );
}

void job::load(const std::string &filename, std::vector<string> &key, std::vector<int> &address)
{
    // Create empty property tree object
    using boost::property_tree::ptree;
    ptree pt;

	boost::property_tree::ini_parser::read_ini(filename.c_str(), pt);
	
	// load data
	for(int i=0; i < key.size(); i++)
	{
		address.push_back(pt.get<int>(key[i]));
	}	
}


the output of the program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Level shortname  address  generated key
1      A         123       1A
2      B         234       2B
3      C         345       3C
3      D         456       3D
4      E         567       4E
3      F         678       3F
4      E         789       4E

generated key       address
1A          123
2B          234
3C          345
3D          456
4E          789
3F          678
4E          789
Last edited on
hey, thx for ur example... :)

i think u will get a better idea when i give u my programm code.

the problem ist how u mention it before that i have comparing the level and shortname just with an other addresss.....thtas why the idea to create a shortname path to have a unique shortname like a key.

give me some time. i can show u my programm code with few lines of the table which i use.....
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
#include <iostream>
#include <fstream>
#include <string>
#include <string.h> 
#include <map>
#include <vector>
#include <utility>
#include <sstream>
#include <stdio.h>
#include <time.h>
using namespace std;
#define FILEPATH "...." /* Enter The Filepath here. */
#define MinColumnSize 0
#define MaxColumnSize 6

typedef struct ODK_DATA_STRUCT{    
  unsigned char       dataType;           /* Data Type of memory */ 
  unsigned short      quantity;            /* Quantity of data types */    
  unsigned short      dbNumber;         /* DB number if data type is a DB */   
  unsigned char       memoryArea;      /* Memory Area type */  
  unsigned short      areaOffset;        /* Offset into memory area */    
  unsigned char       bitNumber;         /* Bit number of data */    
  unsigned char*      pBuff;                /* Pointer to the data buffer */   
  unsigned short      maxSize;           /* Maximum size in bytes of buffer */   
  unsigned int        status;                /* Status of the read/write operation */
} ODK_DATA_STRUCT;/* Data structure for use with Read/Write functions */
class TableReader {
    map<string, ODK_DATA_STRUCT> dataMap;   
public:
    ODK_DATA_STRUCT dataStructForShortName(string shortName);
    void getAddress(string searchname);
    TableReader();
    ~TableReader();	    
private:
    map<string, ODK_DATA_STRUCT> loadMapFromCSV();
};
unsigned short shortFromString(string string) {
    stringstream ss(string);
    unsigned short i;
    if( (ss >> i).fail() ) {        
            return i; 
   } else {   
        return i;
    }}
void replaceAll(std::string& str, const std::string& from, const std::string& to){ 
    if(from.empty())
    return;
    size_t start_pos = 0;
    while((start_pos = str.find(from, start_pos)) != std::string::npos) {
        str.replace(start_pos, from.length(), to);
        start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
    }}
/* Used to split strings by given delimiter (for analyzing the rtName) */
void split(const string& s, char c, vector<string>& v) { 
    string::size_type i = 0;
    string::size_type j = s.find(c);
    while (j != string::npos)  {
        v.push_back(s.substr(i, j-i));
        i = ++j;
        j = s.find(c, j);
        if (j == string::npos)
            v.push_back(s.substr(i, s.length( )));
    }}
void key(){
    vector<int> vectorLevel;
    string add;
    string key;
    string levelContent1;
    for (int level, shortName; cin >> level >> shortName;)  { 
        while (!vectorLevel.empty() && vectorLevel.back()>=level)
vectorLevel.pop_back(), add.erase(add.size()-2);
vectorLevel.push_back(level);
add += shortName + ".";
        cout << add << endl;
    }}
TableReader::TableReader() {
    dataMap = loadMapFromCSV();
}
TableReader::~TableReader() {
    /*   delete dataMap;*/
}
map<string, ODK_DATA_STRUCT> TableReader::loadMapFromCSV (){      
    ifstream MyFile;
    string StringLine;
    int z=0; /* Used for endless while loops */
    string Line = " ";
    char delimiter[] = ";"; 
    char* pointer_mychar; 
    map<string, ODK_DATA_STRUCT> names;    
    MyFile.open (FILEPATH); 
    if (!MyFile){
        printf ("Error, can not open the file!\n"); 
        throw false;
    } else
        /* printf ("File opened!\n"); */
        MyFile.seekg (0, ios::beg);  // to start at be beginning
     while(z<1)  {
        string tagType; /* tagType given in the CSV file */
        string level; /* level given in the CSV file */
        string shortName; /* shortName given in the CSV file */
        string rtName; /* rtName given in the CSV file */
        string dataType; /* dataType given in the CSV file */
        string accessRights; /* accessRights given in the CSV file */
        string datatypeStringFromTable; /* data Type given in the CSV */
        int dataTypeNumRep; /* integer representation of the datatypeStringFromTable */
        ODK_DATA_STRUCT dataStruct; /* data struct to be filled */
        vector<string> v; /* vector to analyze the two parts of the rtName -> firstPartOfrtName and secondPartOfrtName */       
        getline(MyFile, StringLine); /* reading the full line */
        if (StringLine != Line) /* If the line is not empty */ {
            Line = StringLine;
            pointer_mychar = &StringLine[0];             
            pointer_mychar = strtok(pointer_mychar, delimiter);
            int i=0;
            int pairfind = 0; 
            while(pointer_mychar != NULL) {
                if (i>=MinColumnSize && i<  MaxColumnSize) /*MinColumnSize =0 and MaxColumsSize = 6 */  { 
                 if (pairfind == 0) 
	 tagType = pointer_mychar; /* First column is the tagType */
                 pairfind = 1;
                 } else if (pairfind == 1) {  
                        level = pointer_mychar; /* Second column is the level */
                        pairfind = 2;
                  } else if (pairfind == 2) {  
                       shortName = pointer_mychar; // third column is the shortName */
	string levelContent1; /* Content from level 1 */
	string levelContent2; /* Content from level 2 */
	string levelContent3; /* Content from level 3 */
	string levelContent4; /* Content from level 4 */
	string levelContent5; /* Content from level 5 */
	string levelContent6; /* Content from level 6 */
                if (level == "1") {
	levelContent1 = pointer_mychar; /* if level 1 then load the content (shortName) to pointer_mychar */
	}else if ( level == "2"){
	 levelContent2 = pointer_mychar; 	
                }else if ( level == "3"){
	levelContent3 = pointer_mychar	
                } else if ( level == "4"){
	levelContent5= pointer_mychar; 	
                }else if ( level == "5") {
	levelContent5 = pointer_mychar; 	
                } else if ( level == "6") {
	levelContent6 = pointer_mychar; 	
                }
                 pairfind = 3;
                 } else if(pairfind == 3) {
                  rtName = pointer_mychar; /* fourth column is the rtName */
                  size_t position =rtName.find("]"); /* The project name ist not the same in every project. For a simple handling just find the symbol ] and erase the part before it */
                  rtName.erase(0, position+1);           
                  split(rtName, ',', v); /* rtName is splited by a , which */
                  pairfind = 4;
                  }else if(pairfind ==4){
                  dataType = pointer_mychar; /* fifth column is the dataType. This column ist not really necessary*/
	   pairfind = 5;
	  } else if (pairfind == 5) {
                   accessRights = pointer_mychar; /* sixth column is the accessRights */		              
                   pairfind = 6;
                  } else {
                   datatypeStringFromTable = pointer_mychar;
                   dataTypeNumRep = i;
                   pairfind = 0; /* Resetting pairfind for a new row */
                    }}
                 pointer_mychar = strtok(NULL, delimiter);
                 i++;
            }} else {
            break;
        }
        /* analyzing the two parts of rtName */
        string firstPartOfrtName;
        string secondPartOfrtName;      
        if (v.size() == 2){
            firstPartOfrtName = v[0]; 
            secondPartOfrtName = v[1]; 
        } else {
            firstPartOfrtName = "";
            secondPartOfrtName = "";
        }
        /* analyzing firstPartOfrtName */
        if (firstPartOfrtName.find("DB") != string::npos) {
            dataStruct.memoryArea = ODK_MEM_AREA_DB;
            replaceAll(firstPartOfrtName, "DB", "");
            dataStruct.dbNumber = shortFromString(firstPartOfrtName);
        } /*following other types...*/
Last edited on
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
/* Second part of RTName after the comma */
        if (secondPartOfrtName.find("STRING") != string::npos) {
            replaceAll(secondPartOfrtName, "STRING", "");
            dataStruct.dataType = ODK_DATA_TYPE_STRING;
            vector<string> p;
} /*following other types...*/
names.insert(std::pair<string, ODK_DATA_STRUCT>(shortName, dataStruct)); // Inserting finished pair into map
}
  cout<<"Read Process Complete!"<<endl; 
  cout<<"Map size:"<< names.size()<<endl;
  MyFile.close(); 
  return names;
}
ODK_DATA_STRUCT TableReader::dataStructForShortName(string shortName){
ODK_DATA_STRUCT dataStruct=dataMap[shortName];
return dataStruct;    
}
int main (){ 
 TableReader tableReader;
 while (true){
 cout<<"Enter string to search for"<<endl;
 string searchString;
 cin>>searchString;
 ODK_DATA_STRUCT dStruct=tableReader.dataStructForShortName(searchString); /*How to call the previous function*/
  string dataTypeName;
  if(dStruct.dataType==ODK_DATA_TYPE_BOOL){
  dataTypeName="BOOL";
   }/*following other types...*/
string memAreaName;
 if(dStruct.memoryArea==ODK_MEM_AREA_A){
  memAreaName = "Process image output";
  }/* following other types...*/
cout<<"Data Type:"<< dataTypeName<<endl;
cout<<"Quantity:"<< dStruct.quantity<<endl;
cout<<"db Number:"<< dStruct.dbNumber<<endl;
cout<<"Memory Area:"<< memAreaName<<endl;
cout<<"Area Offset:"<< dStruct.areaOffset<<endl;
printf("bitNumber:%d\n", dStruct.bitNumber );
//cout<<"pBuff:"<< dStruct.pBuff<<endl;  
cout<<"max Size:"<< dStruct.maxSize<<endl;
// cout<<"status:"<< dStruct.status<<endl;      
cout<<"--------------------"<<endl;
}       
return 0; }



this is how the table looks like


Level ShortName RTName
1 jet
2 WinLC RTX F
3 EXEC_COM ....]SFB65002
4 REQ ....]DB6509,X0.0
4 ODK_OUT
5 Done ....]DB6509,X46.0
4 ODK_Status ....]DB6509,WORD56
3 DB_OPC_DISCONNECT ....]DB65084
4 REQ ....]DB6508,X0.0


If i want to get the first REQ, i want this key
jet.WinLC RTX F.EXEC_COM.REQ
the address here will be ....]DB6509,X0.0


and if i want my second REQ, i want this key
jet.WinLC RTX F.DB_OPC_DISCONNECT.REQ
the address here will be ....]DB6508,X0.0

i want my key as my input and the output should be the address.... but no idea how do wirte my code or to change it so i get this function.


Last edited on
Is that all the contents of the file?
would it be possible for you to paste the whole file you are reading?
achutally there are just few more columns but i dont need it for my task so i skip the reading those columns..... thats y i didnt posted it.

TagType; Level; ShortName; RTName; DataType; AccessRights ; 102; 103

there are the columns..... the info in it are just few numbers.
Last edited on
Topic archived. No new replies allowed.