Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

input history?

Posted on 2001-06-08
11
Medium Priority
?
255 Views
Last Modified: 2010-04-15
I've done a program which output strings from the user using the gets() function. Now, as far as I see, with F3 (under DOS) I can print the last entered string, and with the arrows I can edit the string.

Now, I wish to replace the function with something else, which offer me history options, with the up and down arrows, like in doskey, for example, with the minimum changes to the whole program, if any.

Well, I guess TC/BC doesn't has it, so I wish anyone over here to help. Thanks.
0
Comment
Question by:tomercohen
  • 6
  • 3
  • 2
11 Comments
 
LVL 5

Expert Comment

by:djbusychild
ID: 6172275
you'll first need to store however many commands the user enters (you may need to limit this to say 10, etc) then
you can detect the up/down arrow key to traverse that list and output that.

did you want a sample code?
0
 

Author Comment

by:tomercohen
ID: 6173903
Sure I want to. That's emergency. ;-)
0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6174123
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Accepted Solution

by:
djbusychild earned 800 total points
ID: 6174125
#include <stdlib.h>
#include <stdio.h>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <conio.h>

#define MAX_HISTORY 10
#define MAX_COMMAND_LEN 256

typedef char bool;
#define false 0
#define true 1

int echoCommand(int direction, char* history[],int last_cmd,int num_cmd) {
     if (direction==72) { /* up */
          last_cmd--;
          if (last_cmd<0) { last_cmd=num_cmd-1; }
     } else if (direction==80) { /* down */
          last_cmd++;
          if (last_cmd>(num_cmd-1)) { last_cmd=0; }
     } else { return -1; }

     return last_cmd;
}

int main(int argc,char* argv[])
{
     char *history[MAX_HISTORY]; /* I'm being lazy, you should use a circular queue or something */
     char command[MAX_COMMAND_LEN];
     int last_cmd_index=0;
     int char_buf;
     int num_char=0;
     int num_cmd=0;

     bool command_entered=false;

     memset(command,0,MAX_COMMAND_LEN);
     memset(history,0,MAX_HISTORY*sizeof(char*));

     printf(">> "); /* print cursor */
     while (!_kbhit()) {
               char_buf=_getch();
               switch (char_buf)
               {
                    case 0: /* this is the function key */
                         /* as of now it'll just quite after a function key */
                         
                         break;
                    case 224: /* possibly up,down */
                         last_cmd_index=echoCommand(_getch(),history,last_cmd_index,num_cmd);
                         if (last_cmd_index>=0) {
                              /* wipe out the command line */
                              while (num_char>0) {
                                   printf("\b \b");
                                   num_char--;
                              }
                              num_char=strlen(history[last_cmd_index]);
                              strcpy(command,history[last_cmd_index]);
                              printf("%s",command);
                         } else { last_cmd_index=0; }
                         break;
                    case 8: /* backspace */
                         if (num_char>0) {
                              command[--num_char]='\0';
                              printf("\b \b"); /* back delete back again */
                         }
                         break;
                    case 13: /* newline */
                         if (num_char>0) {
                              num_char=0;
                              if (num_cmd>=MAX_HISTORY) {
                                   num_cmd=0;
                              }
                              if (history[num_cmd]) { /* if there's a command there already, we have to free it */
                                   free(history[num_cmd]);
                                   history[num_cmd]=NULL;
                              }
     
                              history[num_cmd]=(char*)malloc(strlen(command)+1);
                              strcpy(history[num_cmd],command); /* assuming buffer check has already been done by command it self */
                              num_cmd++;
                         }
                         printf("\n>> "); /* print a new cursor */
                         break;
                    default: /* just another key */
                         if ( num_char<(MAX_COMMAND_LEN-1) ) {
                              command[num_char]=char_buf;
                              command[++num_char]='\0'; /* NULL terminate it */
                              printf("%s",&command[num_char-1]); /* just print out the last input */
                         }
                         break;
               }
     }

     printf("\n"); /* end with a carriage return */
}
0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6174131
this is a rought, it's not really using a circular queue but just a simple array, so it's not truly circular. I haven't dealt with the situation where you go beyond the history limit and you have to push the others away... Instead I just start overwriting from index 0.

you get the idea, though.
0
 
LVL 1

Expert Comment

by:yabelson
ID: 6198850
as a default,the DOS also has up to 10(if i am not mistaken) commands back.if you press up further,it simply returns you to the last command.this means that if you recieve more hten 10 "up"s you reset your counter,and start all over again.
0
 
LVL 1

Expert Comment

by:yabelson
ID: 6198856
tomer-(fonetic hebrew)tikanes laatar shel rotter,ulai yuchlu laazor lecha sham.yesh sham harbe anashiim shemevinim besi.
0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6198871
Hi, did this help at all?
0
 

Author Comment

by:tomercohen
ID: 6200009
Yabelson, I guess you wrong. DOS has no built-in history options at all, except the F3 key, for the last command. This let me use gets() with only one line of history. In other operating system, this is not the same.

Oh, (in fonetic hebrew) - Ani lo ohev et haatar shel rotter. En sham anashim nechmadim. ;-)
0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6200011
tomercohen, did the code help?
0
 

Author Comment

by:tomercohen
ID: 6203334
djbusychild, not at all, I made it with another way.
Thanks anyway. ;-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question