Reputation: 11
I am currently writing my first program in C after being exposed to Java, and the purpose of this program is to simulate a stack that holds 10 integers. The user can ask to push ('u'), pop('o'), exit('x'), or change the format of output. There is a bug with how I am doing the outputs, but I can handle that later. The main cause for concern is that I get this output when running my program:
Welcome to the stack program.
Enter option: u
What number? 1
Stack: 1
Enter option: Invalid character.
Enter option: u
What number? 2
Stack: 1 2
Enter option: Invalid character.
Enter option: u
What number? 3
Stack: 1 2 3
Enter option: Invalid character.
Enter option:
As you can see, the program allows me to push items onto the stack and stores them (pop also works), but every time the user would be prompted to enter a new option the invalid character case is reached in my switch statement and creates an excess line in error. I understand more program context may be necessary, but is there anything glaringly wrong with my switch statement?
#include <stdio.h>
#include <stdlib.h>
char currentOption;
int *printMode = 0;
//A program to simulate a stack data type of integers.
int main()
{
printf("Welcome to the stack program.\n");
printf("\nEnter option: ");
scanf ("%c", ¤tOption);
while(currentOption != 'x')
{
processOption(currentOption);
printf("\nEnter option: ");
scanf ("%c", ¤tOption);
}
return 0;
}
//interpret the user input character as one of several options
void processOption(char option)
{
int storedValue;
switch(option)
{
case 'u' : //push to stack
printf("What number? ");
scanf ("%d", &storedValue);
if(push(storedValue) == 1)
{
printf("Overflow!!!");
}
else
{
printf("Stack: ");
printStack(printMode);
}
break;
case 'o' : //pop, return popped value
pop(&storedValue);
if(storedValue == NULL)
{
printf("Underflow!!!");
}
else
{
printf("Popped %d", storedValue);
printf("\nStack: ");
printStack(printMode);
}
break;
case 'd' : //change print mode to decimal and print
printf("\nStack: ");
*printMode = 0;
printStack(printMode);
break;
case 'h' : //change print mode to hex and print
printf("\nStack: ");
*printMode = 1;
printStack(printMode);
break;
case 'c' : //change print mode to char and print
printf("\nStack: ");
*printMode = 2;
printStack(printMode);
break;
case 'x' : //change print mode to char and print
printf("Goodbye!");
exit(EXIT_SUCCESS);
default :
printf("Invalid character." );
break;
}
}
Thank you in advance for your time!
Upvotes: 1
Views: 1627
Reputation: 11
Yes, even in JAVA scanner, if you scan for a numeric type like int, double, it reads until it hits an optional sign, then continutes until it hits a digit, then continues with digits, and for float/double one radix point, until something else is read, which it then puts back in the buffer (there is always room for 1, see man ungetc() ). So the line feed is in the buffer waiting for the next scan input. It's a classic case of the computer doing what you ask. You can put a literal linefeed '\n' in the format to make scanf eat it, or add a dummy read of some sort, or read a line into a string/String/char[] and scan that with sscanf()/Scanner/>> !
Upvotes: 0
Reputation: 5351
In main()
change :
scanf ("%c", ¤tOption);
to
scanf (" %c", ¤tOption);
The reason being the stray newline entered after the first input is immediately consumed by scanf()
following it.
To fix this use a blank space before the conversion specifier.
The blank space prefixed to %c
tells scanf()
to skip the first stray characters and consume only the character entered after that.
Upvotes: 4
Reputation: 106
I don't think anything is wrong with your switch statement, but rather where you are acquiring
char option
It looks to me like you're doing something like
scanf("%c", &option);
processOption(option);
in a loop. When processOption() returns, scanf immediately picks up the newline that gets put into the stream when you press enter. \n, is, of course, not one of your valid options. So, change your scanf call to something like
scanf(" %c", &option);
This causes scanf to skip all leading whitespace, including newlines. This isn't required for scanf("%d", int) because whitespace is obviously not valid for a numeric value but might be valid for a char.
Upvotes: 2
Reputation: 16540
the main problem is:
The user enters a character by first typing the character, then hitting the 'return' key.
That key (newline) is still in the buffer at the top of the next loop so is what is read.
That newline is part of the 'white space' definition.
To consume the white space, write the scanf like so:
scanf (" %c", ¤tOption);
Notice the leading space in the format string? A space in a format string causes white space to be consumed at that point in the input.
Also, the returned value from scanf(), and family of functions, should always be checked to assure the input/conversion operation was successful.
Upvotes: 3