1 Replies - 168 Views - Last Post: 08 February 2018 - 09:27 AM Rate Topic: -----

#1 StudioUAC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-September 16

I'm not reading the file, I'm instead reading the name of the

Posted 08 February 2018 - 09:06 AM

I'm having a terrible bug in my program it's not opening and reading anything from the file I give it. instead it's read the name of the file. what did I do wrong?

the objective of this program is to read in a list of sentences and determine what kind of palindromes they are.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

string getFileName();
string processFile(string filename);
void PrintBackwords(string phrase);
string ReverseString(string phrase);
void StringCompare(string phrase, string out, string filename);
string RemovePunct(string phrase);
string RemoveSpace(string phrase);





int main()
{
 string out, filename;
    
 
 string phrase = processFile(filename);
 StringCompare(phrase, out, filename);
 
    
return 0;
}

// Function to get the filename
string getFileName()
{
    string filename;
    
    cout << "Enter the file name=> ";
    cin >> filename;
    cin.ignore(256, '\n');

    cout << endl;
    
    
 
  cout << "CHECK NUMBER 1!!!" << endl << endl;
   //I'M STUCK RIGHT HERE!!!!!
    return filename;
}


// Function to open the file
string processFile(string filename)
{
    
 ifstream infile;
    
 filename = getFileName();
 infile.open(filename.c_str());
 
 
  while (!infile)
    {
        cout << "Cannot open the file" << endl;
        filename= getFileName();
        infile.open(filename.c_str());
    }
     
     
     
  cout << "CHECK NUMBER 2!!!" << endl << endl;    
    
  return filename;  
}


void PrintBackwords(string phrase)
{
    int i;

    for(i = phrase.length()-1; i>=0 ;i--) {
       cout <<  phrase[i];
    }

    return;
}

string ReverseString(string phrase) 
{
    string reverse;
    int i;

    for(i=phrase.length()-1; i>=0 ;i--) {
        reverse = reverse + phrase[i];
	
    }
 cout << "CHECK NUMBER 3!!!" << endl << endl;
    return reverse;
}


void StringCompare(string phrase, string out, string filename)
{    
    int i;
    ifstream infile;

    
    out = ReverseString(phrase);
    cout << "CHECK NUMBER 4!!!" << endl << endl;
    
    getline(infile, phrase, '\n');
    
    string a, b;
    
    while(!infile.eof())
    {
        for (i = 0; i < phrase.size(); i++)
        {
    
    if (phrase[i] == out[i])
    {
        cout << "'' " << phrase << " '' is a perfect palindrome." << endl;
    }
        else //remove the punctuation and check again
        RemovePunct(phrase);
    
    
        if (phrase[i] == out[i])
        {
            cout << "'' " << phrase << "'' is a natural palindrome." << endl; 
        }
        
        else //remove the spaces
            RemoveSpace(phrase);

        
            if (phrase[i] == out[i])
         {
            cout << "'' " << phrase << "'' is a palindrome." << endl; 
         }
        else 
        {
            cout << "'' " << phrase << "'' is a non-palindrome." << endl; 
        }

        } 
        
    }
    
    
      cout << "CHECK NUMBER 5!!!" << endl << endl;
    
    infile.close();
    
    
            return;
            
            
            
}


string RemovePunct(string phrase)
{
   string returnValue;
   size_t i;

   for(i=0; i< phrase.size(); i++) {
       if(not ispunct(phrase[i])) {
           returnValue = returnValue + phrase[i];
       }
   }


   return returnValue;
}


string RemoveSpace(string phrase)
{
   string returnValue;
   size_t i;

   for(i=0; i< phrase.size(); i++) {
       if(not isspace(phrase[i])) {
           returnValue = returnValue + phrase[i];
       }
   }


   return returnValue;
}





this is the file i'm using to debug with called input.txt

racecar

god saw I was dog

I did, did I?

Able was I ere I saw Elba.

A man, A plan, A canal: Panama!

Madam, I'm Adam.

Hello World!

spam




Is This A Good Question/Topic? 0
  • +

Replies To: I'm not reading the file, I'm instead reading the name of the

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2603
  • View blog
  • Posts: 4,157
  • Joined: 21-June 11

Re: I'm not reading the file, I'm instead reading the name of the

Posted 08 February 2018 - 09:27 AM

// Function to open the file
string processFile(string filename)
{
    
 ifstream infile;
    
 filename = getFileName();
 infile.open(filename.c_str());
 
 // ...
   
  return filename;  
}



You open the file, but then you never read from it or do anything else with it. Instead you just return the file name (and implicitly close the file when infile goes out of scope).

void StringCompare(string phrase, string out, string filename)
{    
    int i;
    ifstream infile;

    // ...
    
    getline(infile, phrase, '\n');
    
    string a, b;
    
    while(!infile.eof())
    {
        // ...
    }
    cout << "CHECK NUMBER 5!!!" << endl << endl;
    infile.close();
    return;
}



Here you create an ifstream, but never open a file with it. Then you try to read from it while it isn't yet opened.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1