0

you need to add brackets around cin and cout to make both lines part of the while statement.

while( strcmp(word,"quit") != 0)
{
          cin >> word;
          outfile << word;
}
0

thnx for helping...now in case 6 the merge function, i wrote the idea like this...is to create another text file then output one file containing both files...
i wrote this so far but i can't complete the code for the case:

case 6:  {outfile.open (myFile.c_str(), ios::app);
                           ofstream outfile2 ("text.txt");
                          
                          }
                          continue;
0

I don't think you have to create a third file. You could just append one file to the other. Open the first file for append as you did in case 1 and the second file for read. The use getline() to read each line of file 2 and write it to file1.

0

i wrote it like this but it doesn't work also....

case 6:  {outfile.open (myFile.c_str(), ios::app);
                           ifstream infile2 ("text.txt");
                           
                           
                           outfile << infile2.get();
                           
                          }
                          continue;
0

it worked like a charm...but please can i know why did we put that line and what does it mean ?

0

actually i began to fit in this stuff....i did case 9 on my own......here is my source file so far..
but please i need help in case 10 cause i don't know how to use the toupper command...please look at my progress and try to tell me how can i write case 10....thnx

#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;
    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);
                          cout << "....Type (^z) Whenever You Finish Typing....\n" << endl;                     
                          string word;
                          while( word != "^z" )
                          {
                          cin >> word; 
                          outfile << word << " " ;
                          }
                          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 6:   {
                           outfile.open (myFile.c_str(), ios::app);
                           ifstream infile2 ("text.txt");
                           
                           outfile << ". " << infile2.rdbuf();
                           outfile.close();
                           }
                          
                          continue;
                 case 7:    
                          infile.open( myFile.c_str());
 
                          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 9:  infile.open( myFile.c_str());
 
                          infile.clear();
                          if( infile.is_open() )
                         {
                          string word;
                          string wordtofind;
                          unsigned long wordCount = 0 ;
                          cout << "Please Enter A Word To Search For:  " ;
                          cin >> wordtofind ;
                          while( infile >> word )
                         {
                          if (word == wordtofind){
                          wordCount++;}

                         }

                          cout << "\nThe Word " << wordtofind << " Exists " << wordCount << " Time(s)." << 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;
                 }
                 
  }
0

In this case 11 you will have to rewrite the contents of the file into a new temp file after converting each line to all upper case. Use getline() to read each line then create a loop for the length of the line and convert each character to upper case. After doing that output the line to a temp file. There is a macro toupper() that will convert one charcter to upper case (see link for example)

0

i did this code in case 10 but it doesn't work...can u tell me what's wrong ?

case 10:   {infile.open( myFile.c_str());
                            ofstream out("new.txt");
                            while (infile.good())
                            {
                            (char) infile.get();
                            char c;
                            
                             c = 'infile';
                             out << toupper(c);
                             i++;
                             }
                             infile.close();
                             out.close();
                             continue;
                             }
0

i wrote it like that but still doesn't work...what's wrong in this code:

case 10:   {infile.open( myFile.c_str());
                            ofstream outfile2("new.txt");
                            char c;
                            while (infile.good())
                            cout << (char) infile.get();
                            while( (c = infile.get()) > 0 )
                            cout << (char)toupper(c);
                            outfile2 << toupper(c);
                            infile.close();
                            outfile2.close();}
                            continue;
0

Hopefully you've absorbed some of the information you've been given and used to write your code so far and can now start putting it into practice.

For example what's going to stop this loop from reading to the end of the file?

while (infile.good())
  cout << (char) infile.get();

and what needs to be done to the stream once it reaches the end of the file before you can use it again?

How many of these lines are going to be processed the the while loop?

while( (c = infile.get()) > 0 )
    cout << (char)toupper(c);
    outfile2 << toupper(c);

How are you going to get the information in new.txt back into myFile and do you want to erase the original contents of myFile or do you want to append the new file contents to myfile?

0

actually u gave me no clue...these are the wrong codes i know...and by the way...i want it fixed so that the original file is overwrited with a file with uppercase words...thnx anyway

0

actually...u somehow gave me an idea..
i did it successfully but the problem is that right now it writes on another file (new.txt)...
i want it to overwrite the content of the file with the uppercase words in th original file..

here's my code now...plz tell me what can i replace in the code to make it overwrite rather than creating a new file...

case 10:   {
                            infile.open( myFile.c_str());
                            ofstream outfile("new.txt");
                            char c;
                            infile.clear();
                            
                            
                            while( (c = infile.get()) > 0 )
                            
                            outfile << (char)toupper(c);
                            cout << "File Successfully Converted";
                            infile.close();
                            outfile.close();
                            }
                            continue;
0

what you have is correct so far. Now what you have to do is to delete the original file and rename the new.txt to the name of the original. The remove() function will delete a file and rename() will rename the file. Look up those two functions in your textbook or google for them to see the parameters.

0

i need also another favour...in case 1...i found that i can easily enter as much text as i want to the end of the file and it's functioning correctly...but the problem is that i made the user to type ^z when he wants t end typing...but when i type this...it's save with the text....how can i only save the words that i enter without the command to exit ???...anyway here's the code for case 1:

case 1: {
                          outfile.open (myFile.c_str(), ios::app);
                          cout << "....Type (^z) Whenever You Finish Typing....\n" << endl;                     
                          string word;
                          while( word != "^z" )
                          {
                          cin >> word; 
                          outfile << word << " " ;
                          }
                          outfile.close();
                          }
                          continue;
0

Hii...i think i knew the concept of encryption and decryption....i need to replace each letter with the one that comes after it in the ascii table. In that case 'welcome' should become 'xfmdpnf'..
and for sure this file contains letters which will be all changed by the same way to encrypt them...plz this is the last case left for me...ca anyone help in the code ??

0

anyway...this is all of my code so far...but really i need the encrypt code and maybe then i can try to make the decrypt also...

#include <iostream>
#include <string>
#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;
    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);
                          cout << "....Type (^z) Whenever You Finish Typing....\n" << endl;                     
                          string word;
                          while( word != "^z" )
                          {
                          cin >> word; 
                          outfile << " " << word ;
                          }
                          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 6:   {
                           outfile.open (myFile.c_str(), ios::app);
                           ifstream infile2 ("text.txt");
                           
                           outfile << ". " << infile2.rdbuf();
                           outfile.close();
                           infile2.close();
                           }
                          cout << "File Successfully Merged " << endl;
                          continue;
                 case 7:    
                          infile.open( myFile.c_str());
 
                          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 8:  infile.open( myFile.c_str());
                           
                          infile.clear();
                          if( infile.is_open() )
                         {
                          string word;
                          string wordtofind;
                          
                          cout << "Please Enter A Word To Search For:  " ;
                          cin >> wordtofind ;
                          while( infile >> word )
                         {
                          if (word == wordtofind){
                          cout << "Word Exists In The File";break;}
                          if (word != wordtofind){
                          cout << "Word Doesn't Exist In The File";break;}
                         }
                          
                          
                          infile.close(); 
                         }
                          continue;
                                   
                          
                 case 9:  infile.open( myFile.c_str());
                           
                          infile.clear();
                          if( infile.is_open() )
                         {
                          string word;
                          string wordtofind;
                          unsigned long wordCount = 0 ;
                          cout << "Please Enter A Word To Search For:  " ;
                          cin >> wordtofind ;
                          while( infile >> word )
                         {
                          if (word == wordtofind){
                          wordCount++;}

                         }

                          cout << "\nThe Word " << wordtofind << " Exists " << wordCount << " Time(s)." << endl;
                          infile.close(); 
                         }
                          continue;        
                 
                 
                 
                 case 10:   {
                            infile.open( myFile.c_str());
                            ofstream outfile("new.txt");
                            char c;
                            infile.clear();
                            
                            
                            while( (c = infile.get()) > 0 )
                            
                            outfile << (char)toupper(c);
                            cout << "File Successfully Converted";
                            infile.close();
                            outfile.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;
                 }
                 
  }
0

each char in C/C++ is actually handled internally as an int. The conversion from char viewed to internal int is governed by some agreed upon table, for example ASCII or Unicode. It turns out you can add two chars or do something like char plus an int, etc. That means you can do something like this:

char ch = 'A';
cout << ch + 1;
//out put will be B

Encryption means that you somehow change the original int value of a char value to a different value and display the corresponding char. The conversion protocol should be reproduceable and reversible to be useful. Adding 1 to the original int value of the char is certainly one way to encrypt the chars. The only hitch is z or Z, which will often wrap around to a or A, though you can do it however you want.

0

actually when i write this code......the output is 66 not B.....can u fix that ?

0

i got it fixed like that:

{char ch = 'A';
    ch = ch + 1;
       cout << ch;}
    continue;
0

but now..how can i write this code so as to convert all the file contents by this way ??

0

i wrote this code but something is wrong...would u please check it out...

case 4:  { 
                          infile.open(myFile.c_str());
                          infile.clear();
                          string save;
                          
                          while (infile.eof() == false)
                           {
                                getline(infile,save);
                                                               
                                }
                                save = save + 1;
                                cout << save;
                                }
                          continue;
0

You don't need infile.clear() there and I'd definitely use the keyword break rather than the keyword continue.

Don't use the return value of eof() in a conditional. You're asking for trouble. Use this instead:
while(getline(infile,save))

What's the difference between save and a char? Answer that and you should be able to figure out what to do once you have something in save.

0

save is a string i know...but i don't know how to add all characters in the string just like we did in the character...

0

>>i don't know how to add all characters in the string

You don't. You change the value of each char in the string one char at a time, so if the string entered is add it becomes bee, or whatever.

0

can u write for me this code....cuz really i'm obviously stuck in that case.

0

can u write for me this code....cuz really i'm obviously stuck in that case.

After all the code you have written in this program you still don't know how to create a loop and change each character in the string? Astonishing!

0

You already know how to read the file in one char at a time and convert it (to upper case), i.e.

while( (c = infile.get()) > 0 )
{
    outfile << (char)toupper(c);
}

One thing you could do is to write a function which behaves like toupper(), i.e. takes one character as an input and returns the processed character, so you might code yourself an encrypt() function and use it like ...

while( (c = infile.get()) > 0 )
{
    outfile << (char)[B]encrypt[/B](c);
}

And from there on, you'd probably could quickly derive the counterpart decrypt() function.

0

i've finished my 2 case regarding the encrypt and decrypt and they r working...but the problem is that i've earlier posted that instead instead of writing to a new file i want to overwrite the original file...see the code and please tell me what can i add to overwrite the original file contents with the encrypted contents..

case 4:   {
                          infile.open(myFile.c_str());
                          outfile.open("bola.txt");
                          char c;
                          infile.clear();
                                            
                          while((c = infile.get()) > 0 )
                         {
                          c = c + 1;          
                           outfile << c;                         
                          }
                                                   
    
                      infile.close();
                      outfile.close();
                      continue; }   
                 
                 
                 case 5:  
                         {                    
                          infile.open("bola.txt");
                          outfile.open("bolanew.txt");
                          char c;
                          infile.clear();
                                            
                          while((c = infile.get()) > 0 )
                         {
                          c = c - 1;          
                           outfile << c;                         
                          }
                                                   
    
                      infile.close();
                      outfile.close();
                      continue; }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.