Thread: switch()

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    switch()

    I would need some help using switch() statement.
    We should use switch() statement instead of if() if we have a lot of conditions.
    For keeping it simple i only use two conditions here though.

    I dont really understand what to put in the place of expression
    switch (expression)
    {
    case valueOne: statement;
    break;
    case valueTwo: statement;
    break;
    default: statement;
    }

    That would be the if() version and i would like that both if() would be true and then to do something after
    Code:
    if  (str1[pos-3] != ' '  &&  
         str1[pos-2] != 'a' ) 
    {
        if  (str1[pos-4] != ' ' && 
             str1[pos-3] != 'a' && 
             str1[pos-2] != 'n' )
            
                        str1[pos] = toupper (str1[pos]);
                              pos += str2.size();
    }
    Code:
    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 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' ){ 
                          
                                if  (str1[pos-4] != ' ' && 
                                     str1[pos-3] != 'a' && 
                                     str1[pos-2] != 'n' )
                                    
                                     str1[pos] = toupper (str1[pos]);
                                     pos += str2.size();
                           }              
                    }
             }
             TheCopy << str1 << endl;
       }
       file2.close();
       TheCopy.close();
       return 0;
    }
    Last edited by Ducky; 03-03-2008 at 01:51 PM.
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I dont really understand what to put in the place of expression
    Here's a simple example. We can turn:
    Code:
    if (x == 0)
    {
        foo();
    }
    else if (x == 1)
    {
        bar();
    }
    else if (x == 2)
    {
        baz();
    }
    else
    {
        bazz();
    }
    into:
    Code:
    switch (x)
    {
    case 0:
        foo();
        break;
    case 1:
        bar();
        break;
    case 2:
        baz();
        break;
    default:
        bazz();
    }
    In your case you have:
    Code:
    if (str1[pos-3] != ' ' && str1[pos-2] != 'a' )
    {
        if (str1[pos-4] != ' ' && str1[pos-3] != 'a' && str1[pos-2] != 'n' )
            str1[pos] = toupper (str1[pos]);
        pos += str2.size();
    }
    It does not look like a good candidate to turn into a switch.
    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. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can't see any obvious place to use switch in your code (that would simplify things).

    A side point:
    Code:
                         if  (str1[pos-3] != ' ' &&  
                               str1[pos-2] != 'a' ){ 
                          
                                if  (str1[pos-4] != ' ' && 
                                     str1[pos-3] != 'a' && 
                                     str1[pos-2] != 'n' )
                                    
                                     str1[pos] = toupper (str1[pos]);
                                     pos += str2.size();
                           }
    Looks a bit like it's never going to reach the inner-most part, as if str[pos-3] is ' ', then it wonät be 'a' when it comes to the second if.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thank you very much both of you, now i understand why my program hung.

    Any advice on how to make the two if() statement together?

    Can we use it for example like that:
    if ((condition1) && (condition2))

    do something;
    Last edited by Ducky; 03-03-2008 at 01:55 PM.
    Using Windows 10 with Code Blocks and MingW.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You would just write:
    Code:
    if (condition1 || condition2)
    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. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thanks Laserlight!

    I put a second "()" on it to make it clearer
    Code:
    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();
    Its not working though in the sense that it wont take action on the word "bill"
    if i put "aka" before it. Like 'aka bill'.
    I mean there is no "space" before the last 'a' of 'aka' , so it should though.
    Using Windows 10 with Code Blocks and MingW.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just a warning. Your code is equivalent to:
    Code:
    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();
    If that is not what you intend, you better use braces.
    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. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thanks for pointing it out i changed the code.
    So i still dont understand whats wrong with the code.
    If i only use, i dont dont know how to call it, one "groupe" of condition
    like if (str1[pos-3] != ' ' && str1[pos-2] != 'a' ) it works.

    But if i use two it won't.
    Code:
      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();
    Maybe we cant use two "groupes" of conditions like that?
    Last edited by Ducky; 03-04-2008 at 12:36 AM.
    Using Windows 10 with Code Blocks and MingW.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So i still dont understand whats wrong with the code.
    The problem is that you need to be clear on what is the condition. Your syntax is correct, but the logic as implemented by your code may not be correct.
    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

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Wow you're real fast Laserlight! Thanks!

    Well the condition would be:

    if there is 'a' or 'an' before the word dont go to the "if statement". But it goes...

    How to put it other way then this?
    Using Windows 10 with Code Blocks and MingW.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, your if reads (from what I gather), if there is not " a" before OR there is not " an" before, then execute this IF.
    If you want it to execute the if only if neither " a" or " an" is there, perhaps it should be if not " a" AND not " an".
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    You are right, i should have put '&&' there, thank you, but its still not working.
    Actually now it just hangs.

    Im gonna thinking...
    Last edited by Ducky; 03-04-2008 at 07:18 AM.
    Using Windows 10 with Code Blocks and MingW.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    I was thinking...

    Yeah actually if i put AND there, like i should by the way, that will give us the same problem
    pointed out by Matsp above, namely that str1[pos-3] cant be a 'space' and an 'a' at the same time.
    Thats why the program hangs if we put && instead of ||.

    Im completely at lost here. It looks like a vicious circle.
    There got to be a solution there though...
    Using Windows 10 with Code Blocks and MingW.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    if (str1[pos - 1] != ' ' && str1[pos - 1] != 'a')
    And so on, might work.
    I'm not sure how you word(s) look like, so I can't say further.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why not post your current code?
    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

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