switch()

This is a discussion on switch() within the C++ Programming forums, part of the General Programming Boards category; I would need some help using switch() statement. We should use switch() statement instead of if() if we have a ...

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

    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 12:51 PM.
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    796
    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 12:55 PM.
    Compiler MSVC++ 2013 with Code::Blocks.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    You would just write:
    Code:
    if (condition1 || condition2)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    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.
    Compiler MSVC++ 2013 with Code::Blocks.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    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-03-2008 at 11:36 PM.
    Compiler MSVC++ 2013 with Code::Blocks.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    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?
    Compiler MSVC++ 2013 with Code::Blocks.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    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
    796
    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 06:18 AM.
    Compiler MSVC++ 2013 with Code::Blocks.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    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...
    Compiler MSVC++ 2013 with Code::Blocks.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    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
    21,941
    Why not post your current code?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
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, 08: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, 05:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21