Hi...Please help me with while loop...

This is a discussion on Hi...Please help me with while loop... within the C Programming forums, part of the General Programming Boards category; This program suppose to read 10 integer values and then add in array list and display it..But my promblem is ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    36

    Hi...Please help me with while loop...

    This program suppose to read 10 integer values and then add in array list and display it..But my promblem is why i can't stop entering values even it reach 10 instead it suppose to.. and it is something like infinite loops..Please give me some advice or tips..Thanks...
    Code:
     #include<stdio.h>
     #define SIZE 10
     
     typedef struct 
     {
             int number[SIZE];
             unsigned size;
     }IntList;
     
     void makeList(IntList *);
     unsigned findLocation(IntList *, int);
     void addItem(IntList *, int, unsigned);
     void displayItem(IntList *);
     
     int main()
     {
         int input;
         IntList sample;
         int i;
         
    Code:
          /* i think the problem is in here */ 
         makeList(&sample);
         printf("Enter any 10 integers values : ");
         while ( scanf( "%d", &input) == 1 && sample.size < 10 )
    addItem(&sample, input, findLocation(&sample, input)); displayItem(&sample); return 0; } void makeList(IntList *ptr) { if (ptr!=NULL) ptr->size=0; } unsigned findLocation(IntList *data, int searchItem) { unsigned i, found; if (data != NULL) for (i=0, found = 0; i < (data->size) && !found; i++) { if (data->number[i] > searchItem) { i--; found = 1; } } return i; } void addItem(IntList *data, int newItem, unsigned location) { unsigned i; for (i=data->size;i>location;i--) data->number[i] = data->number[i-1]; data->number[location] = newItem; } void displayItem(IntList *data) { unsigned i; for (i=0; i < data->size; i++) printf("\nNumber[%d]=%d", i, data->number[i]); }

  2. #2
    Registered User
    Join Date
    May 2005
    Posts
    24
    you're not incrementing the structure's 'size' variable each time you add an item.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    ok..then wer should i add it?

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    24
    take a wild guess.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    issit i should add sample.size++; or declared another variables inside that count the size in the main function.

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    24
    Code:
     void addItem(IntList *data, int newItem, unsigned location)
     {
          unsigned i;
          for (i=data->size;i>location;i--)
               data->number[i] = data->number[i-1];
               
          data->number[location] = newItem;
          data->size++;     
     }

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    ooo..ookok..thanks alot buddy..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

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