Help with linked list retrieval

This is a discussion on Help with linked list retrieval within the C Programming forums, part of the General Programming Boards category; This file will not read in the balance correctly. Could you tell me why? Code: #include <stdio.h> #include <stdlib.h> #include ...

  1. #1
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65

    Help with linked list retrieval

    This file will not read in the balance correctly. Could you tell me why?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    /* Function Prototypes */
    void addAccount(void);     
    void listAccounts(void);				
    void deleteAccount(void);				
    
    /* The structure of a basic account */
    
    struct account
    {
    	int number;
    	char name[50];
    	double balance;
    	struct entry
    	{
    		int number;   /*Checkbook entry */
    		int date;
    		char info[100];
    		union
    		{
    			double credit;  /*credit or debit, not both */
    			double debit;
    		};
    	};
    	struct account *next; /* Pointer to next account in linked list */
    };	
    struct account *firsta,*currenta,*newa; /* Linked list pointers */
    int anum = 0;
    
    int main()
    {
    	char c;
    	firsta = NULL;
    	int done = 0; /* for while loop */
    	char go;		/* for exit program condition */
    	while(done == 0) 
    	{
    		fflush(stdin);
    		puts("\nDigital Checkbook\n");
    		puts("A - Add new account\n");
    		puts("L - List accounts\n");		/* Main menu */
    		puts("D - Delete Account\n");
    		puts("Q - Quit\n");
    		printf("Your choice: ");
    		
    		c = getchar(); 
    		fflush(stdin);
    		c = toupper(c);
    		switch(c)
    		{
    			case ('A'):
    				addAccount();
    				break;
    			case ('L'):
    				listAccounts();
    				break;
    			case ('D'):
    				deleteAccount();
    				break;
    			case ('Q'):
    				printf("\nAre you sure you want to quit? Y/N: ");
    				go = getchar();
    				go = toupper(go);
    				if(go == 'Y')
    				{
    					done = 1;
    					fflush(stdin);
    					system("cls");
    				}
    				else if(go == 'N')
    				{
    					fflush(stdin);
    					system("cls");
    				}
    				break;
    			default:
    				puts("\nInvalid answer ");
    		} /*End switch*/
    	} /*End while*/
    	return(0);
    } /*End main()*/
    
    
    void addAccount(void)
    {
    	newa = (struct account *)malloc(sizeof(struct account)); /* Allocate proper space*/
    	
    	/* Check to see if this is the first record. If so, then initalize all the pointers to this first structure in the database */
    	if(firsta == NULL)
    	{
    		firsta = currenta = newa;
    	}
    	
    	/* Otherwise, find the end of the structure list, the one with the NULL pointer, and add on the new structure you just allocated memory for */
    	
    	else
    	{
    		currenta = firsta; 		/*make first record current */
    		while(currenta->next != NULL) /*Loop through all till NULL pointer found */
    		{
    			currenta = currenta->next; /* last record found */
    			currenta->next = newa;     /*save the address of new */
    			currenta = newa; 		   /* make current record new*/
    		}
    	}
    
    	/* Enter info */
    	anum++;
    	printf("%27s: %5i\n","Account number",anum);
    	currenta->number = anum;
    	printf("%27s: ","Enter account balance (Without commas)");
    	scanf("%s",&currenta->balance);
    	
        /* Cap new record with NULL pointer so I know it's the last record */
    
    	currenta->next = NULL;
    }
    
    void listAccounts(void)
    {
    	if(firsta == NULL)
    	{
    		puts("There are no records to list!");
    	}
    	else
    	{
    		currenta = firsta;
    		do
    		{
    			printf("Account number: %d\n",currenta->number);
    			printf("Balance: %lf",currenta->balance);
    		}
    		while((currenta=currenta->next) != NULL);
    	}
    	getchar();
    	return;
    }
    
    void deleteAccount(void)
    {
    }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Code:
    double balance;
    ...
    scanf("%s",&currenta->balance);
    What are you doing with the call to scanf? 'balance' is type double, but you are not asking for a double with scanf....

    Have you got the warnings turned on for your compiler?

  3. #3
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    Oops. Thanks a lot. It must have been a typo.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    There are other problems lurking here as well.

    Code:
    char c;
    ...
    c = getchar();
    getchar returns an int, not a char.

    Code:
    fflush(stdin);
    This produces undefined behaviour. See here, here, and here for an explanation.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kermit View Post
    There are other problems lurking here as well
    Code:
    char c;
    ...
    c = getchar();
    getchar returns an int, not a char.
    IT'S THE EXACTLY SAME THING SILLY! getchar() will never return a value higher than 127 and it exactly corresponds to the ascii value of the character, which means it's exactly the same if:
    Code:
    char x='x';
    char x=120;
    That's what getchar() does.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MK27 View Post
    IT'S THE EXACTLY SAME THING SILLY! getchar() will never return a value higher than 127 and it exactly corresponds to the ascii value of the character, which means it's exactly the same if:
    Code:
    char x='x';
    char x=120;
    That's what getchar() does.
    getchar returns a value higher than 127 quite a bit, I would guess. EOF, for one.

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by MK27 View Post
    IT'S THE EXACTLY SAME THING SILLY! getchar() will never return a value higher than 127 and it exactly corresponds to the ascii value of the character, which means it's exactly the same if:
    Code:
    char x='x';
    char x=120;
    That's what getchar() does.
    Am I really that silly? Try the following code, and see what you get:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        unsigned char c;
    
        while ((c = getchar()) != EOF) {
            putchar(c);
        }
    
        return 0;
    }
    Working with an implementation where declaring 'char' is actually 'unsigned char' by default, when you define c as a plain char (char c;), you end up with a broken program. In effect, it is the same as if you did the above code. You could check to see what your implementation does (i.e., what kind of char it uses for plain char) but why bother? If you assign the return value of getchar to an int, (if you read your documentation for getchar, you will see that its return type is indeed an int), you will avoid problems such as this.
    Last edited by kermit; 01-15-2009 at 08:27 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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