-
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";
}
-
what seems to be the problem i don't even see anything listed in your case 3 & 4?
-
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:
-
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 :confused:
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";
}
-
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? :eek:
Thanks!!
-
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.
-
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
-
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.