So all i have to do is clean it up but cant find the source to the actual error. Heres my code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

bool isVowel(char ch);
string rotate(string pStr);
string pigLatinString(string pStr);


int main()
{
ifstream infile;
string newstr;
int length;
char achar;
string str; 
string str1;
string str2;
string str3;
string pStr;
char ch;

infile.open("C:\\201\\PigLatin.txt");

if(infile.is_open() == false)
{
    std::cerr << "Error, unable to open file." << std::endl;
    return 1;
}

std::string word;
while(infile.fail() == false)
{
    infile >> word;
    std::cout << pigLatinString(word) << ' ';
}

cout << endl;
cout << "Pig Latin String Is: " << endl;
cout << endl;
while (!infile.eof())
{
infile.get(achar);

if (achar != ' ') 
{
str = str + achar;

}	
else 
{
cout << pigLatinString(str)<< " ";
str = "";
}
}
if (achar == '!')
{
length = static_cast<unsigned int>(str.length());
newstr = str.substr(0,(length-2));
cout << " " << pigLatinString(newstr) << "!";
}
if (achar == '?')
{
length = static_cast<unsigned int>(str.length());
newstr = str.substr(0,(length-2));
cout << " " << pigLatinString(newstr) << "?";
}
if (achar == '.')
{
length = static_cast<unsigned int>(str.length());
newstr = str.substr(0,(length-2));
cout << " " << pigLatinString(newstr) << ".";
}

cout << endl;
str = "";
cout << endl;
isVowel(ch);
rotate (pStr);
pigLatinString(pStr);
infile.close();


return 0;
}
bool isVowel(char ch){
switch (ch)
{
case 'A': case 'E':
case 'I': case 'O':
case 'U': case 'Y':
case 'a': case 'e':
case 'i': case 'o':
case 'u': case 'y': return true;
default: return false;
}
}

string rotate(string pStr)
{
string::size_type len = pStr.length();

string rStr;

rStr = pStr.substr(1, len - 1) + pStr[0];

return rStr;
}

string pigLatinString(string pStr)
{
string::size_type len;

bool foundVowel;

string::size_type counter;

if (isVowel(pStr[0]))
pStr = pStr + "-way";
else
{
pStr = pStr + '-';
pStr = rotate(pStr);
len = pStr.length();
foundVowel = false;

for (counter = 1; counter < len - 1; counter++)
if (isVowel(pStr[0]))
{
foundVowel = true;
break;
}
else 
pStr = rotate(pStr);

if (!foundVowel)
pStr = pStr.substr(1,len) + "-way";
else
pStr = pStr + "ay";
}
return pStr;
}

here is what is in the file that it reads from:

hello there my friend.

Here is how it prints out into the command prompt:


ello-hay ere-thay y-may iend.-fray iend.-fray
Pig Latin String Is:


And finally here is the error that i receive(Click the link to see the error):

http://tinypic.com/r/e8wpdv/7

Recommended Answers

All 12 Replies

I believe you are having this problem because the code is trying to go through the file two times. Line 33 starts a loop that goes through the input file all the way to the end of the file. Since you are now at the end of the file, line 42 (another while loop) returns false, so the loop is never executed. Then on line 46, you test using achar which never was initialized. (achar only gets initialized in the loop starting on line 42.)

so which loop should i be getting rid of?

You need to look at what you are trying to accomplish as you go through your code. What happens in the first loop? What is the intention of the second loop and what are all the tests for after the second loop?

You need to look at what you are trying to accomplish as you go through your code.

And it would really help you do this if you format your code properly.

this is the wa my college professors taught us im sorry its just the way im used to it.

>> this is the wa my college professors taught us im sorry its just the way im used to it.

You might want to give the formatting a try anyway. I predict programming will become much easier since a lot errors will stand out in a way that don't stand out without the formatting. Most people won't even look at someone's code if it isn't formatted since it makes debugging so much more difficult.

i agree i've been looking at it and i'll try and figure it out, but would you guys mind helping me this has been stumping me the past 2 days.

I think djarn may have spotted it earlier. Re-read djarn's post. If achar gets initialized in the while loop from lines 42 - 56, if you never go through that while loop, it will hit line 57 with achar uninitialized.

Stick a cout statement right before line 44. Make sure line 44 actually gets executed. If not, there's your problem.

As mentioned, you have two while loops. Go back and think about what you are trying to accomplish with each of them. If you expect to read the file entirely twice, you need to reset the input stream and perhaps close and re-open the file or get the pointer back to the beginning of the file.

i agree i've been looking at it and i'll try and figure it out, but would you guys mind helping me this has been stumping me the past 2 days.

Yes, if you will format the code. If it's true that this is the format your instructor is teaching, please change instructors. He is not a programmer. Sorry.

Look at the combination of your output so far and see how your code produces it... You are getting the Pig Latin output. This is from the loop at lines 33 to 37. Then you get the output of "Pig Latin String Is:" This is from your line 40. Line 42 then does a test that is false, you are at the end of the file (you got there in your loop at lines 33 to 37) and so it skips the loop here and goes directly to line 57 where achar is tested. But achar is not initialized, so this gives the error you are seeing.

so i should be initializing achar? and removing line 42, and editing which line?

Since I don't really know what you want to output, I can only guess. Let's say that your current output is fine, and you're just wanting to end cleanly.

Move your "announcement" output from lines 40 and 41 to before the first loop at line 33. Then lines 42 - 81 can be commented out.

If you want some of the functionality that those lines provide, you will have to add them to your pigLatinString method. But currently, they are doing nothing for the output you are currently getting and because the file has already been read, you are getting that error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.