Write a program in C that accepts the following inputs : w, a, s, d, or q. If th
ID: 3836557 • Letter: W
Question
Write a program in C that accepts the following inputs : w, a, s, d, or q.
If the user types w, the program instantly prints UP.
If the user types a, the program instantly prints LEFT.
If the user types s, the program instantly prints RIGHT.
if the user types d, the program instantly prints DOWN.
The user should be able to repeatedly enter these characters for as long as the program runs, printing UP, LEFT, RIGHT, DOWN as required. If the user hits q, the program ends.
Nothing other than UP, LEFT, RIGHT, or DOWN is displayed.
The program should restore any terminal settings it changes before terminating.
********** NOTE (PLEASE READ BEFORE ANSWERING): **********
The user types w, a, s, d, or q. They do not type w then enter. You should only need to press w and then the word UP should instantly appear on the screen (you should NOT see "w" anywhere on the screen, only the word "UP" in its place).
Explanation / Answer
#include<stdio.h>
int main(void){
int c;
system ("/bin/stty raw");
while((c=getchar())!= 'q') {
if(c == 'w') printf("UP");
else if(c == 'a') printf("LEFT");
else if(c == 's') printf("RIGHT");
else if(c == 'd') printf("DOWN");
}
system ("/bin/stty cooked");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.