Tally via Map/ I/O issues

I am having trouble getting my head around what exactly i am supposed to do.
I am trying to create an input on output method for reading in and writing into files. which seems easy enough, this is what i have for now

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
#include <iostream>
#include <fstream>
#include <string>
using namespace::std;
// read 
void readIn() {
	string temp;
	ifstream in;
	in.open("output.txt");
	if (in.is_open()) {

	}
	else {
		cout << "The file did not open.";
	}
	while (!in.eof()) {
		in >> temp;
	}
}

//write
void write() {
	ofstream outfile;
	string temp, in;
	cout << "Enter outfile name: ";
	cin >> in;
	outfile.open(in);
	//not sure about the input just yet
	outfile << temp;
}
}

but what i need these for, i need these so that i can take an infile full of strings and take the words that you read in from the example.txt and tally the strings in the file by inserting those strings into a map<string,int> object. the string part of the map will be the individual words read in from the file, and the int portion of the map is the number of times the string part has been inserted in the map.

This is what i have no clue how to do, or how to make the read and write work for the end result.
Last edited on
Your tasks consists of different steps.
1. Read all the words from the file,
2. Remove non-alpha chars
3. Add the word in the map, or increment the count if it already exists
4. Finally print the words and the count of the map
Here's a skeleton to get you started.
There are plenty of tutorials on how to read a string from a file.
There are also plenty of posts here about removing unwanted chars from a string.
On how to print the map using an iterator or for-loop have a look at the reference here on the forum.
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
#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <cstdio> // perror, errno

using namespace std;

void input_words(map<string, int> & words);
void output_words(map<string, int> & words);
string remove_non_alpha(const string& s);

int main()
{
  map<string, int> words;
  input_words(words);
  output_words(words);
}

void input_words(map<string, int> & words)
{
  // TO DO read the words from the file, 
  // remove unwanted chars like .,! etc.
  // and store them in the map
}

void output_words(map<string, int> & words)
{
  // TO DO iterate through the map and print each word
  // and it's count
}

string remove_non_alpha(const string& s)
{
  string retval;

  // TO DO loop through string s and 
  // add each alphabetic char to retval
}
I am away from a compiler at the moment so i dont know if its functional, but i am trying to work through your template.
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
void inputWords(map<string , int> &words){
//read words from the file and store them in a map
ifstream in;
	string temp,tempName;
 cout<<"Enter Filename: ";
	cin>>tempName;
	in.open(tempName);
	if(in.isopen()){	
	while(!in.eof()){
		in>>temp;
words>>temp;
	}
	}
	else{
	cout<<"There was an error. \n";	
	}
	in.close();
}
void outputWords(map<string , int> &words){
//iterate through the map and print to outfile
	ofstream outfile;
	outfile.open("output.txt");
	map<string, int>::iterator it;
	for(it=words.begin(); it!words.end();it++){
	outfile<<it->first<<:<<it->second<<"\n";
	}
	outfile.close();
}
void main() {
map<string,int> words;
	inputWords(words);
	cout<<"Printing to output.txt \n";
	outputWords(words);
	
}

I am ignoring the remove non-alpha right now because the input example file i have is all alpha no numeric. but it is always good practice to write those functions so i will at some point any way. but how is my code going? am i moving in the right direction?
Last edited on
thoughts on how to improve?
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
void inputWords(map<string , int> &words){
//read words from the file and store them in a map
ifstream in;
	string temp,tempName;
	int i = 0;
 cout<<"Enter Filename: ";
	cin>>tempName;
	in.open(tempName);
	if(in.is_open()){	
	while(!in.eof()){
	// i dont know what goes here.
		in >> temp;
		words.insert(std::pair<string,int>(temp, i));
		i++;
	}
	}
	else{
	cout<<"There was an error. \n";	
	}
	in.close();
}
void outputWords(map<string , int> &words){
//iterate through the map and print to outfile
	if (!words.empty()) {
		ofstream outfile;
		outfile.open("output.txt");
		map<string, int>::iterator it;
		cout << "Printing to output.txt \n";
		for (it = words.begin(); it != words.end(); it++) {
			outfile << it->first << ":" << it->second << "\n";
		}
		outfile.close();
	}
}
void main() {
map<string,int> words;
	inputWords(words);
	outputWords(words);
	system("pause");
}

}
Last edited on
A better way to do it:
1
2
3
4
5
6
7
while(in >> temp)
{
  // TO DO remove non-alpha chars in temp.
  words[temp]++; 
  // words[temp] will return a ref to the count so you just increment it.
  // if temp is not in the map the map will create the entry with the count 0 and so it becomes 1
}
I am ignoring the remove non-alpha right now because the input example file i have is all alpha no numeric.

Bravo! That's the right way to do iterative development.
Topic archived. No new replies allowed.