GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Nov-2009, 08:21
iblink iblink is offline
New Member
 
Join Date: Nov 2006
Posts: 24
iblink has a little shameless behaviour in the past

Problem with comparing character


Hello. I am trying to push in into a stack characters one by one from a char array. It is prompted to stop when an asterisks '*' char is read. But it doesnt seem to work that way for me. I am having problem with the part where i compare each char to '*' while(input[i] != '*'). I get no output from the program and it exits. Can someone help me with that. My program is bellow.

CPP / C++ / C Code:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

typedef struct 
{
    char home[35];
    int top;
} My_stack;

void push (My_stack *, char);/
void pop (My_stack *);
int is_empty (My_stack *);


int main()
{
	My_stack st;
	char input[50];   /* input sequence for processing */
	int i = 0;
	st.top = -1;

	printf("Enter the sequence : ");
	gets(input);
	
	while(input[i] != '*')   /* while '*' is not reached */
	{
		push(&st, input[i]);  /* push character onto the stack */
			
		i++;	
	}
	i =0;
	while(input[i] != '\0'){
	pop(&st);
	i++;
	}
	return 0;
}
void push(My_stack * s, char c)   // push (insert) operation
{  // assume there is enough space for pushing next element!
	s->top ++;
	s->home[s->top] = c; 
}
void pop(My_stack * s)   // pop (remove) operation
{
	if(is_empty(s))  
	{ 
		printf("ERROR: Nothing to pop - program terminates\n");
		exit(1);
	}
	printf("%c", s->home[s->top--]);
}
int is_empty(My_stack * s)   // checking whether stack is empty or not
{
	return(s->top < 0 ? 1 : 0);
}
Last edited by LuciWiz : 12-Nov-2009 at 08:28. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #2  
Old 12-Nov-2009, 08:28
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Problem with comparing character


What is your intention here?
CPP / C++ / C Code:
while(input[i] != '\0'){
	pop(&st);
	i++;
	}

Also, you may want to put printf() statements throughout the code so that you can see the value of certain variables. For instance, it might be wise to print to the screen the user input that you read.
  #3  
Old 12-Nov-2009, 08:35
iblink iblink is offline
New Member
 
Join Date: Nov 2006
Posts: 24
iblink has a little shameless behaviour in the past

Re: Problem with comparing character


That isn't part of the initial program. Just did that to see what i have in the stack after pushing the characters.
  #4  
Old 12-Nov-2009, 09:11
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Problem with comparing character


I ran your program with printf() in it like so:

CPP / C++ / C Code:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

typedef struct 
{
    char home[35];
    int top;
} My_stack;

void push (My_stack *, char);
void pop (My_stack *);
int is_empty (My_stack *);


int main()
{
	My_stack st;
	char input[50];   /* input sequence for processing */
	int i = 0;
	st.top = -1;

	printf("Enter the sequence : ");
	gets(input);
	printf("Received: %s\n", input);
	
	while(input[i] != '*')   /* while '*' is not reached */
	{
		printf("input[%d]: %c\n", i, input[i]);
		push(&st, input[i]);  /* push character onto the stack */
			
		i++;	
	}
	i =0;
	while(input[i] != '\0'){
	pop(&st);
	i++;
	}
	getchar();
	return 0;
}
void push(My_stack * s, char c)   // push (insert) operation
{  // assume there is enough space for pushing next element!
	s->top ++;
	s->home[s->top] = c; 
}
void pop(My_stack * s)   // pop (remove) operation
{
	if(is_empty(s))  
	{ 
		printf("ERROR: Nothing to pop - program terminates\n");
		getchar();
		exit(1);
	}
	printf("%c", s->home[s->top--]);
}
int is_empty(My_stack * s)   // checking whether stack is empty or not
{
	return(s->top < 0 ? 1 : 0);
}

This is the output:
Code:
Enter the sequence: abc*jkl Received: abc*jkl input[0]: a input[1]: b input[2]: c cbaERROR: Nothing to pop - program terminates

This is what I expected. Did you?
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Print Japaneese character on Linux terminal & debugging problem rajeshgalla C Programming Language 1 17-Apr-2008 10:57
Print Japaneese character on Linux terminal & debugging problem rajeshgalla C++ Forum 0 14-Apr-2008 01:44
Problem with character encoding linkin C++ Forum 0 26-Mar-2007 22:21
comparing multiple character arrays in C alucard C Programming Language 4 03-Feb-2006 00:47
Character Array problem lyndonp C++ Forum 5 27-Jul-2005 21:19

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:35.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.