Thread: newbie with stack

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    11

    newbie with stack

    Code:
    #include <stdlib.h>    
    #include <stdio.h>    	
    #include <string.h>    	
    #include <ctype.h>    	
    #include <io.h>    	
    #include <conio.h> 
    
    struct client
    {
    	char name[40];
    	int act_number;
    	int balance;
    	
    
    };
    
    struct LLclient{	
    	struct client stud;
    	struct LLclient *next;
    };
    
    struct client clients;
    void addaccount(char namee);
    void linklist(char name,struct LLclient *head);
    struct LLclient *initclient(char name);
    
    
    
    void main (void)
    {	int select;
    	char name;
    
    
    	struct LLclient *head;
    	struct LLclient *tail;
    
    	head=initclient(NULL);
    	tail=initclient(NULL);
    	head->next=tail;
    
    	printf ("\nenter selection");
    	printf ("\n1.enter account");
    	printf ("\n2.view account");
    	printf ("\n3.delete account");
    	scanf ("%d",&select );
    
    	switch (select)
    	{
    	case 1 :
      addaccount(name);
      linklist(name,head);
    
    
    	}
    
    }
    
    void addaccount(char *name)
    
    {
    printf ("\nenter name");
    scanf ("%s", name);
    printf (name);
    
    
    return;
    
    }
    
    struct LLclient *initclient(char name){
    	struct LLclient *temp;
    	temp=(struct LLclient *)malloc(sizeof(struct LLclient));
    	temp->next = NULL;
    	strcpy(temp->stud.name,name);
    	
    	
    	return(temp);
    }
    
    void linklist(char name, struct LLclient *head){
    	struct LLclient *temp;  
    	struct LLclient *curr;
    	temp=initclient(name);
    	curr = head;
    	while(curr->next->next != NULL) 
      curr = curr->next; 
    	temp->next=curr->next;
    	curr->next=temp;  	
    	return;      
    }
    no error 8 warning
    rawr , I can get the logic right but not most of the syntax
    can anyone help me ? IM struggling even the basic scanf and printf

    this program basically allow user to enter a particular account information . first it display the option for the switch case , then after selecting specific switch case it will jump to specific constructor

    I have add in add node first , I want to allow the user to enter name balance and account number , then use stack to expand the node . first I create a statement to pass the entered value to another constructor , where it will enter it accordingly using malloc . then it come back to original constructor . where I will assign a new pointer name curr to head , then use curr-> next point to temp then use head -> next = curr -> next to link temp inside between head and tail

    I havent add in adding entering information for balanace and account info yet and havent add in the delete function yet . and still got many mistake here and there but my knowledge of c is very limited . were better in java . anyone can help ?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've got a number of issues.
    1) Unlike Java, main always returns an int in C and C++. Always.
    2) You have 'name' defined as a char. Just one. Not a string of them, only one single character.
    3) You don't have a 'break;' in your switch's case statement. While this isn't 'wrong' per say, it probably won't be what you want it to be once you have more than one case.
    4) Consider reading the FAQ entry on getting input from a user for better methods of input.
    5) When you get 'name' fixed, you'll need to look into the string functions such as strcpy to copy it over correctly from place to place.
    6) Linked lists are really easy, especially when all you want to do is prepend to one. (Though I prefer doubly linked lists, because they're actually easier to work with.)
    Code:
    void prepend( struct Node **mylist, struct Node *thisnode )
    {
        thisnode->next = *mylist;
        *mylist = thisnode;
    }
    You call this function with something like this:
    Code:
    struct Node *thelist;
    struct Node *somenode;
    
    ...set 'thelist' to NULL, or put something in it, either way...
    ...allocate and fill up 'somenode'...
    
    prepend( &thelist, somenode );
    Since it sounds like that's what you want. First in, last out. What we're doing is passing a pointer to the actual list so that we can update what it points to inside the function. Otherwise, if we didn't use a pointer to it, the change wouldn't take place unless you did it this way:
    Code:
    thelist = prepend( thelist, somenode );
    And the prepend function would have to look like this:
    Code:
    struct Node *prepend( struct Node *thelist, struct Node *thisnode )
    {
        thisnode->next = thelist;
        return thisnode;
    }
    Here you just make the new node point to the top of the list, and return it. It's return would have to be assigned to the origional list pointer, otherwise the update wouldn't happen, and you'd end up with a ton of nodes all pointing to the same place, each of them thinking they're the top.


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

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    god damm it I freaking hate c now ...
    I have been alot of trouble with syntax

    I have completely rewritten my code
    why c gotto be soo hard , why it cant be like java

    T.T

    oh yeah ... how init work ?
    I cant seem to get it work ...

    and how gets work ?

    how return work ?

    sorry for many question but I really dont understand mostof the online guide I can google
    Last edited by guest18; 08-19-2005 at 01:48 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well we're not stopping you going back there....
    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
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Instead of using gets(), you should do this:
    Code:
    char s[100];
    
    fgets(s, sizeof(s), stdin);
    That can avoid buffer overruns. See the FAQ.

    Code:
    printf (name);
    Either use printf("%s", name); to print a string, or puts(name); which adds a newline onto the end.

    You shouldn't cast malloc. See the FAQ.

    Post your program as it is now.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  4. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM