Thread: get something error with my code.

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    6

    get something error with my code.

    This is my code for my homework ( Pop and push from stack)
    i get something wrong with this,
    why when i make the 2nd time selection, the program will crash??
    anyone can tell me or help me edit it to the correct 1??


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stack>
    
    struct stack{
    	char *storage [50];
    	int top;
    };
    
    bool stk_isFull(stack * s){
    	if( s->top == 50){
    		return true;
    	}else{ 
    		return false;
    	}
    }
    
    void stk_init( stack * s){
    	s->top = 0;
    }
    
    void stk_push(stack*s){
    	char *item;
    	if(stk_isFull(s)){
    		printf("stack is full\n\n");
    	}else{
    		printf(" KEY IN THE ITEM YOU WANT TO PUSH\n\n");
    		scanf("%s",&item);
    		s->storage[s->top] = item;
    		(s->top)++;
    		printf(" %c has been pushed\n\n",item);
    	}
    }
    bool stk_isEmpty( stack*s){
    	if(s->top == 0)
    		return true;
    	else
    		return false;
    }
    
    
    void stk_pop(stack *s){
    	if (stk_isEmpty(s)){
    		printf("Stack is Empty.");
    	}else{
    		printf("%c has been popped!",s->storage[s->top]);
    		s->top = s->top - 1;
    	}
    }
    
    void stk_print(stack *s){
    	int x;
    	for(x = s->top; x > 0; x--){
    		printf("%d",s->storage[x]);
    	}
    }
    
    void menu(){
    	printf("PLEASE SELECT YOUR OPTION\n");
    	printf("1.PUSH\n");
    	printf("2.POP\n");
    	printf("3.DISPLAY\n");
    	printf("4.QUIT\n");
    	printf("Please Choose It:");
    }
    
    bool exit(){
    	char select[4];
    	printf( "\nType End To Exit " );
    	scanf( "%s", select );
    	if( strcmp( select, "END") == 0  || strcmp( select, "end" ) == 0 )
    		return true;
    	else {
    		return false;
    	}
    }
    
    
    void main (){
    	stack stk;
    	char *item;
    	stk_init(&stk);
    	int sel;
    	menu();
    	scanf("%d", &sel);
    	while(1){
    		switch(sel){
    			case 1:
    				stk_push(&stk);
    				break;
    			case 2:
    				stk_pop(&stk);
    			    break;
    			case 3:
    				stk_print(&stk);
    				break;
    			case 4:
    				exit();
    				break;
    			default:
    				printf("\n\nNOTHING HAS BEEN CHOOSE");
    		}
    		menu();
    		scanf("%d",sel);
    	}
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > scanf("%s",&item);
    > s->storage[s->top] = item;
    So where is the PERSISTENT storage for the string you have input here?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    6
    hmm i dont understand ...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well first of all, do you intent to have a stack of strings, or a stack of characters?

    Because different bits of your code are saying different things.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    	char *item;
    	if(stk_isFull(s)){
    		printf("stack is full\n\n");
    	}else{
    		printf(" KEY IN THE ITEM YOU WANT TO PUSH\n\n");
    		scanf("%s",&item);
    What does item point to? Also, you are using scanf wrong there, which your compiler should be complaining about, since you are giving the address of a pointer, not a pointer itself.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    6
    i wan to intent a stack of strings

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    6
    Quote Originally Posted by quzah View Post
    Code:
    	char *item;
    	if(stk_isFull(s)){
    		printf("stack is full\n\n");
    	}else{
    		printf(" KEY IN THE ITEM YOU WANT TO PUSH\n\n");
    		scanf("%s",&item);
    What does item point to? Also, you are using scanf wrong there, which your compiler should be complaining about, since you are giving the address of a pointer, not a pointer itself.


    Quzah.
    then wat to change to make it correct ??

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the simple thing is to do

    char storage [50][20];

    then in your push function
    char item[20];

    Then use strcpy() to copy the input to the stack.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    6
    Code:
    void stk_push(stack*s){
    	char item[20];
    	if(stk_isFull(s)){
    		printf("stack is full\n\n");
    	}else{
    		printf(" KEY IN THE ITEM YOU WANT TO PUSH\n\n");
    		scanf("%s",&item);
    		s->storage[s->top] = item;
    		(s->top)++;
    		printf(" %c has been pushed\n\n",item);
    	}
    }
    Like dis???
    but still got error

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you changed the storage, then

    strcpy( s->storage[s->top], item );

    > printf(" %c has been pushed\n\n",item);
    And all the %c formats you use for printing a string need to be %s as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    May 2011
    Posts
    6
    its done, but can you help me try to run the code??
    i cant input any thing, it straight crash after i input

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by imbax8299 View Post
    its done, but can you help me try to run the code??
    i cant input any thing, it straight crash after i input
    Then it is very very far from "done".

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by imbax8299 View Post
    its done, but can you help me try to run the code??
    i cant input any thing, it straight crash after i input
    How many error messages did you ignore while it compiled?

    Code:
    char item[20];
    ...
    
    printf(" KEY IN THE ITEM YOU WANT TO PUSH\n\n");
    scanf("%s",&item);

    Lose the ampersand.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Post your latest code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code Error
    By kbat82 in forum C Programming
    Replies: 13
    Last Post: 11-13-2008, 03:05 PM
  2. error in code
    By cole in forum C Programming
    Replies: 6
    Last Post: 09-26-2008, 09:51 PM
  3. Code error
    By DeepFyre in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2004, 01:32 AM
  4. Help with VC++ 6.0 (or my code) error
    By theodoreius2000 in forum Game Programming
    Replies: 7
    Last Post: 07-13-2004, 09:48 PM
  5. error with code
    By dayknight in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2004, 02:43 PM