• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

String problems in sentence manipulation program

Hi guys. As I am winding down my program I am having a few last issues. The purpose of the program is to manipulate a sentence with insertion and removal commands, by finding a word before and after where the change is to occur.

Basically, for my Insert > After  function, The change is occurring one position too soon. In both my remove> before and remove > after commands, no change is occurring at all. Any finger toward the right direction would be highly appreciated!
#include <iostream>
#include <string>
#include <fstream>
                         
using namespace std;
                         
                                
int main()
{
        string userFileInput,line, userFileOutput ,initialSentence;
        size_t pos;
        char command;
        ifstream sentenceInformation;
        ofstream fileOut;
        int cS2Length;
        string  commandSentence, commandSentence2;
        cout << "Please enter the name of the file you wish to use "
             << "including the appropriate extension. ( ie .txt)"
             << endl;
        getline(cin,userFileInput);
        sentenceInformation.open(userFileInput.c_str());
        if(!sentenceInformation)
        {
                cout << "Unable to open file!" << endl;
                exit(1); 
        }
        cout << "What would you like to name your outfile? (also include extension)" << endl;
        getline(cin,userFileOutput);
        fileOut.open(userFileOutput.c_str());
        getline(sentenceInformation, initialSentence);
        cout << initialSentence << endl;
        fileOut << initialSentence;
        fileOut << "\n";
        while(! sentenceInformation.eof())
        {
                command = sentenceInformation.get();
                getline(sentenceInformation, commandSentence);
                cout << command << endl;
                cout << commandSentence << endl;
                if(command == 'I')
                {
                        sentenceInformation.get(command);
                        getline(sentenceInformation, commandSentence2);
                        cout << command << endl;
                        cout << commandSentence2 << endl;
                        if(command == 'B')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if (pos != string::npos)
                                 {
                                 {
                                    initialSentence.insert(pos, commandSentence);
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                        if(command == 'A')
                        {
                                pos = initialSentence.find_last_of(commandSentence2);
                                if (pos != string::npos)
                                {
                                        initialSentence.insert(pos, commandSentence);
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                }
                if(command == 'R')
                {
                        sentenceInformation.get(command);
                        getline(sentenceInformation, commandSentence2);
                        cout << command << endl;
                        cout << commandSentence2 << endl;
                        if(command == 'B')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if (pos!= string:: npos)
                                {
                                        initialSentence.erase(pos, commandSentence.length());
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                        if(command == 'A')
                        {
                                pos = initialSentence.find_last_of(commandSentence2);
                                if ( pos!= string::npos)
                                {
                                        initialSentence.erase(pos, commandSentence.length());
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                }
                        
      }

Open in new window

0
pacman32689
Asked:
pacman32689
  • 30
  • 13
  • 8
3 Solutions
 
jkrCommented:
That does not compile in the 1st place (one '{' too much), try
#include <string>
#include <fstream>
#include <iostream>
                         
using namespace std;
                         
                                
int main()
{
        string userFileInput,line, userFileOutput ,initialSentence;
        size_t pos;
        char command;
        ifstream sentenceInformation;
        ofstream fileOut;
        int cS2Length;
        string  commandSentence, commandSentence2;
        cout << "Please enter the name of the file you wish to use "
             << "including the appropriate extension. ( ie .txt)"
             << endl;
        getline(cin,userFileInput);
        sentenceInformation.open(userFileInput.c_str());
        if(!sentenceInformation)
        {
                cout << "Unable to open file!" << endl;
                exit(1); 
        }
        cout << "What would you like to name your outfile? (also include extension)" << endl;
        getline(cin,userFileOutput);
        fileOut.open(userFileOutput.c_str());
        getline(sentenceInformation, initialSentence);
        cout << initialSentence << endl;
        fileOut << initialSentence;
        fileOut << "\n";
        while(! sentenceInformation.eof())
        {
                command = sentenceInformation.get();
                getline(sentenceInformation, commandSentence);
                cout << command << endl;
                cout << commandSentence << endl;
                if(command == 'I')
                {
                        sentenceInformation.get(command);
                        getline(sentenceInformation, commandSentence2);
                        cout << command << endl;
                        cout << commandSentence2 << endl;
                        if(command == 'B')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if (pos != string::npos)
                                 
                                 {
                                    initialSentence.insert(pos, commandSentence);
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                        if(command == 'A')
                        {
                                pos = initialSentence.find_last_of(commandSentence2);
                                if (pos != string::npos)
                                {
                                        initialSentence.insert(pos, commandSentence);
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                }
                if(command == 'R')
                {
                        sentenceInformation.get(command);
                        getline(sentenceInformation, commandSentence2);
                        cout << command << endl;
                        cout << commandSentence2 << endl;
                        if(command == 'B')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if (pos!= string:: npos)
                                {
                                        initialSentence.erase(pos, commandSentence.length());
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                        if(command == 'A')
                        {
                                pos = initialSentence.find_last_of(commandSentence2);
                                if ( pos!= string::npos)
                                {
                                        initialSentence.erase(pos, commandSentence.length());
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                }
                        
      }
}

Open in new window

0
 
pacman32689Author Commented:
It does compile, copying error, my apologies.
0
 
pacman32689Author Commented:
However, not compiling wasn't the issue :)
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
jkrCommented:
Yes, that I know ;o) Could you elaborate a bit on the format of your input files? The 1st issue can be fixed using

initialSentence.insert(pos + 1, commandSentence);

if I interpret 'I' correctly as 'Insert'.
0
 
pacman32689Author Commented:
Sure! Here is a quick example

User Input file:

I hate c++
I debugging
A hate
I but love
A hate
R hate but
A I


Resulting output file
I hate c++
I hate debugging c++
I hate but love debugging c++
I love debugging c++
0
 
pacman32689Author Commented:
However, here is the problem. I have now attached the input file.

CSE2341 is a wonderful class.
CSE2341 is a great and wonderful class.
CSE2341 is a great and wonderful class I intend to make a(an). <-------- initial problem, should be CSE2341 is a great and wonderful class. I intend to make a(an)
input.txt
0
 
jkrCommented:
Um, what output do you expect?
0
 
pacman32689Author Commented:
CSE2341 is a wonderful class.
CSE2341 is a great and wonderful class.
CSE2341 is a great and wonderful class. I intend to make a(an)
CSE2341 is a great and wonderful class. I intend to make a(an) A.
CSE2341 is a great and wonderful class. I will make a(an) A.
0
 
pacman32689Author Commented:
I will keep reading online. I assumed since pos stores an integer(?) I could just add the length of the word that I am pointing to, but apparently I was wrong. :)
0
 
jkrCommented:
The 'pos + 1' missing increment is all over that, try


#include <string>
#include <fstream>
#include <iostream>
                         
using namespace std;
                         
                                
int main()
{
        string userFileInput,line, userFileOutput ,initialSentence;
        size_t pos;
        char command;
        ifstream sentenceInformation;
        ofstream fileOut;
        int cS2Length;
        string  commandSentence, commandSentence2;
        cout << "Please enter the name of the file you wish to use "
             << "including the appropriate extension. ( ie .txt)"
             << endl;
        getline(cin,userFileInput);
        sentenceInformation.open(userFileInput.c_str());
        if(!sentenceInformation)
        {
                cout << "Unable to open file!" << endl;
                exit(1); 
        }
        cout << "What would you like to name your outfile? (also include extension)" << endl;
        getline(cin,userFileOutput);
        fileOut.open(userFileOutput.c_str());
        getline(sentenceInformation, initialSentence);
        cout << initialSentence << endl;
        fileOut << initialSentence;
        fileOut << "\n";
        while(! sentenceInformation.eof())
        {
                command = sentenceInformation.get();
                getline(sentenceInformation, commandSentence);
                cout << command << endl;
                cout << commandSentence << endl;
                if(command == 'I')
                {
                        sentenceInformation.get(command);
                        getline(sentenceInformation, commandSentence2);
                        cout << command << endl;
                        cout << commandSentence2 << endl;
                        if(command == 'B')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if (pos != string::npos)
                                 
                                 {
                                    initialSentence.insert(pos + 1, commandSentence);
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                        if(command == 'A')
                        {
                                pos = initialSentence.find_last_of(commandSentence2);
                                if (pos != string::npos)
                                {
                                        initialSentence.insert(pos + 1, commandSentence);
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                }
                if(command == 'R')
                {
                        sentenceInformation.get(command);
                        getline(sentenceInformation, commandSentence2);
                        cout << command << endl;
                        cout << commandSentence2 << endl;
                        if(command == 'B')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if (pos!= string:: npos)
                                {
                                        initialSentence.erase(pos + 1, commandSentence.length());
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                        if(command == 'A')
                        {
                                pos = initialSentence.find_last_of(commandSentence2);
                                if ( pos!= string::npos)
                                {
                                        initialSentence.erase(pos + 1, commandSentence.length());
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                }
                        
      }
}

Open in new window

0
 
pacman32689Author Commented:
Well darn. 1+ is only needed for the 'a' statement because a " " is already added I believe. Now my remove statements still wont function. I looked up the string library and found the erase() function. Seems like the most appropriate. However, looking at my input, there is no change being made to the sentence when the remove function is called. Maybe the entire sentence is being deleted because here is my output.

CSE2341 is a wonderful class.
CSE2341 is a great and wonderful class.
CSE2341 is a great and wonderful class. I intend to make a(an)
CSE2341 is a great and wonderful class. I intend to make a(an) A.




0
 
jkrCommented:
Um,

                         pos = initialSentence.find(commandSentence2);
                                if (pos!= string:: npos)
                                {
                                        initialSentence.erase(pos + 1, commandSentence.length());
                                }

will erase the amount of characters inputted by the user (as 5 in "ABCDE"), is that what you want to do?
0
 
pacman32689Author Commented:
Remove should remove the phrase that the command gives.

ie for the next sentence it should be "intend to" so those 9 characters.
0
 
jkrCommented:
More examples about the input files you are using? The 'CSE2341' does not really make sense in that contect, at least that is to me... (call me dumb ;o)
0
 
pacman32689Author Commented:
It is the name of my class. ^_-
0
 
pacman32689Author Commented:
The final product should be.

CSE2341 is a great and wonderful class. I will make a(an) A. ... based on the input file I uploaded. Both insert functions are working, but the removal functions are not working. The removal function should find the word in the sentence string, and remove it, then re-concatenating the sentence back together.
0
 
jkrCommented:
Aaah, I see  - well, ' find_last_of()' wll get you incorrect results here, since it "Searches through a string for the last character that matches *any* element of a specified string.". You'd rather use a  plain 'find()' with the increment here.

0
 
jkrCommented:
IOW:
#include <string>
#include <fstream>
#include <iostream>
                         
using namespace std;
                         
                                
int main()
{
        string userFileInput,line, userFileOutput ,initialSentence;
        size_t pos;
        char command;
        ifstream sentenceInformation;
        ofstream fileOut;
        int cS2Length;
        string  commandSentence, commandSentence2;
        cout << "Please enter the name of the file you wish to use "
             << "including the appropriate extension. ( ie .txt)"
             << endl;
        getline(cin,userFileInput);
        sentenceInformation.open(userFileInput.c_str());
        if(!sentenceInformation)
        {
                cout << "Unable to open file!" << endl;
                exit(1); 
        }
        cout << "What would you like to name your outfile? (also include extension)" << endl;
        getline(cin,userFileOutput);
        fileOut.open(userFileOutput.c_str());
        getline(sentenceInformation, initialSentence);
        cout << initialSentence << endl;
        fileOut << initialSentence;
        fileOut << "\n";
        while(! sentenceInformation.eof())
        {
                command = sentenceInformation.get();
                getline(sentenceInformation, commandSentence);
                cout << command << endl;
                cout << commandSentence << endl;
                if(command == 'I')
                {
                        sentenceInformation.get(command);
                        getline(sentenceInformation, commandSentence2);
                        cout << command << endl;
                        cout << commandSentence2 << endl;
                        if(command == 'B')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if (pos != string::npos)
                                 
                                 {
                                    initialSentence.insert(pos + 1, commandSentence);
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                        if(command == 'A')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if (pos != string::npos)
                                {
                                        initialSentence.insert(pos + 1, commandSentence);
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                }
                if(command == 'R')
                {
                        sentenceInformation.get(command);
                        getline(sentenceInformation, commandSentence2);
                        cout << command << endl;
                        cout << commandSentence2 << endl;
                        if(command == 'B')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if (pos!= string:: npos)
                                {
                                        initialSentence.erase(pos + 1, commandSentence.length());
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                        if(command == 'A')
                        {
                                pos = initialSentence.find_last_of(commandSentence2);
                                if ( pos!= string::npos)
                                {
                                        initialSentence.erase(pos + 1, commandSentence.length());
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                }
                        
      }
}

Open in new window

0
 
pacman32689Author Commented:
OOOOOOOHHHHHHHHHHH!!!!!!!! Thanks! I will make the appropriate suggestions

myReadingSkills = myReadingSkills - 1;
0
 
jkrCommented:
Argh, forgive me:
#include <string>
#include <fstream>
#include <iostream>
                         
using namespace std;
                         
                                
int main()
{
        string userFileInput,line, userFileOutput ,initialSentence;
        size_t pos;
        char command;
        ifstream sentenceInformation;
        ofstream fileOut;
        int cS2Length;
        string  commandSentence, commandSentence2;
        cout << "Please enter the name of the file you wish to use "
             << "including the appropriate extension. ( ie .txt)"
             << endl;
        getline(cin,userFileInput);
        sentenceInformation.open(userFileInput.c_str());
        if(!sentenceInformation)
        {
                cout << "Unable to open file!" << endl;
                exit(1); 
        }
        cout << "What would you like to name your outfile? (also include extension)" << endl;
        getline(cin,userFileOutput);
        fileOut.open(userFileOutput.c_str());
        getline(sentenceInformation, initialSentence);
        cout << initialSentence << endl;
        fileOut << initialSentence;
        fileOut << "\n";
        while(! sentenceInformation.eof())
        {
                command = sentenceInformation.get();
                getline(sentenceInformation, commandSentence);
                cout << command << endl;
                cout << commandSentence << endl;
                if(command == 'I')
                {
                        sentenceInformation.get(command);
                        getline(sentenceInformation, commandSentence2);
                        cout << command << endl;
                        cout << commandSentence2 << endl;
                        if(command == 'B')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if (pos != string::npos)
                                 
                                 {
                                    initialSentence.insert(pos + 1, commandSentence);
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                        if(command == 'A')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if (pos != string::npos)
                                {
                                        initialSentence.insert(pos + 1, commandSentence);
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                }
                if(command == 'R')
                {
                        sentenceInformation.get(command);
                        getline(sentenceInformation, commandSentence2);
                        cout << command << endl;
                        cout << commandSentence2 << endl;
                        if(command == 'B')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if (pos!= string:: npos)
                                {
                                        initialSentence.erase(pos + 1, commandSentence.length());
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                        if(command == 'A')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if ( pos!= string::npos)
                                {
                                        initialSentence.erase(pos + 1, commandSentence.length());
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                }
                        
      }
}

Open in new window

0
 
pacman32689Author Commented:
However, how will find what value to concatenate by? It tells me when i use statement

 initialSentence.erase(pos + strlen(commandSentence2), commandSentence);

lab2.cpp:88: error: cannot convert 'std::string' to 'const char*' for argument '1' to 'size_t strlen(const char*)'
0
 
pacman32689Author Commented:
Beat me to it, thats what I thought
0
 
pacman32689Author Commented:
Ignore my previous post.
0
 
pacman32689Author Commented:
No dice...
#include <iostream>
#include <string>
#include <fstream>
 
using namespace std;
 
 
int main()
{
        string userFileInput,line, userFileOutput ,initialSentence;
        size_t pos;
        char command;
        ifstream sentenceInformation;
        ofstream fileOut;
        int cS2Length;
        string  commandSentence, commandSentence2;
        cout << "Please enter the name of the file you wish to use "
             << "including the appropriate extension. ( ie .txt)"
             << endl;
        getline(cin,userFileInput);
        sentenceInformation.open(userFileInput.c_str());
        if(!sentenceInformation)
        {
                cout << "Unable to open file!" << endl;
                exit(1);
        }
        cout << "What would you like to name your outfile? (also include extension)" << endl;
        getline(cin,userFileOutput);
        fileOut.open(userFileOutput.c_str());
        getline(sentenceInformation, initialSentence);
        cout << initialSentence << endl;
        fileOut << initialSentence;
        fileOut << "\n";
        while(! sentenceInformation.eof())
        {
                command = sentenceInformation.get();
                getline(sentenceInformation, commandSentence);
                cout << command << endl;
                cout << commandSentence << endl;
                if(command == 'I')
                {
                        sentenceInformation.get(command);
                        getline(sentenceInformation, commandSentence2);
                        cout << command << endl;
                        cout << commandSentence2 << endl;
                        if(command == 'B')
                        {
                               pos = initialSentence.find(commandSentence2);
                                if (pos != string::npos)
                                {
                                    initialSentence.insert(pos , commandSentence);
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                        }
                        if(command == 'A')   
                        {
                                pos = initialSentence.find_last_of(commandSentence2);
                                if (pos != string::npos)
                                {
                                        initialSentence.insert(pos + 1, commandSentence);
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;   
                                fileOut << "\n";
                        }
                }
                if(command == 'R')
                {
                        sentenceInformation.get(command);
                        getline(sentenceInformation, commandSentence2);
                        cout << command << endl;
                        cout << commandSentence2 << endl;
                        if(command == 'B')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if (pos!= string:: npos)
                                {
                                        initialSentence.erase(pos + 1, commandSentence.length());
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                        if(command == 'A')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if ( pos!= string::npos)
                                {
                                        initialSentence.erase(pos + 1, commandSentence.length());
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                }
                        
      }
     fileOut.close();
     sentenceInformation.close();
                        
}

Open in new window

0
 
jkrCommented:
What do you get as the output? And, just kill me, yet

CSE2341 is a wonderful class.
CSE2341 is a great and wonderful class.
CSE2341 is a great and wonderful class. I intend to make a(an)
CSE2341 is a great and wonderful class. I intend to make a(an) A.
CSE2341 is a great and wonderful class. I will make a(an) A.

contains none of your edit commands....
0
 
pacman32689Author Commented:
The input file is as follows

CSE2341 is a wonderful class.
I great and
B wonderful
I I intend to make a(an)
A class.
I A.
A a(an)
R intend to
B I
I will
B make


This information is parsed by the program.

Output file results in

CSE2341 is a wonderful class.
CSE2341 is a great and wonderful class.
CSE2341 is a great and wonderful class. I intend to make a(an)
CSE2341 is a great and wonderful class. I intend to make a(an) A.
CSE2341 is a great and wonderful class. I will make a(an) A.

output does not contain commands, rather, it is a log of changes.
0
 
pacman32689Author Commented:
Every time the loops finishes, the program writes to the file. Thus why the log is possible.
0
 
pacman32689Author Commented:
No worries, learning how to explain equates to better understanding of the subject matter.
0
 
pacman32689Author Commented:
That is, if you explain it right :) Any luck yet, I can try to explain it again, lol. >.<
0
 
pacman32689Author Commented:
Given up on me?
0
 
Infinity08Commented:
>> Output file results in

That looks fine to me, no ? What's the problem ?
0
 
Infinity08Commented:
I made some changes to the code ... I hope that's how you intended it ...

I also replaced this in the input.txt file :

        R intend to
        B I

to this :

        R intend to
        A I

since "intend to" comes after "I", unless I misunderstood the meaning of R and A (remove and after respectively).
#include <iostream>
#include <string>
#include <fstream>
                         
using namespace std;
                         
                                
int main()
{
        string userFileInput,line, userFileOutput ,initialSentence;
        size_t pos;
        char command;
        ifstream sentenceInformation;
        ofstream fileOut;
        //int cS2Length;
        string  commandSentence, commandSentence2;
        cout << "Please enter the name of the file you wish to use "
             << "including the appropriate extension. ( ie .txt)"
             << endl;
        getline(cin,userFileInput);
        sentenceInformation.open(userFileInput.c_str());
        if(!sentenceInformation)
        {
                cout << "Unable to open file!" << endl;
                exit(1); 
        }
        cout << "What would you like to name your outfile? (also include extension)" << endl;
        getline(cin,userFileOutput);
        fileOut.open(userFileOutput.c_str());
        getline(sentenceInformation, initialSentence);
        cout << initialSentence << endl;
        fileOut << initialSentence;
        fileOut << "\n";
        while(! sentenceInformation.eof())
        {
                command = sentenceInformation.get();
                getline(sentenceInformation, commandSentence);
                cout << command << endl;
                cout << commandSentence << endl;
                if(command == 'I')
                {
                        sentenceInformation.get(command);
                        getline(sentenceInformation, commandSentence2);
                        cout << command << endl;
                        cout << commandSentence2 << endl;
                        if(command == 'B')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if (pos != string::npos)
                                {
                                    initialSentence.insert(pos, commandSentence);
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                        if(command == 'A')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if (pos != string::npos)
                                {
                                        initialSentence.insert(pos + commandSentence2.length(), commandSentence);
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                }
                if(command == 'R')
                {
                        sentenceInformation.get(command);
                        getline(sentenceInformation, commandSentence2);
                        cout << command << endl;
                        cout << commandSentence2 << endl;
                        if(command == 'B')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if (pos!= string::npos)
                                {
                                        if (pos > commandSentence.length()) {
                                            pos -= commandSentence.length();
                                            if (pos == initialSentence.find(commandSentence, pos)) {
                                                initialSentence.erase(pos, commandSentence.length());
                                            }
                                        }
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                        if(command == 'A')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if ( pos!= string::npos)
                                {
                                        pos += commandSentence2.length();
                                        if (pos == initialSentence.find(commandSentence, pos)) {
                                            initialSentence.erase(pos, commandSentence.length());
                                        }
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                }
                        
      }
}
 

Open in new window

0
 
pacman32689Author Commented:
now getting

CSE2341 is a great and wonderful class. I intend to will make a(an) A.

Looks like it is ignoring removal command. Correct me if I am wrong, erase deletes a certain amount of positions in the string based on a starting position, correct?
0
 
Infinity08Commented:
>> Looks like it is ignoring removal command.

Did you modify the input.txt file like I suggested ?
0
 
Infinity08Commented:
Or maybe I misunderstood the meaning of R and A (remove and after respectively) ?
0
 
pacman32689Author Commented:
Here is what my input file looks like

CSE2341 is a wonderful class.
I great and
B wonderful
I I intend to make a(an)
A class.
I A.
A a(an)
R intend to
A I
I will
B make

Final result should be CSE2341 is a great and wonderful class. I intend to make a(an) A.

For some reason, the remove function isnt pulling out intend to, and I am stumped :)



0
 
jkrCommented:
>>Given up on me?

No, but time was running out in my time zone...

Hm, but if the output you excpect is

CSE2341 is a wonderful class.
CSE2341 is a great and wonderful class.
CSE2341 is a great and wonderful class. I intend to make a(an)
CSE2341 is a great and wonderful class. I intend to make a(an) A.
CSE2341 is a great and wonderful class. I will make a(an) A.

and you use the code from http://www.experts-exchange.com/codeSnippetPopup.jsp?csid=30787

C:\tmp\cc>sent2
Please enter the name of the file you wish to use including the appropriate exte
nsion. ( ie .txt)
test.txt
What would you like to name your outfile? (also include extension)
new.txt
CSE2341 is a wonderful class.
C
SE2341 is a great and wonderful class.
C
SE2341 is a great and wonderful class. I intend to make a(an)
C
SE2341 is a great and wonderful class. I intend to make a(an) A.


should be pretty much it...
0
 
Infinity08Commented:
The code from my post with id 20757757 should correctly remove the "intend to" ... Are you sure you tried that ?
0
 
pacman32689Author Commented:
here is my code and input file

CSE2341 is a wonderful class.
I
 great and
B
 wonderful
CSE2341 is a great and wonderful class.
I
 I intend to make a(an)
A
 class.
CSE2341 is a great and wonderful class. I intend to make a(an)
I
 A.
A
 a(an)
CSE2341 is a great and wonderful class. I intend to make a(an) A.
R
 intend to
A
 I
CSE2341 is a great and wonderful class. I intend to make a(an) A.
I
 will
B
 make
CSE2341 is a great and wonderful class. I intend to will make a(an) A.
?
 will


Input file:

CSE2341 is a wonderful class.
I great and
B wonderful
I I intend to make a(an)
A class.
I A.
A a(an)
R intend to
A I
I will
B make





#include <iostream>
#include <string>
#include <fstream>
 
using namespace std;
 
 
int main()
{
        string userFileInput,line, userFileOutput ,initialSentence;
        size_t pos;
        char command;
        ifstream sentenceInformation;
        ofstream fileOut;
        //int cS2Length;
        string  commandSentence, commandSentence2;
        cout << "Please enter the name of the file you wish to use "
             << "including the appropriate extension. ( ie .txt)"
             << endl;
        getline(cin,userFileInput);
        sentenceInformation.open(userFileInput.c_str());
        if(!sentenceInformation)
        {
                cout << "Unable to open file!" << endl;
                exit(1);
        }
        cout << "What would you like to name your outfile? (also include extension)" << endl;
        getline(cin,userFileOutput);
        fileOut.open(userFileOutput.c_str());
        getline(sentenceInformation, initialSentence);
        cout << initialSentence << endl;
        fileOut << initialSentence;
        fileOut << "\n";
        while(! sentenceInformation.eof())
        {
                command = sentenceInformation.get();
                getline(sentenceInformation, commandSentence);
                cout << command << endl;
                cout << commandSentence << endl;
                if(command == 'I')
                {
                        sentenceInformation.get(command);
                        getline(sentenceInformation, commandSentence2);
                        cout << command << endl;
                        cout << commandSentence2 << endl;
                        if(command == 'B')
{
                                pos = initialSentence.find(commandSentence2);
                                if (pos != string::npos)
                                {
                                    initialSentence.insert(pos, commandSentence);
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                        if(command == 'A')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if (pos != string::npos)
                                {
                                        initialSentence.insert(pos + commandSentence2.length(), commandSentence);
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                }
                if(command == 'R')
                {
                        sentenceInformation.get(command);
                        getline(sentenceInformation, commandSentence2);
                        cout << command << endl;
                        cout << commandSentence2 << endl;
                        if(command == 'B')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if (pos!= string::npos)
                                {
                                        if (pos > commandSentence.length()) {
                                            pos -= commandSentence.length();
                                            if (pos == initialSentence.find(commandSentence, pos)) {
                                                initialSentence.erase(pos, commandSentence.length());
                                            }
                                        }
                                }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                        if(command == 'A')
                        {
                                pos = initialSentence.find(commandSentence2);
                                if ( pos!= string::npos)
                                {
                                        pos += commandSentence2.length();
                                        if (pos == initialSentence.find(commandSentence, pos)) {
                                            initialSentence.erase(pos, commandSentence.length());
                                        }
          }
                                cout << initialSentence << endl;
                                fileOut << initialSentence;
                                fileOut << "\n";
                                fileOut.flush();
                        }
                }
                                        
      }
}

Open in new window

0
 
pacman32689Author Commented:
Resulting outfile:

CSE2341 is a wonderful class.
CSE2341 is a great and wonderful class.
CSE2341 is a great and wonderful class. I intend to make a(an)
CSE2341 is a great and wonderful class. I intend to make a(an) A.
CSE2341 is a great and wonderful class. I intend to make a(an) A.
CSE2341 is a great and wonderful class. I intend to will make a(an) A.


Last line should result in : CSE2341 is a great and wonderful class. I intend to make a(an) A.
0
 
jkrCommented:
>>initialSentence.insert(pos, commandSentence);

Any specific reason to remove the '+ 1' from pos?
0
 
pacman32689Author Commented:
WOW I AM SORRY. Last line should read: CSE2341 is a great and wonderful class. I will make a(an) A.
0
 
pacman32689Author Commented:
>>initialSentence.insert(pos, commandSentence);

Any specific reason to remove the '+ 1' from pos?

Is that in before statement for Insert? I did so because I was getting a double space due to what I thought was an implied ' ' before each statement.
0
 
pacman32689Author Commented:
So I am assuming that my program isn't finding intend to in initialsentence.
0
 
pacman32689Author Commented:
This code segment must be where the problem is. for some reason, the 2nd if statement is getting a false instead of a true.
if (pos!= string::npos)
                                {
                                        if (pos > commandSentence.length()) {
                                            pos -= commandSentence.length();
                                            if (pos == initialSentence.find(commandSentence, pos)) {
                                                initialSentence.erase(pos, commandSentence.length());
                                            }
                                        }
                                }       

Open in new window

0
 
Infinity08Commented:
It works fine for me, pacman32689 :


F:\TMP>init2
Please enter the name of the file you wish to use including the appropriate exte
nsion. ( ie .txt)
input.txt
What would you like to name your outfile? (also include extension)
output.txt
CSE2341 is a wonderful class.
I
 great and
B
 wonderful
CSE2341 is a great and wonderful class.
I
 I intend to make a(an)
A
 class.
CSE2341 is a great and wonderful class. I intend to make a(an)
I
 A.
A
 a(an)
CSE2341 is a great and wonderful class. I intend to make a(an) A.
R
 intend to
A
 I
CSE2341 is a great and wonderful class. I make a(an) A.
I
 will
B
 make
CSE2341 is a great and wonderful class. I will make a(an) A.

Open in new window

0
 
Infinity08Commented:
Are you sure that the input.txt file is correct (any extra characters that aren't supposed to be there ?) ?

Copy and paste this in input.txt :
CSE2341 is a wonderful class.
I great and
B wonderful
I I intend to make a(an)
A class.
I A.
A a(an)
R intend to
A I
I will
B make

Open in new window

0
 
jkrCommented:
>>if (pos == initialSentence.find(commandSentence, pos))

That can only be 'true' if 'commandSentece' is right at the start of 'initialSentence'.
0
 
pacman32689Author Commented:
My input file got corrupted, thanks for help!
0
 
Infinity08Commented:
>> That can only be 'true' if 'commandSentece' is right at the start of 'initialSentence'.

No, that will be true if commandSentence is found where it's supposed to be found - ie. right before commandSentence2. Which is the behavior we want (again : if I understood the meanings of R and B correctly).
0
 
pacman32689Author Commented:
Thanks guys, I think I am starting to understand strings a heck of a lot better now. :)
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 30
  • 13
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now