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.

C++
#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(">>: ");
fgets(user_input,INPUT_SIZE,stdin);
}

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)
{
	getInput(user_input);
	removeNewLine(user_input);
	
	int len = strlen(user_input);
	if (strcmp(user_input,"exit") ==0)
		session = 0;
	else{
		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)))
			FLUSH_STDIN(user_input);
		
		printf("\n");
	}
} 

return(0);
}
Posted
Updated 11-Feb-14 23:53pm
v2
Comments
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 http://www.cplusplus.com/reference/cstdio/fgets/[^] you may understand why the comments were made.

At the moment in your code
C++
user_input
points into space. You need to allocate some memory to it before you use it with
C++
fgets()
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:

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


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

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


And add this:

C#
if(NULL ==getInput(user_input))
    {
    printf("Input error occured.\n");
    break;
}



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

C++
if (!(len < (INPUT_SIZE-1)))
    FLUSH_STDIN(user_input);


Why have you used a macro for FLUSH_STDIN(x) and not a function? http://stackoverflow.com/questions/14041453/why-are-preprocessor-macros-evil-and-what-are-the-alternatives[^]

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:
http://msdn.microsoft.com/en-us/library/ms686031%28v=VS.85%29.aspx[^]
http://msdn.microsoft.com/en-us/library/ms682077%28v=VS.85%29.aspx[^]

In Windows 7 if you press F7 you will see the entire history.
http://en.kioskea.net/faq/14204-windows-7-view-the-command-prompt-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
 
v11

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