View Single Post
Old 10-29-2004, 08:15 PM   #13 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
Originally Posted by Androto
ifstream scrabble("/Users/andrewassaly/Desktop/scrabble_words.txt");
if (!scrabble.is_open())
{
cout << "Error, cannot find file" << endl;
wait_for_enter();
return 13;
}
The code above is wrong. That means, the cout<<"Error:.." is wrong. You can't know why the file didn't open correctly. Perhaps it's there but something else is wrong. So change your output to something more general.
Code:
    while (!scrabble.eof())
    {
 }
No.
Code:
while(infile>>myString) { }
Dot it right.

Code:
///////////////// Palindrome /////////////////////
void palindrome(string line)
{
    string reversed;
    reversed = reverse(line);
    verify(reversed);
    wait_for_enter();
}
The wait_for_enter() doesn't belong there. It belongs in verify(). But that's wrong too. I told you to let it return a bool only. Don't let it do two things. Let it only verify. Learn it the right way while you can.

Code:
///////////////// reverse///////////////////////
string reverse(string wordin)
{
    string wordr;
    int i, j;
    i = 0;
    j = wordin.size() -1; // -1 because an array starts counting at 0, not 1
    wordr = wordin;
    /* this while loop reverses the wordin
    it does this by taking the letter at i and making it equal to the letter
    at j*/  
    while (j >= 0)
    {
        wordr[i] = wordin[j];
        i++;
        j--;
    }
    return wordr;
You forgot a }.
Besides, don't use c-style comments, e.g. /* */ when coding C++.

Code:
/////////////////////verify ///////////////////////
void verify(string reversed, string wordin)
{
    if (reversed == wordin)
    {
        cout << wordin << " is a palindrome!!!!" << endl << endl;
    }
    else
    {
        cout << wordin << " is not a palindrome!!!" << endl << endl;        
    }
}
The cout doesnt belong there. Functions do one distinguished thing only!
Make it so:
bool verify(string reversed, string wordin)
If your functions do more things then expected then the effect of functions is gone. A function groups code for a single purpose task. Image all the functions in a bigger program do multiple things. What a mess is that going to be.

Quote:
///// i need this code for mac, other system paus does not work/////
void wait_for_enter()
{
cout << "press <enter> to continue...";
cin.clear();
string line;
getline( cin, line);
}
Mac or no mac, this is the way to go.
__________________
Valmont is offline   Reply With Quote