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----------------------------------