Hi...Plz i'm facing some problems on how to make my program work successfully.
i'm a beginner in the c++ and i have an assignement to make a text editor using the devc++..it requires certain functions and i've written this code so far but i can't handle except the choice number 2 and number 11..i've wriiten the functions i need in red colour..plz you can see the menu of functions i need in the source code and i need them to be solved using the fstream and strings...plz i'd really appreciate your concern:

#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;
int main()
{
    int choice;
    int i=1;
    string myFile ;
    ifstream infile;
    ofstream outfile;
    cout << "Welcome To My Text Editor" <<endl;
    cout << "Please Enter Your File's Name: ";
    
    cin >> myFile;
    outfile.open (myFile.c_str());
    cout << "File " << myFile << " Doesn't Exist.I Created It For You" << endl;
    outfile.close();
    
    while (i>0)
    {
              
          cout << "\n\n" << "1.  Add New Text To The End Of The File" << endl;
          cout << "2.  Display The Content Of The File" << endl;
          cout << "3.  Empty The File" << endl;
          cout << "4.  Encrypt The File Content" << endl;
          cout << "5.  Decrypt The File Content" << endl;
          cout << "6.  Merge Another File" << endl;
          cout << "7.  Count The Number Of Words In The File" << endl;
          cout << "8.  Search For A Word In The File" << endl;
          cout << "9.  Count The Number Of Times A Word Exists In The File" << endl;
          cout << "10. Turn The File Content To Upper Case" << endl;
          cout << "11. Exit" << endl;
    
          cout << "\nPlease Enter Your Choice From The Menu Above: " ;
          cin >> choice;
          cout << "\n" ;
          switch (choice)
          {
                 case 2: {
                          
                          infile.open (myFile.c_str(), ifstream::in);
                          while (infile.good())
                          cout << (char) infile.get();
                          infile.close();continue;
                         }
                 
                 
                         
                 
                 case 11: cout << "Thank You For Using My Text Editor" << endl; break;
                 default: cout << "Choice Not Available,Please Choose An Appropriate Choice"; continue;
                                        
                 
                 }
                     
                 system ("Pause");
                 return 0;
                 }
                 
  }

menu items 4 and 5 are probably the most difficult and I would leave them to last. But the others aren't all that hard.

Take #1 -- append text to the end of a file. The answer to that is all in how the output file is opened. Look at the list of open flags and read their descriptions. One of them, ate says that the text will always be written to the end of the file. That means the file would be opened like this: ofstream out("filename.txt", ios::ate); .
Now each time you write something to that stream it will be appended to the end of the file.

thanks for your help....but plz help me in the rest.....try to tell me what to write because i understand the concept of the string.lenght() and this stuff...but i can't write it to work properly with files....plz try to help me in the other red coloured lines..thnx

#3 - empty a file. Look at the file flags link I posted then pick the one that will truncate a file to 0 length when the file is opened.

#6 - merge a file. I suppose that means to append one file to the end of another. This is related to menu #1. So what you do is open one file for input and the other for append then read the first file and write it to the second.

#7: just open a file for input then read each line and count the number of lines read.

When you get all that done you may know how to do the others.

Would You Please as an example....with reference to my code....can u write wat u say for me to understand it more...cuz everytime i try to do this something goes wrong...this is actually my first program to handle strings and files...so i know i'm bothering you but plz try to teach me how to do this well....just all i'm asking for is for a written example which works in the program for the red coloured lines....thnx anyway

>>...cuz everytime i try to do this something goes wrong
Post the code you have tried.

>>so i know i'm bothering you
How do you know that??? I wouldn't have responded to your thread if I thought I was being "bothered" :) But I'm not allowed to write the code for you. You should be reading examples in your text book.

fine...i'll tell u something more interesting...i figured out a problem in my program that i can't fix...when i added the code to add new text...it allows me to add only one word..for example if i wrote (do it)the program goes in an endless loop....but if i wrote(do)...it works..how can i fix this ?..
and this is my code so far with only case 1...the one i'm having a problem with right now:

case 1:
{
    outfile.open (myFile.c_str(), ios::ate);
    cin >> text;
    outfile << text;
    outfile.close();continue;
}

line 6: Use break instead of continue at the end of case statements.

>> cin >> text;
That only allows you to enter one word. If you want to enter more than one word there then you have to use getline(). There are two different versions of getline(), one for using on std::string objects and the other for character arrays

char text[255];
cin.getline(text, sizeof(text));

or
std::string text;
getline(cin, text);

>>it allows me to add only one word..for example if i wrote (do it)the program goes in an endless >>loop....but if i wrote(do)...it works..how can i fix this ?..

You'll have to post the code you wrote.

this is what i did in case 1 but i didn't work also:

case 1: 
                          outfile.open (myFile.c_str(), ios::app);
                          char text[255];
                          cin.getline(text, sizeof(text));


                          cin >> text;
                          outfile << text;
                          outfile.close();continue;

i really tried to search also for the other cases but i can't figure any out....this is f.y.i a self studying assignement...and i can't figure anything out unless i know the format to write a code properly....i really can't figure out anything rather than i did...plz try to help even give me hints.

this is what i did in case 1 but i didn't work also:

case 1: 
                          outfile.open (myFile.c_str(), ios::app);
                          char text[255];
                          cin.getline(text, sizeof(text));


                          cin >> text;
                          outfile << text;
                          outfile.close();continue;

i really tried to search also for the other cases but i can't figure any out....this is f.y.i a self studying assignement...and i can't figure anything out unless i know the format to write a code properly....i really can't figure out anything rather than i did...plz try to help even give me hints.

Get rid of this line:

cin >> text;

and see if it works better.

#3 is really nothing more than two lines of code -- open the file and then close it. Re-read my previous comments about this on how to open the file to make this work.

i did but it returns to the menu again and doesn't let me enter any input

i did but it returns to the menu again and doesn't let me enter any input

You're going to have to post your updated code because we have no idea what you did.

It doesn't require any user imput. All you have to do is empty the file. But I would add another message that the file has been empied so that you can see that something happened.

case 1: 
                          outfile.open (myFile.c_str(), ios::app);
                          char text[255];
                          cin.getline(text, sizeof(text));


                          outfile << text;
                          outfile.close();continue;

this is it so far:

#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;
int main()
{
    int choice;
    int i=1;
    string myFile ;
    string text;

    ifstream infile;
    ofstream outfile;
    cout << "Welcome To My Text Editor" <<endl;
    cout << "Please Enter Your File's Name: ";
    
    cin >> myFile;
    infile.open(myFile.c_str());
if (!infile) {
        cout << "file "<< myFile.c_str() << " Couldn't Be Opened."<< endl;
        outfile.open (myFile.c_str());
        cout << "file " << myFile.c_str() << " Doesn't Exist.I Created It For You" << endl;
        outfile.close();
             }
             
else { cout << "good job" << endl;
       while (infile.good())
       cout << (char) infile.get();
     }
    
    while (i>0)
    {
              
          cout << "\n\n" << "1.  Add New Text To The End Of The File" << endl;
          cout << "2.  Display The Content Of The File" << endl;
          cout << "3.  Empty The File" << endl;
          cout << "4.  Encrypt The File Content" << endl;
          cout << "5.  Decrypt The File Content" << endl;
          cout << "6.  Merge Another File" << endl;
          cout << "7.  Count The Number Of Words In The File" << endl;
          cout << "8.  Search For A Word In The File" << endl;
          cout << "9.  Count The Number Of Times A Word Exists In The File" << endl;
          cout << "10. Turn The File Content To Upper Case" << endl;
          cout << "11. Exit" << endl;
    
          cout << "\nPlease Enter Your Choice From The Menu Above: " ;
          cin >> choice;
          cout << "\n" ;
          switch (choice)
          {
                 case 1: 
                          outfile.open (myFile.c_str(), ios::app);
                          char text[255];
                          cin.getline(text, sizeof(text));
                          outfile << text;
                          outfile.close();continue;
                         
                           
                 case 2:                           
                          infile.open(myFile.c_str());
                          while (infile.good())
                          cout << (char) infile.get();
                          infile.close();continue;
                         
                 
                 case 3:  
                          outfile.open (myFile.c_str(), ios::trunc);
                          
                          outfile.close();continue; 
                          
                 case 7:{

                       infile.open(myFile.c_str(), ios::in );

                       
                        string word;
                        unsigned long wordCount = 0;

                        while( !infile.eof() )
                       {
                         infile >> word;
                         if( word.length() > 0 )
                           {
                        wordCount++;
                        }
                   }

cout << "The file had " << wordCount << " word(s) in it." << endl; 
continue;
}
              
                 case 11: cout << "Thank You For Using My Text Editor" << endl; break;
                 default: cout << "Choice Not Available,Please Choose An Appropriate Choice"; continue;
                                        
                 
                 }
                     
                 system ("Pause");
                 return 0;
                 }
                 
  }

this is it so far:

#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;
int main()
{
    int choice;
    int i=1;
    string myFile ;
    string text;

    ifstream infile;
    ofstream outfile;
    cout << "Welcome To My Text Editor" <<endl;
    cout << "Please Enter Your File's Name: ";
    
    cin >> myFile;
    infile.open(myFile.c_str());
if (!infile) {
        cout << "file "<< myFile.c_str() << " Couldn't Be Opened."<< endl;
        outfile.open (myFile.c_str());
        cout << "file " << myFile.c_str() << " Doesn't Exist.I Created It For You" << endl;
        outfile.close();
             }
             
else { cout << "good job" << endl;
       while (infile.good())
       cout << (char) infile.get();
     }
    
    while (i>0)
    {
              
          cout << "\n\n" << "1.  Add New Text To The End Of The File" << endl;
          cout << "2.  Display The Content Of The File" << endl;
          cout << "3.  Empty The File" << endl;
          cout << "4.  Encrypt The File Content" << endl;
          cout << "5.  Decrypt The File Content" << endl;
          cout << "6.  Merge Another File" << endl;
          cout << "7.  Count The Number Of Words In The File" << endl;
          cout << "8.  Search For A Word In The File" << endl;
          cout << "9.  Count The Number Of Times A Word Exists In The File" << endl;
          cout << "10. Turn The File Content To Upper Case" << endl;
          cout << "11. Exit" << endl;
    
          cout << "\nPlease Enter Your Choice From The Menu Above: " ;
          cin >> choice;
          cout << "\n" ;
          switch (choice)
          {
                 case 1: 
                          outfile.open (myFile.c_str(), ios::app);
                          char text[255];
                          cin.getline(text, sizeof(text));
                          outfile << text;
                          outfile.close();continue;
                         
                           
                 case 2:                           
                          infile.open(myFile.c_str());
                          while (infile.good())
                          cout << (char) infile.get();
                          infile.close();continue;
                         
                 
                 case 3:  
                          outfile.open (myFile.c_str(), ios::trunc);
                          
                          outfile.close();continue; 
                          
                 case 7:{

                       infile.open(myFile.c_str(), ios::in );

                       
                        string word;
                        unsigned long wordCount = 0;

                        while( !infile.eof() )
                       {
                         infile >> word;
                         if( word.length() > 0 )
                           {
                        wordCount++;
                        }
                   }

cout << "The file had " << wordCount << " word(s) in it." << endl; 
continue;
}
              
                 case 11: cout << "Thank You For Using My Text Editor" << endl; break;
                 default: cout << "Choice Not Available,Please Choose An Appropriate Choice"; continue;
                                        
                 
                 }
                     
                 system ("Pause");
                 return 0;
                 }
                 
  }

Regarding case 3, if the file is ALREADY open, I don't think you can reopen it, so I don't think it'll truncate it. If it's NOT open already, that should work. To be on the safe side, try closing it, then opening it again with the trunc option, as you have, then closing it again.

it's working now...now i have a probem in case 7..it always says that no. of words is 0...how can i fix that in the same code above ?

The while statement isn't correct. infile won't read blank lines so you don't have to check for length of the word.

while( infile >> word )
{
   wordCount++;
}

case 11: That has a problem too: break exits the switch statement, not the while loop.

i did what u said...but still giving me 0 words...see case 7 in this code:

#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;
int main()
{
    int choice;
    int i=1;
    string myFile ;
    string text;

    ifstream infile;
    ofstream outfile;
    cout << "Welcome To My Text Editor" <<endl;
    cout << "Please Enter Your File's Name: ";
    
    cin >> myFile;
    infile.open(myFile.c_str());
if (!infile) {
        cout << "file "<< myFile.c_str() << " Couldn't Be Opened."<< endl;
        outfile.open (myFile.c_str());
        cout << "file " << myFile.c_str() << " Doesn't Exist.I Created It For You" << endl;
        outfile.close();
             }
             
else { cout << "good job" << endl;
       while (infile.good())
       cout << (char) infile.get();
     }
     infile.close();
    while (i>0)
    {
              
          cout << "\n\n" << "1.  Add New Text To The End Of The File" << endl;
          cout << "2.  Display The Content Of The File" << endl;
          cout << "3.  Empty The File" << endl;
          cout << "4.  Encrypt The File Content" << endl;
          cout << "5.  Decrypt The File Content" << endl;
          cout << "6.  Merge Another File" << endl;
          cout << "7.  Count The Number Of Words In The File" << endl;
          cout << "8.  Search For A Word In The File" << endl;
          cout << "9.  Count The Number Of Times A Word Exists In The File" << endl;
          cout << "10. Turn The File Content To Upper Case" << endl;
          cout << "11. Exit" << endl;
    
          cout << "\nPlease Enter Your Choice From The Menu Above: " ;
          cin >> choice;
          cout << "\n" ;
          switch (choice)
          {
                 case 1: 
                          outfile.open (myFile.c_str(), ios::app);
                          cin >> text;
                          outfile << text;
                          outfile.close();continue;
                         
                           
                 case 2:                           
                          infile.open(myFile.c_str());
                          while (infile.good())
                          cout << (char) infile.get();
                          infile.close();continue;
                         
                 
                 case 3:  
                          outfile.open (myFile.c_str(), ios::trunc);
                          cout << "File Cleared" << endl;
                          outfile.close();continue; 
                          
                 case 7:    
                      infile.open( myFile.c_str(), ios::in );

if( infile.is_open() )
{
string word;
unsigned long wordCount = 0 ;

while( infile >> word )
{



wordCount++;

}

cout << "The file had " << wordCount << " word(s) in it." << endl;
infile.close();continue; 
}
                 
                 case 11: cout << "Thank You For Using My Text Editor" << endl; break;
                 default: cout << "Choice Not Available,Please Choose An Appropriate Choice"; continue;
                                        
                 
                 }
                     
                 system ("Pause");
                 return 0;
                 }
                 
  }

Stick some debugging statements in here or, if you are using a debugger, stick some breakpoints in there at various places. You need to know where your program is going. Display the word that is being read in to make sure it is the right word. See if you are even executing that part of the code. Put some cout statements at different parts of the code to see if they are executing. The problem may not be where you think it is. You may spend a lot of time correcting code that is never even being executed. Also, check your brackets to make sure they are the way you want them. You need to narrow down the problem more before you can solve it.

case 7:    
    infile.open( myFile.c_str(), ios::in );

    if( infile.is_open() )
    {
        string word;
        unsigned long wordCount = 0 ;

        while( infile >> word )
        {
              wordCount++;
         }

        cout << "The file had " << wordCount << " word(s) in it." << endl;
        infile.close();continue; 
     }

Are you sure that the file actually contains any data?

A minor note, in your 'case 7' block, the 'continue;' is inside the if block which checks whether the infile is open, you should move 'continue;' outside of the if block.

i think i posted the whole source code and now i told you that i can't figure out what to do...i'm a beginner in this stuff...and i really don't understand it well...so if you please you can look at the code and try to figure out what's wrong cuz definately you're more experienced than me....then if you find the problem u can tell me about it to avoid repeating it...

You need to use an infile.clear() call, since you are working with same ifstream object throughout the program. See
http://www.cplusplus.com/reference/iostream/ios/clear.html
Try also to understand and learn how to use the eof(), fail(), good() functions.

case 7:
infile.open(myFile.c_str(), ios::in);
[B]infile.clear();[/B]

Note that, when you add words to the file, add a newline also, otherwise you'll have just one long word.

this line ( infile.clear(); ) really solved all my problems....thnx man...now i have to figure out how the other cases work...plz try to help me solve them

After running that program I think the problem is that the file is not where you think it is. After adding the clear(). Make sure that you either (1)include the full path to the file as well as the filename or (2) copy the file into the same directory where the program executes. If the path contains spaces -- such as "Program Files" -- then you will have to use getline() instead of the >> operator to get the filename.

[edit]^^^ glad you found the problem [/edit]

yeah i already checked this....now can we move to another major problem i'm facing till now.....in case 1...i can only add one word to the file....instead i want to add many words with spaces between them...here's my source file till now :

#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;
int main()
{
    int choice;
    int i=1;
    string myFile ;
    string text;

    ifstream infile;
    ofstream outfile;
    cout << "Welcome To My Text Editor" <<endl;
    cout << "Please Enter Your File's Name: ";
    
    cin >> myFile;
    infile.open(myFile.c_str());
if (!infile) {
        cout << "The File "<< myFile.c_str() << " Couldn't Be Opened."<< endl;
        outfile.open (myFile.c_str());
        cout << "The File" << myFile.c_str() << " Doesn't Exist.Instead,I Created It For You" << endl;
        outfile.close();
             }
             
else { cout << "File Successfully Opened" << endl;
       while (infile.good())
       cout << (char) infile.get();
     }
     infile.close();
    while (i>0)
    {
              
          cout << "\n\n" << "1.  Add New Text To The End Of The File" << endl;
          cout << "2.  Display The Content Of The File" << endl;
          cout << "3.  Empty The File" << endl;
          cout << "4.  Encrypt The File Content" << endl;
          cout << "5.  Decrypt The File Content" << endl;
          cout << "6.  Merge Another File" << endl;
          cout << "7.  Count The Number Of Words In The File" << endl;
          cout << "8.  Search For A Word In The File" << endl;
          cout << "9.  Count The Number Of Times A Word Exists In The File" << endl;
          cout << "10. Turn The File Content To Upper Case" << endl;
          cout << "11. Exit" << endl;
    
          cout << "\nPlease Enter Your Choice From The Menu Above: " ;
          cin >> choice;
          cout << "\n" ;
          switch (choice)
          {
                 case 1: 
                          outfile.open (myFile.c_str(), ios::app);
                          infile.clear();
                          
                          char text[255];
                          cin.getline(text, sizeof(text));
                          cin >> text;
                          outfile << text;
                          outfile.close();continue;
                         
                           
                 case 2:                           
                          infile.open(myFile.c_str());
                          infile.clear();
                          while (infile.good())
                          cout << (char) infile.get();
                          infile.close();continue;
                         
                 
                 case 3:  
                          outfile.open (myFile.c_str(), ios::trunc);
                          cout << "File Cleared" << endl;
                          outfile.close();continue; 
                          
                 case 7:    
                          infile.open( myFile.c_str(), ios::in );
 
                          infile.clear();
                          if( infile.is_open() )
                         {
                          string word;
                          unsigned long wordCount = 0 ;

                          while( infile >> word )
                         {
                          wordCount++;

                         }

                          cout << "The File Contains " << wordCount << " Word(s) In It." << endl;
                          infile.close(); 
                         }
                          continue;
                 
                 case 11: cout << "Thank You For Using My Text Editor" << endl; break;
                 default: cout << "Choice Not Available,Please Choose An Appropriate Choice"; continue;
                                        
                 
                 }
                     
                 system ("Pause");
                 return 0;
                 }
                 
  }

In case 1 you could use getline() instead of the >> operator so that it will accept more than one word. And if you use std::string instead of that char array you don't have to worry about the string size and type almost as many words as you want.

Another way to do it is to put that >> in an infinite loop until you type something like "quit"

char word[255] = {0};
while( strcmp(word,"quit") != 0)
   cin >> word;

or using string

std::string word;
while( word != "quit") )
   cin >> word;

or using getline

std::string word;
getline(cin,word);

i wrote the code u told me to use....it made me abl to write more than a word....but ti only saves the qord " quit" to the text file afterwards....here's the source code for case 1 after modification....try to tell me wat's wrong with it:

case 1: {
                          outfile.open (myFile.c_str(), ios::ate);
                                               
                          char word[255] = {0};
                          while( strcmp(word,"quit") != 0)
                          cin >> word;
                          outfile << word;
                          outfile.close();
                          }
                          continue;
This question has already been answered. Start a new discussion instead.