writing words from txt to an array and remove duplicate

Hi, im working on a program that reads from a text document. the first line has a number, that suppose to say how long the array suppose to be, and each other line has an email address. I am suppose to write a program that reads from one txt document and writes to another each email address, removing any duplicates.
I understand how to call a text document, and read and write, however i do not understand how to store words in an array? Ive only worked with very basic arrays and dont knwo where to go from here.
heres what i have so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

int main() {
string name;
ifstream indata;
indata.open("rawlist.txt");
string size;
indata>>size;
string email(size);

while (indata >> name) {
cout << name << endl;
email=name;
}
cout<<"testing             "<<email[5];
indata.close();
}



and the txt document looks like
5
alic@yahoo.com
kobe@hotmail.com
oyu@aol.com
oyu@aol.com
alice@yahoo.com

I am also suppose to ignore any capitolization, like if one is ball@hotmail.com and the other is Ball@hotmail.com, they are suppose to be seen as the same.
You need to declare the array - string email [5]; and also the MAX length of string you will permit. Use getline() to load each email string to an array element using a for loop. Use a similar for loop to print each element. During the write process you probably should include a statement which will convert any upper case characters to lower case.
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
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h> // for tolower-function
#include <vector> // use dynamic-array 

using namespace std;

bool emailexist(vector<string>& emaillist, string email){
	vector<string>::iterator it;
	for (it=emaillist.begin();it<emaillist.end();it++){ //check all elements in array
		if (*it == email)
			return true;
	}
	return false;
}

void main() {
	vector<string> emaillist;
	string filename = "rawlist.txt";
	ifstream quelle;
	quelle.open(filename.c_str(), ios::in);
	int size;
	string email;
	quelle >> size; //get number of elements
	for (int i=0; i<size;i++){
		quelle >> email;
		int j=0;
		while (email[j] != NULL){ //all letters to lower 
			email[j] = tolower(email[j]);
			j++;
		}
		if (!emailexist(emaillist,email)){	//only add if email doesnt exist in array, see function above
			emaillist.push_back(email);		//add element to emaillist
		}
	}
	vector<string>::iterator it;
	for (it=emaillist.begin();it<emaillist.end();it++){ //print all elements in array
		cout << *it << endl;
	}
	quelle.close();
}
Last edited on
Topic archived. No new replies allowed.