Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Trying to make a program that where the user inputs any characters and numbers f

ID: 3755326 • Letter: T

Question

Trying to make a program that where the user inputs any characters and numbers from the keyboard, when the sequence 01111110 is typed in the program is supposed to display a HDLC flag. The program works so far, but when some of the keys are entered the program displays key entries twice. If I push k for example the terminal will show kk. I have tried this on different platforms and getting the same results. Below is what im working on so far.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>

int main() {
  
char ch; // char
char HDLCflag[] = "01111110"; // key
char chacker[8]; // input key container
char data[256]; // data container
int i = 0, c = 0, d = 0;
memset(data, 0, 256);
  
//
printf("Press Any Key to Continue and it will be read automaticly ");
printf("for this expirment to work only enter 0's and 1's and 'e' exit Thank You ");
  
system ("/bin/stty raw"); // this sets the key inputs to raw
while (1) {
  
ch = getchar();
printf("%c",ch);// prints what you are typing
  
// receving char when in container
if (c == 1) {
data[d] = ch;
d++;
}
  
//move over key checker string char
for (i = 7; i >= 0; i--) {
if (i == 0) {
chacker[i] = ch;
break;
} else {
chacker[i] = chacker[i-1];
}
}
  
//checks for flags
if (chacker[0] == HDLCflag[0] &&
chacker[1] == HDLCflag[1] &&
chacker[2] == HDLCflag[2] &&
chacker[3] == HDLCflag[3] &&
chacker[4] == HDLCflag[4] &&
chacker[5] == HDLCflag[5] &&
chacker[6] == HDLCflag[6] &&
chacker[7] == HDLCflag[7]) {
  
  
if (c == 1) {
c--;
system ("/bin/stty cooked"); // this sets the key inputs to processed
printf(" (End flag has been detected) HDLC FLAG ");
data[d-8] = '';
printf(" data : %s ",data);
printf(" (continuing to receive) ");
system ("/bin/stty raw"); // this sets the key inputs to processed
}else {
system ("/bin/stty cooked"); // this sets the key inputs to processed
printf(" (continuing to receive) HDLC FLAG ");
system ("/bin/stty raw"); // this sets the key inputs to processed
c++;
}
  
}
  
// exits on 'e' only when not receving
if (ch == 'e' &&
c == 0) {
system ("/bin/stty cooked"); // this sets the key inputs to processed
printf(" ");
system ("/bin/stty raw"); // this sets the key inputs to processed
break;
}
  
}
system ("/bin/stty cooked"); // this sets the key inputs to processed
return 0;
} // end main

Explanation / Answer

The above code will only work if the input given is always whatever you are expecting (i.e 01111110) since you are using while(1) which is always true and there is no condition in the code which breaks the loop when you give any different values hence it falls in infinite loop, I think you have missed to add else part after

if (ch == 'e' &&
c == 0) {
system ("/bin/stty cooked"); // this sets the key inputs to processed
printf(" ");
system ("/bin/stty raw"); // this sets the key inputs to processed
break;
}

which check's whether for any other character other then 1, 0 and e which will be as below

if (ch == 'e' &&
c == 0) {
system ("/bin/stty cooked"); // this sets the key inputs to processed
printf(" ");
system ("/bin/stty raw"); // this sets the key inputs to processed
break;
}
else if((ch != 0 || ch != 1 || ch != 0){
break;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote