|
Dear friends,
I got an edit box, the user enters the string in the edit box that my application collects in the associated CString variable m_edit_customerAddress .
But the problem is that users are entering characters like "space bars", "tabs" "new line" , in the beginning of the string and at the end of the string. What i want to remove thiese characters from m_edit_customerAddress . How can i do so ?
Imtiaz
|
|
|
|
|
Imtiaz Murtaza wrote:
...users are entering characters like "space bars", "tabs" "new line" , in the beginning of the string and at the end of the string.
Is this a multi-line edit control? In any case, after the variable is populated with the control's contents, simply call iswhite() for each character.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
You can use the CString member function ::TrimLeft() and ::TrimRight() to remove any unwanted characters at either end of your string.
If your string has a mixture of these characters, then its a bit more difficult to infer where your 'valid' input string starts and ends.
I Dream of Absolute Zero
|
|
|
|
|
Ok..So I'm making a program for the "Connect Four" game. You know the one that you try to get 4 colors in a row by dropping a chip in between 2 players.
Well My problem is my function called displayBoard this method places 2 different kinds of markers on the board (one for each player) and displays some kind of marker for empty slots. Be sure your choices are easy to see.(i.e. X's for player 1, O's for players 2)
Well having the data members being...
//data members
int board[12][13];
int numChips[7];
how would I write the function...displayBoard?
All I have so far is this in my .cpp file
void ConnectFour::displayBoard()
{
for(int i=0; i < 12; i++)
{
for(int j=0; j<13; j++)
{
cout << board[i][j] <<" ";
}
cout << endl;
}
}
|
|
|
|
|
The displayBoard() method is fine. What you need to do is populate the board variable with Xs and Os.
You need a method that asks the user which of the seven slots they want to insert a piece into. If that slot is not full, add a piece to it (this is the real meat of your program). Otherwise, tell them the slot is full and ask again.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
ok...but all i have is
cout << "What column do you want to drop a chip in?;
cin >> //do i use an arrary for cin?
for(int j=0; j<13; j++)
{
cout << board[j] << "X";
}
~jason
|
|
|
|
|
Based on this this picture, you should have a 6-row by 7-column board. That said, you could have something like:
int nColumn;
cout << "What column do you want to drop a chip in (1-7)? ";
cin >> nColumn;
for (int nRow = 0; nRow < 6; nRow++)
{
if (' ' == board[][])
{
board[][] = 'X';
break;
}
} I'll leave the two [][] for you to conquer.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
How's it going with this exercise?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
Good..I got it takeing care of....thanks for checking up on me! I have so much trouble with this class. I"m strungling to pass =\
~Jason
|
|
|
|
|
hi!
i've just finished writing a linked list template class, which compiles fine, but when i try to use it in another file (main.cpp) in my console test app' i get LNK2019 errors for every method of the class being used. i've copied the class & output below, so if anyone has any ideas i would REALLY appreciate some help because i'm completely stuck!! thanks!!
#ifndef _LINKEDLIST_H_
#define _LINKEDLIST_H_
#pragma message (" -+- COMPILING " __FILE__)
// declare classes...
template <class type> class ListElement;
template <class type> class LinkedList;
/*
************************
linkedlist class
************************
*/
template <class type> class LinkedList
{
public:
//
// constructor/destructor
//
LinkedList<type>();
~LinkedList<type>();
//
// public methods
//
bool InsertNext(ListElement<type>* pElement, const type* pData);
bool RemoveNext(ListElement<type>* pElement, type** ppData);
ListElement<type>* GetHead();
ListElement<type>* GetTail();
int GetSize();
bool IsHead(const ListElement<type>& element);
bool IsTail(const ListElement<type>& element);
bool IsEmpty();
ListElement<type>* GetElementByIndex(int index);
LinkedList<type>* Clone();
LinkedList<type>* Copy();
void FreeData();
private:
//
// private methods
//
void SetHead(ListElement<type>* pHead);
void SetTail(ListElement<type>* pTail);
void SetSize(int size);
//
// private member data
//
int m_size;
ListElement<type>* m_pHead;
ListElement<type>* m_pTail;
};
/*
*************************
listelement class
*************************
*/
template <class type> class ListElement
{
public:
//
// constructor/destructor
//
ListElement();
~ListElement();
//
// public methods
//
type* GetData();
void SetData(type* pData);
void FreeData();
ListElement<type>* GetNext();
void SetNext(ListElement<type>* pNext);
type* CloneData();
private:
//
// private member data
//
type* m_pData;
ListElement<type>* m_pNext;
};
#endif // _LINKEDLIST_H_
#pragma message (" -+- COMPILING " __FILE__)
#include <stdlib.h>
#include "linkedlist.h"
/*
************************
linkedlist class
************************
*/
template <class type>
LinkedList<type>::LinkedList() : m_size(0), m_pHead(NULL), m_pTail(NULL)
{
// do nothing
}
template <class type>
LinkedList<type>::~LinkedList()
{
if (m_pHead != NULL)
delete m_pHead;
}
template <class type>
bool LinkedList<type>::InsertNext(ListElement<type>* pElement, const type* pData)
{
ListElement<type>* pNewElement = new ListElement<type>;
if (pNewElement == NULL)
return false;
pNewElement->SetData(const_cast<type*> (pData));
if (pElement == NULL) // insertion @ head of list...
{
if (this->GetSize() == 0)
this->SetTail(pNewElement);
pNewElement->SetNext(this->GetHead());
this->SetHead(pNewElement);
}
else // insertion somewhere other than @ the head...
{
if (pElement->GetNext() == NULL)
this->SetTail(pNewElement);
pNewElement->SetNext(pElement->GetNext());
pElement->SetNext(pNewElement);
}
this->SetSize(this->GetSize() + 1);
return true;
}
template <class type>
bool LinkedList<type>::RemoveNext(ListElement<type>* pElement, type** ppData)
{
ListElement<type>* pOldElement;
if (this->GetSize() == 0)
return false;
// if null then handle removal from head of list...
if (pElement == NULL)
{
*ppData = this->GetHead()->GetData();
pOldElement = this->GetHead();
this->SetHead(this->GetHead()->GetNext());
if (this->GetSize() == 1)
{
this->SetTail(NULL);
}
}
else
{
if (pElement->GetNext() == NULL)
return false;
*ppData = pElement->GetNext()->GetData();
pOldElement = pElement->GetNext();
pElement->SetNext(pElement->GetNext()->GetNext());
if (pElement->GetNext() == NULL)
{
this->SetTail(pElement);
}
}
delete pOldElement;
this->SetSize(this->GetSize() - 1);
return true;
}
template <class type>
ListElement<type>* LinkedList<type>::GetHead()
{
return m_pHead;
}
template <class type>
ListElement<type>* LinkedList<type>::GetTail()
{
return m_pTail;
}
template <class type>
int LinkedList<type>::GetSize()
{
return m_size;
}
template <class type>
bool LinkedList<type>::IsHead(const ListElement<type>& element)
{
return this->GetHead() == element ? true : false;
}
template <class type>
bool LinkedList<type>::IsTail(const ListElement<type>& element)
{
return this->GetTail() == element ? true : false;
}
template <class type>
bool LinkedList<type>::IsEmpty()
{
return this->GetSize() == 0 ? true : false;
}
template <class type>
ListElement<type>* LinkedList<type>::GetElementByIndex(int index)
{
int i;
ListElement<type>* pTmp = NULL;
if (this->GetSize() == 0 || index >= this->GetSize())
return pTmp;
pTmp = this->GetHead(); // element zero
for (i = 1; i <= index; i++)
{
pTmp = pTmp->GetNext();
}
return pTmp;
}
template <class type>
LinkedList<type>* LinkedList<type>::Clone()
{
LinkedList<type>* pNewList = NULL;
ListElement<type>* pSrcElement = NULL;
if (this->GetSize() == 0)
return pNewList;
pNewList = new LinkedList<type>;
pSrcElement = this->GetHead();
pNewList->InsertNext(NULL, pSrcElement->CloneData());
while (pSrcElement->GetNext() != NULL)
{
pSrcElement = pSrcElement->GetNext();
pNewList->InsertNext(pNewList->GetTail(), pSrcElement->CloneData());
}
return pNewList;
}
template <class type>
LinkedList<type>* LinkedList<type>::Copy()
{
LinkedList<type>* pNewList = NULL;
ListElement<type>* pSrcElement = NULL;
if (this->GetSize() == 0)
return pNewList;
pNewList = this;
pSrcElement = this->GetHead();
pNewList->InsertNext(NULL, pSrcElement->CloneData());
while (pSrcElement->GetNext() != NULL)
{
pSrcElement = pSrcElement->GetNext();
pNewList->InsertNext(pNewList->GetTail(), pSrcElement->GetData());
}
return pNewList;
}
template <class type>
void LinkedList<type>::FreeData() // only call when data is allocated on the heap
{
ListElement<type>* pElement = NULL;
if (this->GetSize() == 0)
return;
pElement = this->GetHead();
pElement->FreeData();
while (pElement->GetNext() != NULL)
{
pElement = pElement->GetNext();
pElement->FreeData();
}
}
template <class type>
void LinkedList<type>::SetHead(ListElement<type>* pHead)
{
m_pHead = pHead;
}
template <class type>
void LinkedList<type>::SetTail(ListElement<type>* pTail)
{
m_pTail = pTail;
}
template <class type>
void LinkedList<type>::SetSize(int size)
{
m_size = size;
}
/*
*************************
listelement class
*************************
*/
template <class type>
ListElement<type>::ListElement<type>() : m_pData(NULL), m_pNext(NULL)
{
// do nothing
}
template <class type>
ListElement<type>::~ListElement<type>()
{
if (m_pNext != NULL)
delete m_pNext;
}
template <class type>
type* ListElement<type>::GetData()
{
return m_pData;
}
template <class type>
void ListElement<type>::SetData(type* pData)
{
m_pData = pData;
}
template <class type>
void ListElement<type>::FreeData()
{
if (m_pData != NULL)
delete m_pData;
}
template <class type>
ListElement<type>* ListElement<type>::GetNext()
{
return m_pNext;
}
template <class type>
void ListElement<type>::SetNext(ListElement<type>* pNext)
{
m_pNext = pNext;
}
template <class type>
type* ListElement<type>::CloneData()
{
type* pNewData = new type;
*pNewData = *m_pData;
return pNewData;
}
Linking...
main.obj : error LNK2019: unresolved external symbol "public: __thiscall Stack<int,100>::~Stack<int,100>(void)" (??1?$Stack@H$0GE@@@QAE@XZ) referenced in function "void __cdecl stackfunc(void)" (?stackfunc@@YAXXZ)
main.obj : error LNK2019: unresolved external symbol "public: __thiscall Stack<double,30>::~Stack<double,30>(void)" (??1?$Stack@N$0BO@@@QAE@XZ) referenced in function "void __cdecl stackfunc(void)" (?stackfunc@@YAXXZ)
main.obj : error LNK2019: unresolved external symbol "public: void __thiscall Stack<int,100>::push(int const &)" (?push@?$Stack@H$0GE@@@QAEXABH@Z) referenced in function "void __cdecl stackfunc(void)" (?stackfunc@@YAXXZ)
main.obj : error LNK2019: unresolved external symbol "public: void __thiscall Stack<double,30>::push(double const &)" (?push@?$Stack@N$0BO@@@QAEXABN@Z) referenced in function "void __cdecl stackfunc(void)" (?stackfunc@@YAXXZ)
main.obj : error LNK2019: unresolved external symbol "public: bool __thiscall Stack<int,100>::isEmpty(void)const " (?isEmpty@?$Stack@H$0GE@@@QBE_NXZ) referenced in function "void __cdecl stackfunc(void)" (?stackfunc@@YAXXZ)
main.obj : error LNK2019: unresolved external symbol "public: __thiscall Stack<double,30>::Stack<double,30>(void)" (??0?$Stack@N$0BO@@@QAE@XZ) referenced in function "void __cdecl stackfunc(void)" (?stackfunc@@YAXXZ)
main.obj : error LNK2019: unresolved external symbol "public: __thiscall Stack<int,100>::Stack<int,100>(void)" (??0?$Stack@H$0GE@@@QAE@XZ) referenced in function "void __cdecl stackfunc(void)" (?stackfunc@@YAXXZ)
main.obj : error LNK2019: unresolved external symbol "public: class LinkedList<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > * __thiscall LinkedList<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::Clone(void)" (?Clone@?$LinkedList@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAEPAV1@XZ) referenced in function "void __cdecl listfunc(void)" (?listfunc@@YAXXZ)
main.obj : error LNK2019: unresolved external symbol "public: class ListElement<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > * __thiscall LinkedList<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::GetElementByIndex(int)" (?GetElementByIndex@?$LinkedList@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAEPAV?$ListElement@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@H@Z) referenced in function "void __cdecl listfunc(void)" (?listfunc@@YAXXZ)
main.obj : error LNK2019: unresolved external symbol "public: class ListElement<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > * __thiscall ListElement<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::GetNext(void)" (?GetNext@?$ListElement@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAEPAV1@XZ) referenced in function "void __cdecl listfunc(void)" (?listfunc@@YAXXZ)
main.obj : error LNK2019: unresolved external symbol "public: class ListElement<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > * __thiscall LinkedList<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::GetHead(void)" (?GetHead@?$LinkedList@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAEPAV?$ListElement@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@XZ) referenced in function "void __cdecl listfunc(void)" (?listfunc@@YAXXZ)
main.obj : error LNK2019: unresolved external symbol "public: int __thiscall LinkedList<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::GetSize(void)" (?GetSize@?$LinkedList@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAEHXZ) referenced in function "void __cdecl listfunc(void)" (?listfunc@@YAXXZ)
main.obj : error LNK2019: unresolved external symbol "public: class ListElement<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > * __thiscall LinkedList<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::GetTail(void)" (?GetTail@?$LinkedList@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAEPAV?$ListElement@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@XZ) referenced in function "void __cdecl listfunc(void)" (?listfunc@@YAXXZ)
main.obj : error LNK2019: unresolved external symbol "public: bool __thiscall LinkedList<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::InsertNext(class ListElement<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const *)" (?InsertNext@?$LinkedList@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE_NPAV?$ListElement@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@PBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "void __cdecl listfunc(void)" (?listfunc@@YAXXZ)
main.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * __thiscall ListElement<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::GetData(void)" (?GetData@?$ListElement@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAEPAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function "void __cdecl listfunc(void)" (?listfunc@@YAXXZ)
main.obj : error LNK2019: unresolved external symbol "public: void __thiscall ListElement<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::SetData(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *)" (?SetData@?$ListElement@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAEXPAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "void __cdecl listfunc(void)" (?listfunc@@YAXXZ)
main.obj : error LNK2019: unresolved external symbol "public: __thiscall ListElement<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::ListElement<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(void)" (??0?$ListElement@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE@XZ) referenced in function "void __cdecl listfunc(void)" (?listfunc@@YAXXZ)
main.obj : error LNK2019: unresolved external symbol "public: __thiscall LinkedList<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::LinkedList<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(void)" (??0?$LinkedList@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE@XZ) referenced in function "void __cdecl listfunc(void)" (?listfunc@@YAXXZ)
main.obj : error LNK2019: unresolved external symbol "public: __thiscall LinkedList<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::~LinkedList<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(void)" (??1?$LinkedList@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE@XZ) referenced in function "public: void * __thiscall LinkedList<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::`scalar deleting destructor'(unsigned int)" (??_G?$LinkedList@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAEPAXI@Z)
Debug/list_test.exe : fatal error LNK1120: 19 unresolved externals
list_test - 20 error(s), 2 warning(s)
---------------------- Done ----------------------
Build: 0 succeeded, 1 failed, 0 skipped
|
|
|
|
|
You might want to use <pre> tags in the future so that code is readable. Does main.cpp include the file linkedlist.h ?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
here's a bit of main.cpp:
#include <string>
#include <iostream>
using namespace std;
#include "linkedlist.h"
void listfunc()
{
ListElement<string>* pStrElmt = NULL;
LinkedList<string>* pStrList = new LinkedList<string>;
string sData1("NUMERO UNO!");
string sData2("NUMERO DOS!");
string sData3("NUMERO TRES!");
string sData4("NUMERO QUATRO!");
string sData5("NUMERO CINQO!");
// element 1
pStrElmt = new ListElement<string>;
pStrElmt->SetData(&sData1);
pStrList->InsertNext(NULL, pStrElmt->GetData());
// element 2
pStrElmt = new ListElement<string>;
pStrElmt->SetData(&sData2);
pStrList->InsertNext(pStrList->GetTail(), pStrElmt->GetData());
// etc...
as you can see i've #include'd where needed, & it's compiled fine, but the linker doesn't like it.
|
|
|
|
|
Seems like you didnt add the file to your project via Project->Add to project->File
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
i'm pretty sure i did.
i'm using vs.net 2003, & added all files to the project by doing:
'project->add new item' (or it's equivalent)
there's a beginners template tutorial (part 1) on this site submitted by a chap called stefan spenz which i copied into my project in the usual way i.e. class definition into header & class implementation into source file (.cpp), then i tried to build the project with the class being used in main.cpp (with #include "stack.h" for new class where needed) and i had the same problem.
i'm obviously missing something about templates!?
|
|
|
|
|
Milby007 wrote:
there's a beginners template tutorial (part 1) on this site submitted by a chap called stefan spenz which i copied into my project in the usual way i.e. class definition into header & class implementation into source file (.cpp), then i tried to build the project with the class being used in main.cpp (with #include "stack.h" for new class where needed) and i had the same problem.
There is your problem. You should include the template implementation in the header file. Read this[^] faq about C++ templates and you should do fine...
Multiply it by infinity and take it beyond eternity and you'll still have no idea about what I'm talking about.
|
|
|
|
|
Example:
#include <tuple.hpp>
What does the hpp mean? Is it just for the human eye? Is it any different to the compiler than a .h file?
thanks
|
|
|
|
|
It's the name of a header file for a corresponding .cpp file. With C, you have .c and .h files. With C++, you have .cpp and .hpp files. In the past 15 years, I've never used a .hpp file.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
It's just for the human eye, the compiler doesn't care.
|
|
|
|
|
The compiler does not care. SOME (but not all) editors switch to C++ mode when the see *.hpp.
|
|
|
|
|
|
I've got an About CDialog that has 2 buttons on it (OK, Release Notes). When I click the 'X' to close the dialog it acts as though I clicked on the Release Notes button.
I can close the dialog by clicking OK, any ideas on getting it to close on 'X'?
BW
The Biggest Loser
"Farm Donkey makes us laugh. Farm Donkey hauls some ass." -The Stoves
|
|
|
|
|
Make sure the ID of the 'Release Notes' buttons is not IDCANCEL.
|
|
|
|
|
Ah, good thought, but no it's ID_RELEASE.
BW
The Biggest Loser
"Farm Donkey makes us laugh. Farm Donkey hauls some ass." -The Stoves
|
|
|
|
|
brianwelsch wrote:
...it acts as though I clicked on the Release Notes button.
So when the "X" is clicked, the handler function for the "Release Notes" button is getting called. Yes?
What is the value of ID_RELEASE ?
What does the About dialog's message map look like?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
DavidCrow wrote:
So when the "X" is clicked, the handler function for the "Release Notes" button is getting called. Yes?
Yes
What is the value of ID_RELEASE?
2
What does the About dialog's message map look like?
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)<br />
ON_BN_CLICKED(ID_RELEASE, OnRelease)<br />
END_MESSAGE_MAP()
BW
The Biggest Loser
"Farm Donkey makes us laugh. Farm Donkey hauls some ass." -The Stoves
|
|
|
|
|