Archived

This topic is now archived and is closed to further replies.

How to read strings from console apps?

This topic is 6389 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Yet Another Newbie Question: Sorry dudes, but this isn''t covered in any of my books . After numerous attempts, and numerous interesting results, I am no closer to the light. How does one read a string, with spaces, from the console in a console app? All I know officially is that you use cin >> to read in a string. Works great with single words, but anything else and the entire application devours itself. I know there is a way to do this, I just don''t know how. All help is appreciated, as always. The_Minister 1C3-D3M0N Interactive

Share this post


Link to post
Share on other sites
If I wanted to read in a string I would just getchar() until i found ''\n'' or somesuch I guess.

while (buffer[i++] = (getchar()) != ''\n'');

I think that would work.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

Share this post


Link to post
Share on other sites
Close, but backspaces, and the alternative enter key doesn''t work that way. getline works as long as the user doesn''t type too much.

For a good time hit Alt-F4! Go ahead try it, all the cool people are doing it.

Share this post


Link to post
Share on other sites
some console code from my engine (using DirectInput, PressedKey is the number of the key in DInput). Have fun!

INT ManageConsole(int PressedKey, s_player &Player, Game_Msg &GMsg)
{
if((timeGetTime()-GMsg.ConsoleLetterTime>100) // (PressedKey!=GMsg.OldKey))
{
GMsg.OldKey=PressedKey;
GMsg.ConsoleLetterTime=timeGetTime();
switch(PressedKey)
{
case 88: return E_FAIL; break;
case 1: return E_FAIL; break;
case 15: {strcpy(GMsg.ConsoleText, " ");
GMsg.ConsoleCounter=0; Player.ConsoleOn=false;} break;
case 28: ConsoleMessage(GMsg, Player); GMsg.ConsoleCounter=0; break;
}//alter the following lines to specify which keys are valid
if((PressedKey>15 && PressedKey<28) // (PressedKey>29 && PressedKey<41) //
(PressedKey>43 && PressedKey<54) // (PressedKey==57) //
(PressedKey>1 && PressedKey<12))
{
GMsg.ConsoleText[GMsg.ConsoleCounter]=LetterConvert(PressedKey);
if(GMsg.ConsoleCounter<255)GMsg.ConsoleCounter++;
}
if(PressedKey==14)
{
GMsg.ConsoleText[GMsg.ConsoleCounter]='' '';
if(GMsg.ConsoleCounter>0)GMsg.ConsoleCounter--;
}
}

return 0;
}

Share this post


Link to post
Share on other sites
quote:
Original post by The_Minister

Yet Another Newbie Question:

Sorry dudes, but this isn't covered in any of my books . After numerous attempts, and numerous interesting results, I am no closer to the light.

How does one read a string, with spaces, from the console in a console app?

All I know officially is that you use cin >> to read in a string. Works great with single words, but anything else and the entire application devours itself.

I know there is a way to do this, I just don't know how. All help is appreciated, as always.

The_Minister
1C3-D3M0N Interactive


This function will read all characters until you press ENTER:

#include <stdio.h>

char *gets_safe(char *s, int maxlength)
{
fgets(s, maxlength, stdin);
return s;
}

Then simply call the function like this:

int main(void)
{
char string[100];

// Only take 99 characters.
gets_safe(string, 99);

return 0;
}

You could just use gets() instead of gets_safe(), but I like my own function more since it's a lot safter than gets() (no buffer overflows, or whatever it's called ).

/. Muzzafarath
Mad House Software

Edited by - Muzzafarath on June 18, 2000 3:19:48 PM

Share this post


Link to post
Share on other sites