That is a stack program - I tried to change from integers to characters so when you enter a single char of a-z. I ran the program then it displayed shown below:

Enter a number: <EOF> to stop: a
Enter a number: <EOF> to stop: Enter a number: <EOF> to stop: b
Enter a number: <EOF> to stop: Enter a number: <EOF> to stop: c
Enter a number: <EOF> to stop: Enter a number: <EOF> to stop: d
Enter a number: <EOF> to stop: Enter a number: <EOF> to stop: e
Enter a number: <EOF> to stop: Enter a number: <EOF> to stop: ^Z


The list of numbers reversed:


e


d


c


b


a
Press any key to continue . . .


Why did it appear the output twice "Enter a number: <EOF> to stop: Enter a number: <EOF> to stop:"?

The code is below:

#include <stdio.h>
#include <stdbool.h>
#include "stacksADT.h"

int main (void)
{
	int  done = false;
	char* dataPtr;

	STACK* stack;

	stack = createStack ();

	while (!done)
	   {
	    dataPtr = (char*) malloc (sizeof(char));
	    printf ("Enter a number: <EOF> to stop: ");
	    if ((scanf ("%c" , dataPtr)) == EOF 
	           || fullStack (stack))
	       done = true;
	    else
	       pushStack (stack, dataPtr);
	   }

	printf ("\n\nThe list of numbers reversed:\n");
	while (!emptyStack (stack))
	   {
	    dataPtr = (char*)popStack (stack);
	    printf ("%3c\n", *dataPtr);
	    free (dataPtr);
	   }

	destroyStack (stack);
	system("pause");
	return 0;
}

Recommended Answers

All 3 Replies

The answer is that scanf() leaves the newline char (which is generated when you hit the enter key), behind.

To solve your problem, add a getchar() line of code, immediately below your scanf(), and all will be well, (unless you press other unnecessary keys).

Now you know just a bit of the problem of controlling user input, for your program. ASAP, stop using scanf() for programs that will be used for user input other than your own, and start using fgets() - it's much more robust.

if ((scanf ("%c" , dataPtr)) == EOF 
	           || fullStack (stack))
	       done = true;
               getchar();

Is that right? it got errors though.

Please explain more about fgets()? :?:

scanf() returns the number of items it has worked with - not EOF, etc.

fgets(nameOfSring, sizeof(nameOfString), stdin);

Always takes in a full line of code - of any type, with any punctuation, but you specifiy how many char's it should take in.

Puts the newline char: '\n', at the end of the string, then

Adds the end of string marker char: '\0', to every line of input it takes in.

The keyboard buffer has no "junk" char's left over in it.

Never overflows the string buffer - will leave off the newline and end of string marker char, in a pinch. (don't leave it "in a pinch", give it room to work).

Once the input is in the string buffer, you can work it any way you want to get your specific variables out of the string - ssccanf() is one way,

The biggest thing is you've got the input from the file or user, "in a cage", and you can now test it, remove parts of it as needed, convert certain char's, delete others, YOU CAN DO WHATEVER YOU WANT, through multiple tests, to get good input.

Try that with scanf() and you'll be practicing your cursing before long. ;)

If you indent your code with spaces, instead of tabs, the curly braces can line up much better. 2 to 4 spaces, is normally best. Your compiler (editor), will have an option for this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.