Code Error Help

Hello, I'm working in Dev C++ and I had some problems with my code. It wont compile giving me an error signal. The code was implemented to read in a data file, get all the significant words (that dont appear twice, and get the frequencies of how many times they appear. Please Help :)

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
#include <iostream>
#include <map>
#include <interator>
#include <algorithm>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;
string toLower(string s)
{
	string data = s;
	std::transform(data.begin(), data.end(), data.begin(), ::toLower);
	return data;
	
 }

string trim(string str)
{
	stringstream trimmer;
	trimmer << str;
	str.clear();
	trimmer >>str;
	return str;
}

int main()
{
	
	map<string, int> item_count;
	string line;	// variable used to read a line from file
	int lines =0;  	// variable containing the numbers of transactions
	ifstream myfile("C:\Users\marqu_000\Documents\marketdata.txt");
	
	if(myfile.is_open())  // checking if the file was open
	{
		getline(myfile, line); // to ignore the first line which contains number of transactions 
		while(getline(myfile, line)) // read the file line by linr until end of file is reached
		{
			//now we are processing each line
			stringstream ss(line);
			int i;
			string item;
			// ignore Transection ID, #of Items
			getline(ss, item, ',');
			getline(ss, item, ',');
			while (getline(ss, item, ','))
			{
				item = trim(toLower(item));
				// Now the item variable is containing the item name
				map<string, int> ::interator itr = item _count.find(item);
				if (itr == item_count.end())
				{
					//means the element is not present
					item_count.insert(pair<string, int>(item, 1));
			
				}
				else
				{
					// increment the count
					itr->second = 1 + itr -> second;
				}
			}
		}
		// now traverse in the array and print entries which have count 1 
		cout << "unique item: " << endl;
		for( map<string, int>::const_iterator it = item_count.begin(); it ! = item_count.end(); ++it)
		{
			cout << it -> first << endl;
		}
	}
	cout << endl << "Items frequencies: " << endl;
	for(map<string, int>::const_iterator it = item_count.begin(); it ! = item_count.end(); ++it)
	{
		cout << it->first << ":" << it->second << endl;
	}
	myfile.close(); //closing the file
	}
	else 
	{
		cout << "Unable to open input file." << endl;
		return 1;
	}
	return 0;
}






this is the data file that it should read in this format:

1, 3, gum, bread, napkin //transecID, # of items, item1, item2,...
2, 1, bread
3, 2, napkin, bread
4, 5, salt ,soup, fork, bread, spoon
5, 1, napkin
line 3
<interator> is not a header file, you mean <iterator>


line 33
ifstream myfile("C:\Users\marqu_000\Documents\marketdata.txt");
Backslashes have to be escaped \\, just use forward slashes instead, they still work.
ifstream myfile("C:/Users/marqu_000/Documents/marketdata.txt");

line 51
map<string, int> ::interator itr = item _count.find(item);
interator typo again, you mean iterator.

line 67 & 73
for( map<string, int>::const_iterator it = item_count.begin(); it ! = item_count.end(); ++it)
The != operator cannot have a space between it.

line 79
1
2
3
4
5
	else 
	{
		cout << "Unable to open input file." << endl;
		return 1;
	}

You have an else without an if, move the else branch up to right below your if branch.
1
2
3
4
5
6
7
8
9
if
{
   ...
}
// no other code allowed here
else
{
    ...
}


EDIT:

line 13
std::transform(data.begin(), data.end(), data.begin(), ::toLower);
You're trying to do some weird recursive operation with mismatching types by mistake.
You mean ::tolower not ::toLower. Capitalization matters.
http://www.cplusplus.com/reference/cctype/tolower/
Last edited on
okay thank you so much. Now I am having problems with the map<string, do you see any more errors

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
#include <iostream>
#include <map>
#include <iterator>
#include <algorithm>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;
string tolower(string s)
{
	string data = s;
	std::transform(data.begin(), data.end(), data.begin()::tolower);
	return data;
	
 }

string trim(string str)
{
	stringstream trimmer;
	trimmer << str;
	str.clear();
	trimmer >>str;
	return str;
}

int main()
{
	
	map<string, int> item_count;
	string line;	// variable used to read a line from file
	int lines =0;  	// variable containing the numbers of transactions
	ifstream myfile("C:/Users/marqu_000/Documents/marketdata.txt");
	
	if(myfile.is_open())  // checking if the file was open
	{
		getline(myfile, line); // to ignore the first line which contains number of transactions 
		while(getline(myfile, line)) // read the file line by linr until end of file is reached
		{
			//now we are processing each line
			stringstream ss(line);
			int i;
			string item;
			// ignore Transection ID, #of Items
			getline(ss, item, ',');
			getline(ss, item, ',');
			while (getline(ss, item, ','))
			{
				item = trim(toLower(item));
				// Now the item variable is containing the item name
				map <string, int> :: iterator itr = item _count.find(item);
				if (itr == item_count.end())
				{
					//means the element is not present
					item_count.insert(pair<string, int>(item, 1));
			
				}
				else
				{
					// increment the count
					itr->second = 1 + itr -> second;
				}
			}
		}
		// now traverse in the array and print entries which have count 1 
		cout << "unique item: " << endl;
		for( map<string, int>::const_iterator it = item_count.begin(); it != item_count.end(); ++it)
		{
			cout << it -> first << endl;
		}
	}
	cout << endl << "Items frequencies: " << endl;
	for( map<string, int>::const_iterator it = item_count.begin(); it != item_count.end(); ++it)
	{
		cout << it->first << ":" << it->second << endl;
	}
	myfile.close(); //closing the file
	}
	else 
	{
		cout << "Unable to open input file." << endl;
		return 1;
	}
	return 0;
}
line 51: there's a space between item and _count - looks like you mean that to be item_count

map <string, int> :: iterator itr = item _count.find(item);

line 13: seems to be missing a comma between begin() and ::tolower

std::transform(data.begin(), data.end(), data.begin()::tolower);

To get the code to compile, I also changed the function name of islower on line 10 to not match the existing function that expects different parameters. At line 49 the code has isLower. Those names need to match, whatever you decide to call the function.

There's an extra closing } at line 71 that's ending the if started at line 35 too early.
OP, we're happy to help fix errors, but it is an invaluable asset to know how to decode the warnings/errors your compiler spits out yourself. Your compiler should give line numbers as part of the error message, to give you a hint for as to which code lines to look around. If you're on windows using the old Dev C++, I would suggest at least using Orwell Dev C++ (and even that is now a few years old, due to the compiler it comes with). Newer compilers will give better error/warning messages, with typo recommendations.

For future problems, copy the exact error message text, and the line number associated with it.
Last edited on
Ok, so the program runs but nothing is displayed after the code has ran. All it says is that the program was terminated

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
#include <iostream>
#include <map>
#include <iterator>
#include <algorithm>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;
string isLower(string s)
{
	string data = s;
	std::transform(data.begin(), data.end(), data.begin(),::tolower);
	return data;
	
 }

string trim(string str)
{
	stringstream trimmer;
	trimmer << str;
	str.clear();
	trimmer >>str;
	return str;
}

int main()
{
	
	map<string, int> item_count;
	string line;	// variable used to read a line from file
	int lines =0;  	// variable containing the numbers of transactions
	ifstream myfile("C:/Users/marqu_000/Documents/marketda.txt");
	
	if(myfile.is_open())  // checking if the file was open
	{
		getline(myfile, line); // to ignore the first line which contains number of transactions 
		while(getline(myfile, line)) // read the file line by linr until end of file is reached
		{
			//now we are processing each line
			stringstream ss(line);
			int i;
			string item;
			// ignore Transection ID, #of Items
			getline(ss, item, ',');
			getline(ss, item, ',');
			while (getline(ss, item, ','))
			{
				item = trim(isLower(item));
				// Now the item variable is containing the item name
				map <string, int> :: iterator itr = item_count.find(item);
				if (itr == item_count.end())
				{
					//means the element is not present
					item_count.insert(pair<string, int>(item, 1));
			
				}
				else
				{
					// increment the count
					itr->second = 1 + itr -> second;
				}
			}
		}
		// now traverse in the array and print entries which have count 1 
		cout << "unique item: " << endl;
		for( map<string, int>::const_iterator it = item_count.begin(); it != item_count.end(); ++it)
		{
			cout << it -> first << endl;
		}
	cout << endl << "Items frequencies: " << endl;
	for( map<string, int>::const_iterator it = item_count.begin(); it != item_count.end(); ++it)
	{
		cout << it->first << ":" << it->second << endl;
	}
	myfile.close(); //closing the file
	}
	else 
	{
		cout << "Unable to open input file." << endl;
		return 1;
	}
	return 0;
}


Data File

1, 3, gum, bread, napkin //transecID, # of items, item1, item2,...
2, 1, bread
3, 2, napkin, bread
4, 5, salt ,soup, fork, bread, spoon
5, 1, napkin



--------------------------------
Process exited after 0.7855 seconds with return value 0
Press any key to continue . . .

closed account (48T7M4Gy)
Your program runs with the following output:
unique item:
bread
fork
napkin
salt
soup
spoon

Items frequencies:
bread:3
fork:1
napkin:2
salt:1
soup:1
spoon:1
Program ended with exit code: 0


It also runs with the following output if the file can't be detected:
Unable to open input file.
Program ended with exit code: 1


Which IDE are you using? Sounds like VS? (now I see it's dev C++ from OP)
Last edited on
Topic archived. No new replies allowed.