switch, case help

This is a discussion on switch, case help within the C++ Programming forums, part of the General Programming Boards category; In this code I got case, 1, 2, 3 in the switch, but I am having trouble with 4 and ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    40

    switch, case help

    In this code I got case, 1, 2, 3 in the switch, but I am having trouble with 4 and 5.

    Code:
     
    #include <iostream>
    #include <list>
    #include <NewString.h> // NewString class definition
    
    using namespace std;    
    
    // display program instructions to user
    void instructions()
    {
       cout << "Enter one of the following:\n"
       <<"  1 to create a NewString from a char*\n" 
       <<"  2 to display the current NewString\n"
       <<"  3 to concatenate a second NewString to the current NewString\n" 
       <<"  4 to show the length of the current NewString\n"
       <<"  5 to find out if a given NewString is substring if the current NewString\n";
     
    } // end function instructions
    
    void main()
    {  
       cout << "Testing a string of characters " <<". \n";
    
       instructions();  // display instructions
    
       int    choice;
       char value[100];
    
       NewString*  pCurString = NULL;
       
       do {
          cout << "Enter choice (0 to quit)? ";
          cin >> choice;
           
         switch ( choice ) 
        {
             case 1:
                cout << "Enter a text string: ";
                cin >> value;
                
                if (pCurString != NULL)
                    delete pCurString;   
                pCurString = new NewString(value);            
                break;
             case 2:
                if (pCurString != NULL)
                {                                            
                    cout << "Current NewString = ";
                    pCurString->display();
                    cout << endl << endl;
                }
                break;
             case 3:   // concatenate string
                if (pCurString != NULL)
                {                                           
                    cout << "Current NewString = ";
                    pCurString->display();
                    cout << endl << endl;
                    cout << "Enter string/character to concatenate = ";
                    char cinput[64];
                    cin >> cinput;
    
                    if (strlen(cinput) != 1)
                    {
                        NewString ns(cinput);
                        pCurString->concatenate(ns);   // append a string
                    }
                    else
                    {
                        pCurString->concatenate(cinput[0]);   // append a single char
                    }
                    case 4: 
                    case 5:
            }
                break;  
    
          } // end switch
    
    
        }while ( choice != 0 );  
                                // end do/while
    
       cout << "End list test\n\n";
    
    
    }

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    what seems to be the problem i don't even see anything listed in your case 3 & 4?
    Woop?

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    Actully now that i look at it you need to change this before you even add anything
    Code:
    case 3:   // concatenate string
                if (pCurString != NULL)
                {                                           
                    cout << "Current NewString = ";
                    pCurString->display();
                    cout << endl << endl;
                    cout << "Enter string/character to concatenate = ";
                    char cinput[64];
                    cin >> cinput;
    
                    if (strlen(cinput) != 1)
                    {
                        NewString ns(cinput);
                        pCurString->concatenate(ns);   // append a string
                    }
                    else
                    {
                        pCurString->concatenate(cinput[0]);   // append a single char
                    }
                    case 4: 
                    case 5:
    To This
    Code:
    case 3:   // concatenate string
                if (pCurString != NULL)
                {                                           
                    cout << "Current NewString = ";
                    pCurString->display();
                    cout << endl << endl;
                    cout << "Enter string/character to concatenate = ";
                    char cinput[64];
                    cin >> cinput;
    
                    if (strlen(cinput) != 1)
                    {
                        NewString ns(cinput);
                        pCurString->concatenate(ns);   // append a string
                    }
                    else
                    {
                        pCurString->concatenate(cinput[0]);   // append a single char
                    }
                 }//<----------------Added This
                   break;//<------And this
                    case 4: 
                    case 5:
    Woop?

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    Case 4 and 5 weren' t there because I am trying to figure out how to do it. I have made an attempt at 4, but not sure how to show the length
    Case 5, I have no idea how to start that one!
    Need help, thanks!!

    Code:
     
    #include <iostream>
    #include <list>
    #include <NewString.h> // NewString class definition
    
    using namespace std;    
    
    // display program instructions to user
    void instructions()
    {
       cout << "Enter one of the following:\n"
       <<"  1 to create a NewString from a char*\n" 
       <<"  2 to display the current NewString\n"
       <<"  3 to concatenate a second NewString to the current NewString\n" 
       <<"  4 to show the length of the current NewString\n"
       <<"  5 to find out if a given NewString is substring if the current NewString\n";
     
    } // end function instructions
    
    void main()
    {  
       cout << "Testing a string of characters " <<". \n";
    
       instructions();  // display instructions
    
       int    choice;
       char value[100];
    
       NewString*  pCurString = NULL;
       
       do {
          cout << "Enter choice (0 to quit)? ";
          cin >> choice;
           
         switch ( choice ) 
        {
             case 1:
                cout << "Enter a text string: ";
                cin >> value;
                
                if (pCurString != NULL)
                    delete pCurString;   
                pCurString = new NewString(value);            
                break;
             case 2:
                if (pCurString != NULL)
                {                                            
                    cout << "Current NewString = ";
                    pCurString->display();
                    cout << endl << endl;
                }
                break;
             case 3:   // concatenate string
                if (pCurString != NULL)
                {                                           
                    cout << "Current NewString = ";
                    pCurString->display();
                    cout << endl << endl;
                    cout << "Enter string/character to concatenate = ";
                    char cinput[64];
                    cin >> cinput;
    
                    if (strlen(cinput) != 1)
                    {
                        NewString ns(cinput);
                        pCurString->concatenate(ns);   // append a string
                    }
                    else
                    {
                        pCurString->concatenate(cinput[0]);   // append a single char
                    }
                }
                
                break; 
              case 4:
              	  if (pCurString != NULL)
              	  {
              	     cout << "NewString length = ";
              	     pCurString->cinput();
                    cout << endl << endl;
                }
                break;
                
              case 5:  
                  if (pCurString != NULL)
                  {
                  
                }
                break;
    
          } // end switch
    
    
        }while ( choice != 0 );  
                                // end do/while
    
       cout << "End list test\n\n";
    
    
    }

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    Still trying to figure out how to show the length in case 4:
    In case 5: returns 1 (true) if string2 is a substring of string1 and 0 (false). How do I do an if with a substring?
    Does anybody know this stuff?

    Thanks!!

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Is NewString a class you wrote?
    As for #4, there should either be a size variable in the class or you should have a way of searching through the string to the end (when a null character is reached, for example). I don't know without seeing that class, though.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    Is this what you are looking for? Obviously I had help with this!!

    Code:
    #ifndef NEWSTRING_H
    #define NEWSTRING_H
    #include <list>
    #include <iostream>
    
    using namespace std;
    
    class NewString
    {
    private:
        list<char> m_chars;
        
    public:
        NewString()  {}
        NewString(const char* psz) {  while (*psz != 0) { m_chars.push_back(*psz); psz++; } }
        ~NewString() {}
        
        NewString& concatenate(const NewString& string2) 
        { m_chars.insert(m_chars.end(), string2.m_chars.begin(), string2.m_chars.end()); return *this; }
        NewString& concatenate(char c) { m_chars.push_back(c); return *this; }
        const char* getText(char* buf, size_t sizBuf)  
        { 
            int i = 0; 
            for (list<char>::iterator it = m_chars.begin(); it != m_chars.end(); ++it) 
                buf[i++] =*it; 
            buf[i] = '\0'; 
            return buf; 
        }
        void            display() 
        { 
            for (list<char>::iterator it = m_chars.begin(); it != m_chars.end(); ++it) 
                cout << *it; 
        }
        int             length()    { return (int)m_chars.size(); }
        bool            is_substring(const NewString& string2)     
        { 
            char* pBuf = new char[m_chars.size()+1]; 
            getText(pBuf, m_chars.size()+1) ; 
            char* pStr = new char[string2.m_chars.size()+1]; 
            getText(pStr, string2.m_chars.size()+1) ; 
            bool ret = false;
            if (strstr(pBuf, pStr) != NULL)
                ret = true;
            delete [] pBuf;   
            delete [] pStr;   
        }
        
    }; 
     
    #endif // NEWSTRING_H

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Hmmm.... NewString has a length() function and an is_substring(const NewString& string2) function. Step one is to try to use those to solve your problem. Since the substring problem requires a second string (the substring to lookup), you might have to ask the user to input the second string in case 5.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 09:55 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Replies: 27
    Last Post: 10-11-2006, 05:27 AM
  4. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM

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