Hi,
I want the user to be able to enter a command then a character, like for example:
push r
I want the command to be stored in the array command, and the character to be stored in the variable c.
Now I wonder what the best way to get rid of the space is, using scanf or getchar (see below for code, only thing that I changed between the 2 versions is the statement before the comment "get rid of space")? Or maybe it doesnt matter?
Code:
include <stdio.h>
#define MAX 200
void push(char c); // Puts a new element last in queue
char pop(void); // Gets the first element in queue
static char s[MAX];
int main(void)
{
char c;
char command[10];
printf("Enter push character to put a new element in in queue. Enter pop to retrieve firs element in queue:\n");
scanf("%s", command);
scanf("%c", &c); // Get rid of space
scanf("%c", &c);
printf("command: %s\n", command);
printf("Element: %c\n", c);
return 0;
}
Code:
include <stdio.h>
#define MAX 200
void push(char c); // Puts a new element last in queue
char pop(void); // Gets the first element in queue
static char s[MAX];
int main(void)
{
char c;
char command[10];
printf("Enter push character to put a new element in in queue. Enter pop to retrieve firs element in queue:\n");
scanf("%s", command);
getchar(); // Get rid of space
scanf("%c", &c);
printf("command: %s\n", command);
printf("Element: %c\n", c);
return 0;
}
Thanks if you want to help.