Thread: One Error

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

    One Error

    I am getting one error in this code, it is at this line:

    #include NewString.h // NewString class definition
    it says preprocessor syntax error
    Not sure how to get rid of it!!!
    Thanks!!!

    Code:
    // newstring.h
    // ----------------------------------------------------------------------------------------------------
    #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
    
    
    #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;   // Never forget the break
    
          } // end switch
    
    
        }while ( choice != 0 );  
                                // end do/while
    
       cout << "End list test\n\n";
    
    
    } 
    
    //---------------------------------- end-of-main.cpp----------------------------------
    Last edited by campermama; 06-14-2004 at 03:07 PM.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Code:
    #include <iostream>
    #include <list>
    #include NewString.h
    Should Be
    Code:
    #include <iostream>
    #include <list>
    #include "NewString.h"
    Woop?

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    Hmm, now it says it can't be opened.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    put it in the same directory as your project or put it in your include directory
    Woop?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    #include NewString.h // NewString class definition
    it says preprocessor syntax error

    It means you need "" or <> around the filename!!!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    Yup, needed the <> or " " and to put it in the right directory! Thanks!
    Now maybe you can help with the rest? 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";
    
    
    }
    Last edited by campermama; 06-14-2004 at 03:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM