Thread: Little Help Needed

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    9

    Little Help Needed

    The code is running well but I have two issues.
    Firstly, from the 3rd printf command, when I input a character, it jumps to the next instruction without me clicking enter.

    Secondly, I'm trying to store the string elements in a new char array and print the new array but I'm having complications.

    Code:
    #include <stdio.h>#include <conio.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    int main (void)
    {
    	int N;
    	printf("insert the length of string\n");
    	scanf("%d", &N);
    
    
    	char v, u, str[N];
    
    
    	printf("insert the string element\n");
    	scanf("%s", str);
    
    
    	printf("insert a character from the element\n");
    	v = _getch();
    	printf("\n%c", v);
    
    
        printf("\n");
    
    
    	printf("insert a character which you want to interchange with\n");
    	u = _getch();
    	printf("\n%c", u);
    
    
    	printf("\n");
    
    
        int i;
    	for (i=0;str[i]!='\0'; i++){
            if (str[i] == v){
                str[i] = u;
            }
            else
              continue;
    
    
    	}
    
    
    	printf("the values are:\n");
    	puts(str);
    
    
    	return 0;
    
    
    
    
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,455
    Firstly, from the 3rd printf command, when I input a character, it jumps to the next instruction without me clicking enter.
    Okay, do you realize that that is what getch() should do?

    Also do you know that getch() is a non-standard function only available on certain compilers?

    Perhaps you should consider either getchar(), or scanf() using the " %c" format specifier (note the leading space).

  3. #3
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    31
    Hello Chibisco!

    the next input with _getch() takes the character from the keyboard-buffer of the last input.
    Co can delete this buffer with a simple function:
    Code:
    void clearKeyboardBuffer(void)
     {
     while (getc(stdin) != '\n')
        ;
     }
    For other people which have no _getch() here is a example:
    Code:
     // for LINUX Systems
    #include <termios.h>
    #include <unistd.h>
    #include <fcntl.h> //for new version of kbhit
    //#include <sys/time.h> old version
    
    /// reads from keypress, doesn't echo
    int _getch(){
        struct termios oldt, newt;
        int ch;
        tcgetattr( STDIN_FILENO, &oldt );
        newt = oldt;
        newt.c_lflag &= ~( ICANON | ECHO );
        tcsetattr( STDIN_FILENO, TCSANOW, &newt );
        ch = getchar();
        tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
        return ch;
    } 
    // End of Defintion for LINUX
    You can use this examples in your code like this:
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
     
     // for LINUX Systems
    #include <termios.h>
    #include <unistd.h>
    #include <fcntl.h> //for new version of kbhit
    //#include <sys/time.h> old version
    
    /// reads from keypress, doesn't echo
    int _getch(){
        struct termios oldt, newt;
        int ch;
        tcgetattr( STDIN_FILENO, &oldt );
        newt = oldt;
        newt.c_lflag &= ~( ICANON | ECHO );
        tcsetattr( STDIN_FILENO, TCSANOW, &newt );
        ch = getchar();
        tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
        return ch;
    } 
     
    // End of Defintion for LINUX 
    
    void clearKeyboardBuffer(void)
     {
     while (getc(stdin) != '\n')
        ;
     } 
     
     
     
    int main (void)
    {
        int N;
        printf("insert the length of string\n");
        scanf("%d", &N);
     
     
        char v, u, str[N];
     
     
        printf("insert the string element\n");
        scanf("%s", str);
     
        clearKeyboardBuffer();
      
        printf("insert a character from the element\n");
        v = _getch();
        printf("\n%c", v);
      
        printf("\n");
     
     
        printf("insert a character which you want to interchange with\n");
        u = _getch();
        printf("\n%c", u);
     
     
        printf("\n");
     
     
          for (int i = 0; str[i] != '\0';  i++){
            if (str[i] == v){
                str[i] = u;
            }
            else
              continue;
     
     
        }
     
     
        printf("the values are:\n");
        puts(str);
     
     
        return 0;
     
    }

  4. #4
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    31
    By the way i think it could be a reason to declare the length of the array str[] by 'N':
    the string what you read in in line 17 of your example is not limited.
    That could be a reason.
    Now my second example of your code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
     // for LINUX Systems
    #include <termios.h>
    #include <unistd.h>
    #include <fcntl.h> 
    
    /// reads from keypress, doesn't echo
    int _getch(){
        struct termios oldt, newt;
        int ch;
        tcgetattr( STDIN_FILENO, &oldt );
        newt = oldt;
        newt.c_lflag &= ~( ICANON | ECHO );
        tcsetattr( STDIN_FILENO, TCSANOW, &newt );
        ch = getchar();
        tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
        return ch;
    } 
     
    // End of Defintion for LINUX 
    
    void clearKeyboardBuffer(void)
     {
     while (getc(stdin) != '\n')
        ;
     } 
     
      
    int main (void)
    {
        //int N;
        //printf("insert the length of string\n");
        //scanf("%d", &N);
        //char v, u, str[N];
        //printf("insert the string element\n");
        //scanf("%s", str);
        
        char v, u, str[200];
        
        printf("insert the string element\n");
        /* scanf("%199s", str);  // limits the length to 199 elements*/
        scanf("%199[^\n]", str); // reads every character including space but not '\n' (=Enter)
     
        clearKeyboardBuffer();
      
        printf("insert a character from the element\n");
        v = _getch();
        printf("\n%c\n", v);
      
        printf("insert a character which you want to interchange with\n");
        u = _getch();
        printf("\n%c\n", u);
     
      
         for (int i = 0; str[i] != '\0';  i++){
            if (str[i] == v){
                str[i] = u;
            }
            else
              continue;
      
        }
     
        printf("the values are:\n%s\n", str);
        puts(str);
     
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed please.
    By caz0026 in forum C Programming
    Replies: 6
    Last Post: 04-26-2012, 06:33 AM
  2. Help Needed
    By Fatima Rizwan in forum C++ Programming
    Replies: 11
    Last Post: 05-02-2009, 02:22 AM
  3. help needed
    By jack_carver in forum C Programming
    Replies: 3
    Last Post: 03-19-2009, 11:15 PM
  4. Help Needed
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 06-14-2002, 01:38 PM

Tags for this Thread