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
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.
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h> // for tolower-function
#include <vector> // use dynamic-array
usingnamespace 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)
returntrue;
}
returnfalse;
}
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();
}