|
Take the input as a string, then strip out the comma and then use atoi to convert the string to an integer.
string input;
int number;
cin >> input;
int pos = input.find(',');
while (pos != -1)
{
input.replace(pos, 1, "");
pos = input.find(',');
}
number = atoi(input.c_str());
cout << number << endl;
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04
Within you lies the power for good - Use it!
|
|
|
|
|
Hi, i'm not sure what this is called cause its the first time i ever saw it :P so here's some code to show the situation...i need help correcting any errors or what not to make it compile.
//BSTree class
typedef void (*VisitFunc)( Item & item );
void InOrder( VisitFunc f )
{
//stuff
}
---
//extended BSTree class
void SortTree()
{
InOrder(VisitingSortFunction);
}
void VisitingSortFunction(Item & item)
{
//stuff that i want to be executed in InOrder(
}
//error message i get
'void BSTree::InOrder(VisitFunc)' : cannot convert parameter 1 from 'void (Item &)' to 'VisitFunc'
|
|
|
|
|
just as a test, i put this code into a new console app, and it compiled just fine, after i moved the body of VisitingSortFunction above the body of SortTree (to fix an undeclared identifier issue with VisitingSortFunction).
class Item
{
};
typedef void (*VisitFunc)( Item & item );
void InOrder( VisitFunc f )
{
}
void VisitingSortFunction(Item & item)
{
}
void SortTree()
{
InOrder(VisitingSortFunction);
}
int main(int argc, char* argv[])
{
return 0;
}
Software | Cleek
|
|
|
|
|
Hmm i guess when simplifying my code for this thread i must of eliminated what leads to my problem...Thanks for help.
|
|
|
|
|
Hmm i'm still confussed :P...This simplified code generates the error.
typedef int Item;
typedef void (*VisitFunc)( Item & item );
class BSTree
{
private:
void InOrder( VisitFunc f)
{}
};
class BSTreeExtendedWithSort : public BSTree
{
public:
void SortTree()
{
InOrder(VisitingSortFunction);
}
private:
void VisitingSortFunction(Item & item)
{}
};
int main()
{
return 0;
}
|
|
|
|
|
well, now you're up against the fact that (non-static) C++ member functions aren't quite the same as C functions. you can't really pass them around as function pointers.
this works:
class BSTree
{
public:
void InOrder( VisitFunc f)
{}
};
class BSTreeExtendedWithSort : public BSTree
{
public:
static void VisitingSortFunction(Item & item)
{}
void SortTree()
{
InOrder(VisitingSortFunction);
}
private:
};
but that 'static' raises a whole bunch of class/object issues.
it's usually pretty safe to say that if you find yourself wanting to pass pointers to member functions around, that you need to take a good hard look at your class relationships. instead of passing pointers to funtions, you'll need to consider passing objects that can see the data they need to operate on.
Software | Cleek
|
|
|
|
|
Thx for help compiles good.
|
|
|
|
|
As Chris said, the design isn't the greatest, but to clear up the typedef question, a member function and a global function are not type-compatible unless the member is static , because the member function needs a this object. In your code, if you change the typedef to:
typedef void (*<font color=red>BSTreeExtendedWithSort::</font>VisitFunc)(Item& item); then it will do what you want (well, once you sort out the class name problems that arise).
--Mike--
LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ | You Are Dumb
|
|
|
|
|
I have a CString that contains a multiple number of %s substrings. I also have a CStringArray such that array member one should replace the first occurrence of %s, array member two should replace the second occurrence of %s, etc. etc. up to the number of %s'. I probably should be using the FormatV member function, but I can't get my head around the va_arg type at the moment. A pointer or two would be appreciated. Thanks.
Chris Meech
I am Canadian. [heard in a local bar]
Gently arching his fishing rod back he moves the tip forward in a gentle arch releasing the line.... kersplunk [Doug Goulden]
|
|
|
|
|
Just a suggestion, as I am unclear as what you are trying to do, but it seems to me that if all you are trying to do is insert strings into another string, wouldn't it work easier to break up the format string and just use the += operator to concatinate the strings?
As for using variable arguments, I could help you if I had a little more info on what you are trying to do.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04
Within you lies the power for good - Use it!
|
|
|
|
|
I think what he's saying is:
CString strFormat = _T("One: %s, Two: %s ... Fifty: %s");
CStringArray strValues; (filled with 50 values)
What he wants:
strValues[0] takes the place of the %s that follows One.
strValues[1] takes the place of the %s that follows Two.
etc...
What he's most likely looking for is something simple like...
CString strText;
strText.Format(strFormat, strValues);
as opposed to
strText.Format(strFormat, strValues[0], strValues[1], ... strValues[49]);
I've never actually used an array with Format, so I haven't the foggiest.
Pssst. You see that little light on your monitor? That's actually a government installed spy camera. Smile and wave to big brother!
|
|
|
|
|
Thanks for the assist, PJ. Sorry I wasn't too clear. The others were on the right track though and I have things working. The va_ macros are for building a function that receives a variable argument list. In my situation though, it was more that I had a variable number of elements to pass to a function. The odd part is that the variable number wouldn't be known until runtime when the format string is picked up from a database call.
Appreciate the help though.
Chris Meech
I am Canadian. [heard in a local bar]
Gently arching his fishing rod back he moves the tip forward in a gentle arch releasing the line.... kersplunk [Doug Goulden]
|
|
|
|
|
Assuming you don't want to do this:
CString str,fmt;
CStringArray arr;
fmt = _T("%s blah %s blah ... %s blah");
str.Format(fmt,arr[0],arr[1],...,arr[]); I don't think FormatV will do what you want. It's used within functions that have variable numbers of arguments. Any approach like this will also behave badly if there's a mismatch between the number of %s format specifiers and the number array elements.
Something like the following ought to do it:
CString str,fmt;
CStringArray arr;
fmt = _T("%s blah %s blah ... %s blah");
str = fmt;
int offset = str.Find(_T("%s"));
int index = 0;
while ((offset >= 0) && (index < arr.GetSize())) {
str = str.Left(offset) + arr[index++] + str.Mid(offset+2);
offset = str.Find(_T("%s"),(offset+2));
}
Software Zen: delete this;
|
|
|
|
|
Thanks Gary. That code looks exactly like what I need. And you are right the FormatV call isn't going to help me out.
Chris Meech
I am Canadian. [heard in a local bar]
Gently arching his fishing rod back he moves the tip forward in a gentle arch releasing the line.... kersplunk [Doug Goulden]
|
|
|
|
|
Looking back over the code I supplied, it has one slight bug: if the strings being substituted into the format include "%s" , the substitution will take place there, rather than in the original format. To fix it, use the following instead:
CString str,fmt;
CStringArray arr;
fmt = _T("%s blah %s blah ... %s blah");
str = fmt;
int offset = str.Find(_T("%s"));
int index = 0;
while ((offset >= 0) && (index < arr.GetSize())) {
str = str.Left(offset) + arr[index] + str.Mid(offset+2);
offset = str.Find(_T("%s"),(offset+arr[index].GetLength()));
index++;
} Sorry about that .
Software Zen: delete this;
|
|
|
|
|
Good point to mention, although I already have validation checking code prior to doing any of the substittions. The validation code ensures that the number of '%s's in the format string match the number of substition strings in the string array and that the substitution strings in the string array do not contain a '%s'. Thanks for the code and help. It's much appreciated.
Chris Meech
I am Canadian. [heard in a local bar]
Gently arching his fishing rod back he moves the tip forward in a gentle arch releasing the line.... kersplunk [Doug Goulden]
|
|
|
|
|
I am doing a document template project and I want to add a line indicator beside the edit area to show line number of each line. Does any body know how to do it? Thanks.
Maggie
|
|
|
|
|
|
|
Please, what´s wrong??
Why the variable "xyz[1]" don´t assigning value "1"???
#include "windows.h"
#include "stdio.h"
#include "iostream.h"
#include "string.h"
#include "stdlib.h"
char palav[8];
char xyz[6];
void main()
{
//(......)
cout << "\n\n\nWrite a word : ";
cin.getline (palav, 8, '\n');
switch (palav[0])
{
case '"A"':
xyz[1] = "1"
case '"B"':
xyz[1] = "2";
case '"C"':
xyz[1] = "3";
case '"D"':
xyz[1] = "4";
case '"E"':
xyz[1] = "5";
case '"F"':
xyz[1] = "6";
case '"G"':
xyz[1] = "7";
case '"H"':
xyz[1] = "8";
case '"I"':
xyz[1] = "9";
case '"J"':
xyz[1] = "0";
}
cout << "Caracter1 " << xyz[1] << endl;
//(.....)
}
erro:
error C2440: '=' : cannot convert from 'char [1]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast.
raf-sp@bol.com.br
|
|
|
|
|
xyz[1] = '1';
not
xyz[1] = "1";
|
|
|
|
|
This code should not have compile without errors.
CORRECTION:
switch (palav[0])
{
case 'A':
xyz[1] = '1'
break;
case 'B':
xyz[1] = '2';
break;
case 'C':
xyz[1] = '3';
case 'D':
break;
xyz[1] = '4';
case 'E':
break;
xyz[1] = '5';
break;
case 'F':
xyz[1] = '6';
break;
case 'G':
xyz[1] = '7';
break;
case 'H':
xyz[1] = '8';
break;
case 'I':
xyz[1] = '9';
break;
case 'J':
xyz[1] = '0';
break;
}
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
Thank´s!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1111
|
|
|
|
|
I have an app whs entering MTA instead STA by calling
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
Now on Windows XP OS all common openfile dialogs behave
'microsoft way'
Any FileOpen Dialog even the style is OFN_EXLORER, or not, or
even I am using extended structure to get the XP left crap I get
folowings.
Current browsing folder have only bmp files.
In FileType Combobox I have 2 file filters, eg. (*.bmp) and (*jpg)
Default selection is bmp. I see the bmp files in the listview.
I am changing the File Types: to jpg. The list view is empty.
Now I change back to bmp. The list box is still empty !!!. There is no way to repopulate the list view unless I type explicit the *.bmp
in the File Name Edit Control.
Also none of the rich edit controls work. They crash.
If I join a STA CoInitialize() everithing is OK but I need the MTA.
If you know any work around couse I searched at msdn and ...
Thank you.
|
|
|
|
|
RIch Edit is still screwed up at least I solve it with the dialog:
BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
BOOL ret = CFileDialog::OnNotify(wParam, lParam, pResult);
OFNOTIFY* pNotify = (OFNOTIFY*)lParam;
switch(pNotify->hdr.code)
{
case CDN_TYPECHANGE:
{
CWnd* pWnd = GetActiveWindow();
CWnd* PDlg = pWnd;
if(pWnd)
{
GetFocus()->PostMessage(WM_KEYDOWN,VK_RETURN,0);
GetFocus()->PostMessage(WM_KEYUP,VK_RETURN,0);
}
}
break;
}
return ret;
}
|
|
|
|