Interesting project for us all

Interesting project for us all

Post by AppAttacker on Mon Nov 23, 2015 7:36 pm
([msg=90720]see Interesting project for us all[/msg])

So as my last forum post stated I was working on a program that reversed what I typed and I finished it finally (still a noob here). Well something interesting happened after I finished it.
Of course when C takes in user input it can't understand the arrow keys without actually putting in the code to do so. Well I was experimenting with what would happen if I submitted arrow keys to the program and as you probably all know it shows up as ^[[A , ^[[B, ^[[C, or ^[[D. But the interesting thing is that the ^[[D and only the D will cause the output to produce a next line. I'm assuming equivalent to \n but as well as that I'm not sure if this is connected or not but the first two characters the ^[ will delete together but the [D part each delete separately. Would anyone like to work out why this happens with me? Not sure it will benefit us any but it would be fun. I'll post the code I am using so you can see for yourself.

Code: Select all
#include <stdio.h>
#include <string.h>

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

  char command[100];
  char *quit = "quit\n";
 
  printf("type 'quit' at any time to quit. \n");
  printf("please enter string: ");
  fgets(command, 100, stdin);
 
 
 
  while(1){
    int ha = strlen(command) + 1;
    int ret = strcmp(command, quit);
   
    if(ret != 0) {
      for(int i = 0; i < ha; i++) {;
   int str = strlen(command) - i;
   putchar(command[str]);
      }
    }
    else {
     exit(0);
    }
    printf("\n");
    printf("please enter string: ");
    fgets(command, 100, stdin);
  }
return 0;
}


If you know the answer as to why it send us a next line please comment and give me a clue where to look for the answer. THANKS
I will be quiet, and I shall hear ALL
AppAttacker
New User
New User
 
Posts: 6
Joined: Sat Nov 21, 2015 2:42 pm
Blog: View Blog (0)


Re: Interesting project for us all

Post by -Ninjex- on Mon Nov 23, 2015 8:56 pm
([msg=90721]see Re: Interesting project for us all[/msg])

Read this:
http://pubs.opengroup.org/onlinepubs/00 ... g_11_01_09
(Cntrl+D == EOF)
Try sending a cntrl+M :)
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Interesting project for us all

Post by AppAttacker on Mon Nov 23, 2015 9:47 pm
([msg=90722]see Re: Interesting project for us all[/msg])

Thanks ninjex! That's an interesting read I'm going to try and read it all! Thanks for the direction
I will be quiet, and I shall hear ALL
AppAttacker
New User
New User
 
Posts: 6
Joined: Sat Nov 21, 2015 2:42 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests