Hi, I'm sort of new to C++, and need to write a code that converts text from an input file to pig latin and outputs it to another output file. I've written the code, but I keep running into issues, and I was wondering if anyone could help me. I keep getting something about the stack of the variable "word", and I don't know what it means. If you could help me it'd be great!
/*
N Bonzani
Lab 4 Problem 1
2/5/13
Write a program that reads in a text file
and converts it to pig Latin. i.e. if the
first letter is a consonant, move it to the
end and add “ay” to the end, if it is a
vowel add “way” to the end. Here’s an
example: “end the war” becomes “endway
hetay arway”. Include a structure chart.
*/
#include <iostream>
#include <fstream>
#include <cstring>
usingnamespace std;
bool check(char word[200]);
void vowel (char word[]);
void consonant(char word[]);
int main()
{
ifstream in;
char filename[200];
cout<< "Enter name of input file: ";
cin.getline(filename,200);
in.open(filename);
ofstream out;
char outname[200];
cout<< "Enter name of output file: ";
cin.getline(outname, 200);
out.open(filename);
if(!in.is_open())
{
cout<<"ERROR failed to open input file " << filename << " BYE\n";
exit(-1);
}
if(!out.is_open())
{
cout<<"ERROR failed to open output file " << filename << " BYE\n";
exit(-1);
}
char word[200];
bool result;
while(!in.eof())
{
in>>word;
result=check(word);
if(result==true)
vowel(word);
else
consonant(word);
out<< word;
}
}
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')
returntrue;
elsereturnfalse;
}
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';
}
It is a school assignment but it looks like ours are different. (I don't have the same rules of pig latin that the other poster does, his are a bit more strict.) And I'm not getting compiler errors but rather a pop up that occurs after I input both the input and output file names. It gives me the option to retry, abort, or cancel.
It gives me the option to retry, abort, or cancel.
It also tells you why it's giving you those options.
1 2
in.open(filename);
out.open(filename);
When you open the same file for output as you do input, you truncate the file. When you read data from in, the file is empty and you don't actually read anything in. Tip: Looping on eof is rarely the right thing to do.