lundi 23 mars 2015

C++ Pig latin code, just deletes file info


So I am trying to make a pig latin program, that reads from a specified file, and then turns it into pig latin. But for some reason its only clearing the original file, and I'm not sure what I'm doing wrong. Here is my code:



int main()
{
ifstream in;
char filename[200];
cout<< "Enter name of input file: ";
cin.getline(filename,200);
in.open(filename);
string out;
if(!in.is_open())
{
cout<<"ERROR failed to open input file " << filename << " BYE\n";
//this.exit();
}
char word[200];
bool result;
while(!in.eof())
{
in>>word;
result=check(word);
if(result==true)
vowel(word);
else
consonant(word);
out += word;
out += " ";

}
cout << out;
}

bool check(char word[200])
{
if(word[0]=='a'|| word[0]=='e'|| word[0]=='i'|| word[0]=='o'|| word[0]=='u'|| word[0]=='A'|| word[0]=='E'|| word[0]=='I'|| word[0]=='O'|| word[0]=='U')
return true;
else
return false;
}

void vowel (char word[])
{
int last(0);
last=strlen(word);
word[last]='w';
word[last+1]='a';
word[last+2]='y';
word[last+3]='\0';

}

void consonant(char word[])
{
int last;
char temp='\0';
last=strlen(word);
word[0]=temp;
for(int i=1;i<last-1;i++)
{
char tem=word[i];
word[i]=word[i+1];
word[i-1]=tem;
}
word[last-1]=temp;
word[last]='a';
word[last+1]='y';
word[last+2]='\0';
}


I included the following:




  • iostream




  • fstream




  • cstring




I apologize for all the code, but I really have no idea where my code is wrong, so I included everything.




Aucun commentaire:

Enregistrer un commentaire