Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 18th, 2006, 6:55 PM   #1
thenewkid
Programmer
 
Join Date: Sep 2004
Location: New Jersey
Posts: 36
Rep Power: 0 thenewkid is on a distinguished road
Send a message via AIM to thenewkid
c stack implementation

hey..my question is not really related to the stack itslef but to something ive encountered while i was writing the code for it:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define SIZE 5

void push(int);
int pop(void);
void printstack(void);

int stack[SIZE], *top = &stack[49], *current, input;
int c;  

int main()
{
	current = stack;

	do {
	printf("Enter the integer: ");
	scanf("%d", &input);
	push(input);
	printf("Do you want to continue (1 for yes / 2 for no): ");
	scanf("%d", &c);
	}while(c == 1);
	printstack();
}

void push(int i)
{
	if(current <= top)
	{
		*current = i;
		current++;
	}
	else
	{
		printf("Stack Overflow occured\nPrinting started: ");
		printstack();
	}
}

int pop()
{
	current--;
	int temp = *current;
	return temp;
}

void printstack()
{
	while(current != stack)
		printf("%d ", pop());
	printf("\n");
}
what the code does is it ask user to input an integer value, then it asks whether he/she wants to continue, as you can see the i use int values to control this decision...and here is my questio: in the beginning i was asking the user to input either y for yes or n for no, c variable was declared as char, scanf() was set to read a character, and the condition in the while statement was as such: do { ..... } while( c == 'y' ); with this in plece the second call scanf wasnt made...the code simply terminated...why i have no idea...for sake of simplicity i attach the other, latter version of the code as well...thanks...and appologize for this long post
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define SIZE 5

void push(int);
int pop(void);
void printstack(void);

int stack[SIZE], *top = &stack[49], *current, input;
char c;

int main()
{
	current = stack;

	do {
	printf("Enter the integer: ");
	scanf("%d", &input);
	push(input);
	printf("Do you want to continue (y/n): ");
	scanf("%c", &c);
	}while(c == 'y');
	printstack();
}

void push(int i)
{
	if(current <= top)
	{
		*current = i;
		current++;
	}
	else
	{
		printf("Stack Overflow occured\nPrinting started: ");
		printstack();
	}
}

int pop()
{
	current--;
	int temp = *current;
	return temp;
}

void printstack()
{
	while(current != stack)
		printf("%d ", pop());
	printf("\n");
}
thenewkid is offline   Reply With Quote
Old May 18th, 2006, 8:27 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
The problem is that first scanf is only reading the number you have typed in, and is not reading the carriage return that you also typed to enter the data. When the second scanf is run looking for any character, that character is the carriage return.
There are lots of threads in the forum where this is discussed, along with options for fixing it.
The Dark is offline   Reply With Quote
Old May 18th, 2006, 8:49 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I wrote this three or four years ago. Stay tuned for it to appear someplace permanent. I'll work in the slightly different "unexpected (but correct) content" phenomenon mentioned by The Dark.
Quote:
Originally Posted by Scanf! Bad boy!
Why does my scanf/fscanf/sscanf stop working?

Most C input is provided in a stream. That is, it is a series of characters made available one at a time. The scanf() function family are format-sensitive functions; they not only collect the characters for you, but attempt to convert them to a type (such as an integer) that you specify. They have great difficulty converting ZyGH4 to a meaningful number so they fail. The conversion attempt is governed by format specifiers that YOU provide. Since these may not match the input actually encountered, the family returns a value indicating the number of items successfully scanned AND assigned. If this value is zero, you have nothing. If this value is EOF, there was an end-of-file or other error. If you don't examine the return, how will you know? If an error occurs it will not be automatically cleared. Operations on the stream will continue to return an error until clearerr(), fseek(), fsetpos(), or rewind() is called. This means that a loop that is designed to pause for input will loop indefinitely.

The characters that f/s/scanf attempt to convert as one value are all the characters up to the first whitespace character (space, tab, newline) or up to the specified field width, or up to the first character that cannot be converted. (Note: The [ and c format directives are not whitespace delimited, but we won't consider them for the explanation here). If a character conflicts with the format specification, the function terminates and the character is left in the stream as if it had not been read. You probably will not expect it to be there to serve as input for your next call, so your input will not behave as you expect.

Example of proper usage:
int status;
status = scanf ("%s%d\n", name, &number);

Check the value of 'status' after the scanf() call. If it is not what you expect (two, in this case), you didn't get all your fields. If it is EOF, your stream is broken and will remain so until you clear the error.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:48 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC