Click here to Skip to main content
15,742,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a simple prompt on the command window, that takes any entered string and tokenized it, then displayed it back. If "exit" is typed the program exits.

The trouble I'm having is that within the command prompt, while the program runs, if I move the arrow keys at all I can edit printed text further up the command prompt, as well as anything before the program. If i then go to the bottom and enter some text the program messes up and displays some weird input.

My code is below, and any help would be appreciated. I have a feeling I need to close the stdin somehow.

#define INPUT_SIZE 513

#define FLUSH_STDIN(x) {if(x[strlen(x)-1]!='\n'){ do fgets(Junk,100,stdin); while(Junk[strlen(Junk)-1]!='\n');}else x[strlen(x)-1]='\0';}

char Junk[100]; // buffer for discarding excessive user input, 
               // used by "FLUSH_STDIN" macro
void getInput(char *user_input){

printf(">>: ");

void removeNewLine(char *str){
int len = strlen(str);
			if (str[len-1] == '\n')
				str[len-1] = 0;


int main(int argc, char *argv[]) {

char *user_input;
char *cur_token;
const char delim[2] = " ";
int session = 1;

while (session != 0)
	int len = strlen(user_input);
	if (strcmp(user_input,"exit") ==0)
		session = 0;
		printf("Entered string split into: ");
		cur_token = strtok(user_input, delim);
		while(cur_token != NULL){
			printf("\"%s\"  ",cur_token);
			cur_token = strtok(NULL, delim);
		if (!(len < (INPUT_SIZE-1)))

Updated 11-Feb-14 23:53pm
Richard MacCutchan 12-Feb-14 7:00am    
I don't see how this could work since you have not initialised the user_input pointer. You will be reading into random areas and corrupting everything.
W Balboos, GHB 12-Feb-14 13:19pm    
Strange Code:
if (strcmp(user_input,"exit") ==0)
session = 0;
Why not simply use "break;" instead of continuing the loop?
Member 10034451 12-Feb-14 13:59pm    
Thanks for those tips, I'll be sure to use them! The code works as expected without initialising the user_input?

Have you any help with my original question? As to why in the command prompt, when I use the arrow keys I can edit previously printed text, which is then used as input in the stdin? Thanks.
Richard MacCutchan 13-Feb-14 3:57am    
The code works as expected without initialising the user_input?
I find that very difficult to believe, particularly when you have already said that it does not work properly.

1 solution

You have some considered responses from experienced people.

If you read this[^] you may understand why the comments were made.

At the moment in your code
points into space. You need to allocate some memory to it before you use it with
The fact that you get something to happen at the moment is just coincidence. That bug needs to be fixed first.

You haven't said which compiler and OS you are using which will affect your results.

A few comments explaining what your code is meant to do would also be a good idea.

I would suggest these changes:

char user_input[INPUT_SIZE ];
char *cur_token=NULL;

Change this function:
char * getInput(char *user_input){

printf(">>: ");
return fgets(user_input,INPUT_SIZE,stdin);

And add this:

if(NULL ==getInput(user_input))
    printf("Input error occured.\n");

At the moment FLUSH_STDIN can never be called (even before you make any changes):

if (!(len < (INPUT_SIZE-1)))

Why have you used a macro for FLUSH_STDIN(x) and not a function?[^]

Because your application is running in a console you have access to command line history. The up and down arrows are used to view this. This behaviour can be modified:[^][^]

In Windows 7 if you press F7 you will see the entire history.[^]

alt-F7 will clear the history. You may also change the default settings manually. Click the console's control box (top left), properties, options tab: "Command history." The default is 50 items, 4 buffers.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900