get something error with my code.

This is a discussion on get something error with my code. within the C Programming forums, part of the General Programming Boards category; This is my code for my homework ( Pop and push from stack) i get something wrong with this, why ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,185
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 02: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, 12: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, 01:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21