How to read strings from console apps?

Started by
6 comments, last by The_Minister 23 years, 10 months ago
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
[email=mwronen@mweb.co.za" onmouseOver="window.status='Mail The_Minister'; return true" onmouseOut="window.status=' '; return true]The_Minister[/email]1C3-D3M0N Interactive
Advertisement
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."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
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.
For a good time hit Alt-F4! Go ahead try it, all the cool people are doing it.
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;
}

I have already tried cin.getline().
The code just proceeds, and the variable it was supposed to write to is empty.

The_Minister
1C3-D3M0N Interactive
[email=mwronen@mweb.co.za" onmouseOver="window.status='Mail The_Minister'; return true" onmouseOut="window.status=' '; return true]The_Minister[/email]1C3-D3M0N Interactive
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
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
uhhhhhhhhhh.....how about
        #include <iostream.h>#define MAX_SIZE   200void main(){   char a[MAX_SIZE];   cin.getline(a, MAX_SIZE, '\n');}        





Edited by - ncsu121978 on June 20, 2000 5:37:37 AM
gets works fine, thanks. I am not really interested how safe it is .

As I stated before, I tried getline and the program just continued running without displaying a prompt.

The_Minister
1C3-D3M0N Interactive
[email=mwronen@mweb.co.za" onmouseOver="window.status='Mail The_Minister'; return true" onmouseOut="window.status=' '; return true]The_Minister[/email]1C3-D3M0N Interactive

This topic is closed to new replies.

Advertisement