Other options for use of istream::get() are:
char inputChar = '\0';
cin.get(inputChar);
or
std::string myString = "";
int charCount = 24;
cin.get(myString, charCount);
See this for more prototypes and examples.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
In addition you have:
(IOSTREAM HEADER)
getline( istream& is, string& str )
string mystring;
getline(cin, mystring);
(STDIO HEADER) scanf( const char * format, ... )
string mystring;
scanf("%s", mystring);
(WINDOWS INPUT)
//....
LRESULT CALLBACK WindowProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY: break;
case WM_ACTIVATEAPP: break;
case WM_KEYDOWN:
{
int virtual_key = (int)wParam;
int key_bits = (int)lParam;
switch(virtual_key)
{
case VK_ESCAPE:break;
case VK_RETURN:break;
//case VK_....
}
}
break;
}
return DefWindowProc (hWnd, Message, wParam, lParam);
}
(DIRECTX)
void Input()
{
static BYTE Keys[256];
GetKeys(&Keys[0]);
ZeroMemory(&KeyboardInput, sizeof(KeyboardInput));
for( int i = 0; i <= 256; i++){
KeyboardInput.Keys[i] = Keys[i];
}
if(Keys[DIK_ESCAPE]) //....
if(Keys[DIK_F]) //....
if(Keys[DIK_O]) //....
if(Keys[DIK_O]) //....
}
ShadowScripter
Junior Poster in Training
94 posts since Apr 2009
Reputation Points: 12
Solved Threads: 6
(STDIO HEADER)
scanf( const char * format, ... )
string mystring;
scanf("%s", mystring);
Not a valid solution -- unless you want the ability to crash at will. See this . And since when does scanf() work on C++ Strings?
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Not a valid solution -- unless you want the ability to crash at will. See this . And since when does scanf() work on C++ Strings?
Whopsie, I was copy - pasting at this point. However, I did turn to a reference before giving the example, where the given reference actually corrects my mistake as well, but thanks for pointing it out.
I agree with you, it's a bad function. It does more work, alot more than is needed. It's also quite confusing at first, if you don't know what commands to put in it.
With that being said, it still does its job. For a beginner, that's really all he needs to know.
As for the apparent buffer overflow, there is always a risk that some memory will be overwritten or will segfault because of bad programming; human nature. Just look at mine :$
ShadowScripter
Junior Poster in Training
94 posts since Apr 2009
Reputation Points: 12
Solved Threads: 6