Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-04-2007, 04:07 PM
mtber mtber is offline
Newbie
 
Join Date: Nov 2007
Posts: 4
Rep Power: 0
mtber is on a distinguished road
Exclamation [C]Functions Help!

The program i am working on i am trying to get it to display how many items are in a list. but i am trying to do it with a function. The program compiles correct and gives no errors but when i run the program and input some things when it displays the number of items in the list it only displays 1 and will not increment when new items are added. Can any one see what i am doing wrong. Thanks.

Code:
#include <stdio.h>
#include <conio.h>
#include <malloc.h>

struct item {char name[10];
              int dollars, cents;
              struct item *next_item;};
struct item * Get_item (void);
void Display_List (struct item * ptr);
int count_List (struct item * ptr);
int count = 0;
int ptr_total;
      
void main()
  { struct item *ptr2start_of_list = NULL;
    struct item *ptr_temp = NULL;
    struct item *ptr_total = NULL;
    char response;

    ptr2start_of_list = Get_item();

    
    
    while (1)
    {
    //This displays the number of items in the List
    printf("\nThere are %i in the list", count_List(ptr_total));
    //count_List(ptr_total);
    
    printf ("\nWould you like to add another item? (Y or N)\n");
    fflush(stdin);
    response = getch();
    if (response == 'y')
      { ptr_temp = Get_item();      //creates and fills new item
        ptr_temp->next_item = ptr2start_of_list;  //move the pointers to insert into list
        ptr2start_of_list = ptr_temp;
      }
    else break;
    }

    //this would be a good spot to display the list!!!
    printf ("\nThe list contains: \n");
    Display_List(ptr2start_of_list);

    //This displays the number of items in the List
    printf("\nThere are %i in the list", count_List);
    //count_List(ptr_total);
    
    fflush(stdin);
    getch();

    return;
  }

struct item * Get_item (void)
{   struct item *ptr;

        //create a space to store the info, then fill the space
    ptr = (struct item *) malloc (sizeof (struct item));
    printf("\nType an item name:   ");
    gets(ptr->name);
    printf("\nType in a price, dollars first:");
    scanf ("%i", &ptr->dollars);
    fflush(stdin);
    printf("\nNow cents: ");
    scanf ("%i", &ptr->cents);
    fflush(stdin);
    ptr->next_item = NULL;
            
    return (ptr);
}

void Display_List (struct item * ptr)
{
                    //traverse the list, displaying each member
    while (ptr != NULL)
    {
        printf("\n.... %s,  $%d.%.2d", ptr->name, ptr->dollars, ptr->cents);
        ptr = ptr->next_item;   //advance to next item in list
    }
    fflush(stdout);
    return;
}

count_List (struct item * ptr)
{int counter = 1;
    //Tracers the list. counting each member
    while (ptr != NULL)
    {
        counter++;

        ptr = ptr->next_item;   //advance to next item in list
        counter = ptr_total;

    }
       return (counter);
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 12-05-2007, 09:25 AM
mtber mtber is offline
Newbie
 
Join Date: Nov 2007
Posts: 4
Rep Power: 0
mtber is on a distinguished road
Default

I got it working Thanks anyway
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -5. The time now is 05:06 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads