Thread: switch()

  1. #16
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Of course.
    Code:
    #include <string>
    #include <cctype>
    #include <vector>
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    int main (){
       
       string FileName;
       cout << "Enter file name : ";
       cin >>   FileName;
       ifstream file1(FileName.c_str());
       ifstream file2 ("List of proper nouns.txt");
       if (!file1){
           cout <<"\nThere is no file, named --> " << FileName << " <-- in your folder.\n\n"
           "What a loser!  Give it up, you're never gonna make it!  :-)\n\n";
           system ("pause");
           return(1);
       }
       ofstream TheCopy;
       TheCopy.open ("Your_Uppercased_Proper_Nouns_Copy.srt",ios::app);
       string str1;
       string str2;
       vector<string> myvector;
       size_t pos = 0;
          
       while (getline(file2, str2))
       {       
           myvector.push_back(str2);
       }    
           
       while (getline(file1, str1))
       {
             for ( int i = 0; i < myvector.size(); i++)
             {
                        
                    while ((pos = str1.find(myvector[i], pos)) != string::npos)
                    {               
                          if ((str1[pos-3] != ' ' &&  str1[pos-2] != 'a' ) &&
                              (str1[pos-4] != ' ' &&  str1[pos-3] != 'a' && str1[pos-2] != 'n'))
                          {
                             str1[pos] = toupper (str1[pos]);
                             pos += str2.size();
                          }
                          pos += str2.size();
                    }
             }
             TheCopy << str1 << endl;              
       }
       
       file1.close();  
       file2.close();    
       TheCopy.close();
       return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, a few comments.

    1. I think the condition should be:
    Code:
    !(str1[pos-3] == ' ' &&  str1[pos-2] == 'a') && !(str1[pos-4] == ' ' &&  str1[pos-3] == 'a' && str1[pos-2] == 'n')
    2. You need to handle cases where the noun is found at the start of the search string.

    3. Rename myvector to nouns, or some other descriptive name.

    4. Technically, pos should be a std::string::size_type, though this is by default a typedef that boils down size_t. It should be declared near first use, e.g., in the for loop.

    5. i should be a std::string::size_type.

    6. You have two instances of "pos += str2.size();". You probably should remove one of them.

    7. You do not actually need str1 and str2. Just a single string object will do as it can be reused.

    8. It makes sense to close files as soon as you are done reading from or writing to them. Consider this for "List of proper nouns.txt".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Wow thats really cool!

    Im gonna change my code and will let you know if it worked.

    Thank you!
    Using Windows 10 with Code Blocks and MingW.

  4. #19
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    2. You need to handle cases where the noun is found at the start of the search string.
    Yes i thought about it. The thing is that im gonna have a lot of cases, like 20 or more.
    Now would it be possible to put them in a file apart like #include "conditions.h" and
    make a reference to if() like: if ( conditions.h)

    4. Technically, pos should be a std::string::size_type, though this is by default a typedef that boils down to size_t. It should be declared near first use, e.g., in the for loop.
    In this case if i declare it in the while loop, i must declare it two times,
    thats why i think its maybe better to declare it outside, only once.

    6. You have two instances of "pos += str2.size();". You probably should remove one of them.
    I have two instances because if if() wont execute, i still want it to continue to search
    for the next noun so i still need to increment it.


    Otherwise with your condition it works perfectly well.
    Wich is comletely incomprehensible because
    These two are exactly the same :

    if ( !( x == 5 ) ) Logical NOT operator
    if ( x != 5 ) not equals (!=) operator

    What even stranger is that if i use my version, the not equals (!=) operator,

    if ((str1[pos-3] != ' ' && str1[pos-2] != 'a' ) &&
    (str1[pos-4] != ' ' && str1[pos-3] != 'a' && str1[pos-2} != 'n'))

    it messes up so much the memory that even if i change it with yours, the code wont work anymore.

    Code:
    void openFile(ifstream& file){
              
            file.clear();
            for(;;)
            {
                string fileName;
                cout << "Enter the name of your file: " ;
                cin >> fileName;
                file.open(fileName.c_str());
                if (file.good())
                {
                    break;
                }
                cerr << "\nThere is no file, named --> " << fileName << " <-- in your folder.\n\n" << endl;
            file.clear();
            }
    }
    
    int main ()
    {   
       ifstream  file1;
       openFile(file1);
       ifstream  file2 ("List of proper nouns.txt");
       ofstream TheCopy;
       TheCopy.open ("Your_Uppercased_Proper_Nouns_Copy.srt",ios::app);
       string str;
       vector<string> vNouns;
       string::size_type pos = 0;
          
       while (getline(file2, str))
       {        
           vNouns.push_back(str);
       }
       file2.close();
       while (getline(file1, str))
       {             
             for ( string::size_type i = 0; i < vNouns.size(); i++)
             {           
                    while ((pos = str.find(vNouns[i], pos)) != string::npos)
                    {         
                          if (!(str[pos-3] == ' ' &&  str[pos-2] == 'a')&&
                              !(str[pos-2] == 'a' &&  str[pos-2] == str[0]) &&
                              !(str[pos-4] == ' ' &&  str[pos-3] == 'a' &&  str[pos-2] == 'n') &&
                              !(str[pos-5] == ' ' &&  str[pos-4] == 'a' &&  str[pos-3] == 'n'  && str[pos-2] == 'y'))
                          {
                             str[pos] = toupper (str[pos]);
                             pos += vNouns[i].size();
                          }
                          pos += vNouns[i].size();     
                    }
             }
             //cout<<str[0]<<endl;
             cout<<str<<endl;
             //TheCopy << str << endl;
       }
       file1.close();
       TheCopy.close();
       system("pause");
       return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes i thought about it. The thing is that im gonna have a lot of cases, like 20 or more.
    Now would it be possible to put them in a file apart like #include "conditions.h" and
    make a reference to if() like: if ( conditions.h)
    No, you should not do that. I believe you do not have so many cases, actually. What do you think these cases are?

    In this case if i declare it in the while loop, i must declare it two times,
    thats why i think its maybe better to declare it outside, only once.
    Do not worry about that: the compiler can optimise it for you.

    Otherwise with your condition it works perfectly well.
    Wich is comletely incomprehensible because
    These two are exactly the same :
    Your expression:
    Code:
    (str1[pos-3] != ' ' &&  str1[pos-2] != 'a' ) && (str1[pos-4] != ' ' &&  str1[pos-3] != 'a' && str1[pos-2] != 'n')
    is equivalent to:
    Code:
    (!(str1[pos-3] == ' ') &&  !(str1[pos-2] == 'a')) && (!(str1[pos-4] == ' ') &&  !(str1[pos-3] == 'a') && !(str1[pos-2] == 'n'))
    as you have pointed out. Now, using De Morgan's rule, we get:
    Code:
    !(str1[pos-3] == ' ' || str1[pos-2] == 'a') && !(str1[pos-4] == ' ' ||  str1[pos-3] == 'a' || str1[pos-2] == 'n')
    which is obviously not the same as my suggestion:
    Code:
    !(str1[pos-3] == ' ' &&  str1[pos-2] == 'a') && !(str1[pos-4] == ' ' &&  str1[pos-3] == 'a' && str1[pos-2] == 'n')
    which is equivalent to:
    Code:
    !((str1[pos-3] == ' ' &&  str1[pos-2] == 'a') || (str1[pos-4] == ' ' &&  str1[pos-3] == 'a' && str1[pos-2] == 'n'))
    it messes up so much the memory that even if i change it with yours, the code wont work anymore.
    What do you mean?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    "What do you think these cases are?"
    Words that shouldnt or rarely be before proper nouns like:
    ( a, an, the, any, some, much, many, all, few, lot of ) maybe more..
    all this multiplied by two because there is the case when they are at the start of the line.

    "Do not worry about that: the compiler can optimise it for you."
    I declared it like that and it still complaining about expected primary expression and missing ()
    while (((string::size_type pos) = str.find(vNouns[i],(string::size_type pos))) != string::npos)

    "it messes up so much the memory that even if i change it with yours, the code wont work anymore. What do you mean?"

    It compiles, it executes and then it just hangs.
    Using Windows 10 with Code Blocks and MingW.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Words that shouldnt or rarely be before proper nouns like:
    ( a, an, the, any, some, much, many, all, few, lot of ) maybe more..
    all this multiplied by two because there is the case when they are at the start of the line.
    Ah. At this point it looks like you may want to consider having a vector of these words. Then instead of doing character-wise comparison, you search based on strings.

    I declared it like that and it still complaining about expected primary expression and missing ()
    More like:
    Code:
    for (string::size_type i = 0; i < vNouns.size(); i++)
    {
        string::size_type pos;
        while ((pos = str.find(vNouns[i], pos)) != string::npos)
    It compiles, it executes and then it just hangs.
    Sounds like there still is a logic error, e.g., one that is causing an infinite loop or something.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Quote Originally Posted by laserlight View Post
    Ah. At this point it looks like you may want to consider having a vector of these words. Then instead of doing character-wise comparison, you search based on strings.
    I have to think about it cause right now i dont know how to realize this.
    Lets say i put 3 elements in a vector: a, an, the.
    Then i will find the word "bill" for example.
    Now how will i tell the program to search for all the elements before "bill" when they arent the same length.
    I mean i cant point out where to search exactly.
    Unless i create several vectors with each of words of the same length.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM