C++ String Entering Customization

Aug 24, 2011 at 4:04pm
Hello everyone :D,

I am making a console application in Dev C++ and I was wondering if the following can be done: when entering a string is it possible to end it with a "." ,for example, instead of pressing enter or space to tell the application that you are done entering the string?

Thanks,

Muhasaresa
Aug 24, 2011 at 4:09pm
Yes but you need to read any single char...try getc() from stdio.h or something...you loop until your just read character is '.' or whatever. Dont forget to add each character to your buffer and finally a '\0' at the end of the string!

ZED
Aug 24, 2011 at 4:13pm
Hey they made even a little example just for you!

http://cplusplus.com/reference/clibrary/cstdio/getchar/
Last edited on Aug 24, 2011 at 4:13pm
Aug 24, 2011 at 4:18pm
Not with the standard lib. The standard lib buffers all input and only forwards it to your program after the user hits enter.

Reading one char at a time like ZED suggests would work with another lib that gives real-time status of keyboard input. If you're on Windows you can check WinAPI for some functions. I don't know any offhand, but try looking around for GetConsoleInput or something similar (note: that's not a real function, that's just what I'd start searching for).

Alternatively you can use a 3rd party lib like ncurses which probably has this functionality.

Though, really, this is probably more trouble than it's worth.
Aug 24, 2011 at 4:22pm
Yeah, just below the example...I should learn reading once more...

"A simple typewriter. Every sentence is echoed once ENTER has been pressed until a dot (.) is included in the text."
Aug 24, 2011 at 4:26pm
Would getc() work however if the string was pasted from the clipboard into command prompt?

So, for example, I paste in "ace.boo.can." all at once and the application interprets it as "ace (enter) boo (enter) can (enter)
Last edited on Aug 24, 2011 at 4:33pm
Aug 24, 2011 at 5:05pm
Would getc() work however if the string was pasted from the clipboard into command prompt?


No.
Last edited on Aug 24, 2011 at 5:06pm
Aug 25, 2011 at 8:29am
So how would I make the application treat "." as enter like in my "ace.boo.can." to "ace (enter) boo (enter) can (enter) example?

Muhasaresa
Aug 25, 2011 at 8:43am
msvc++9.0:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdio>
#include <conio.h>


int main(void)
{
	int ch, index = 0;
	char str[1024] = {'\0'};

	for( ; index < (sizeof str - 1) &&
		(ch = _getche()) != '.'; str[index++] = ch);

	printf("\nLength:   %d\nString:   \"%s\"\n", index, str);

	return 0;
}
Topic archived. No new replies allowed.