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;
}