hi
i have a file dictionary.txt which contains a list of four-letter words which i have to read into a string vector.
i have written the following code
char* char2str(char); //convert single char to null terminated string
int main()
{
vector<string> dict;
ifstream in("dictionary.txt");
char *temp, ch;
int i=0;
while(in)
{
temp = new char[5];
while(i<4)
{
in.get(ch);
if(ch>='a' && ch<='z')
{
strcat(temp, char2str(ch));
i++;
}
}
dict.push_back(temp);
i=0;
delete temp;
}
for(int k=0; k<dict.size()-1; k++)
cout<<dict[k]<<endl;
system("PAUSE");
return 0;
}
where the function chsr2str is
char* char2str(char c)
{
char *str = new char[2];
str[0] = c;
str[1] = '\0';
return str;
}
the output of this program is
trap
game
?=frog
€?=plan
beam
grim
list
bane
span
true
Press any key to continue . . .
please tell me why do i get those characters before the 3rd and 4th words.
thank u