Thread: Arrays......i think

  1. #1
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168

    Arrays......i think

    I'm trying to make a very basic database but it it's either not writing to the array properly or it's not printing the data properly, and i have no idea why, i've been trying to figure it out all nght. Maybe i've made a mistake with the variables i and np, any ideas? thanks

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
        struct member
        {
               char name[50];
        }
        member[10];
        int choice = 0;
        int np = 0; //Number of members on database
        int i; // Member pointer
    
        while (choice !=3)
        {    
             cout<< "\nWhat would you like to do?";
             cout<< "\n 1. Add a member";
             cout<< "\n 2. View current members";
             cout<< "\n 3. Exit";
             cout<< "\n Choice: ";
             cin>> choice;
             cout<< "\n";
             
             switch (choice)
             {
                    case 1:
                         if (np < 10)
                         {
                             cout<< "Name: ";
                             cin>> member[np].name;
                             np = np + 1;
                         }
                         else
                         {
                             cout<< "The database is full!";
                         }
                    case 2:
                         i = np;
                         for (int i; i > 0; i--)
                         {
                             cout<< member[i].name;
                         }
             }
        }
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    For one thing, you are redeclaring i in your loop which you should not be doing. But there are still logical errors in your code.

    Let's consider adding two people. When you add the first person, they go into member[0] and np gets set to 1. When you add the second person, they go into member[1] and np gets set to 2.

    Now let's display the members. When displaying the users, you set i to np (which is 2), and you then start displaying the information from member[2]. You see the problem yet? member[2] doesn't have any valid data in it at this stage. But let's continue... after displaying garbage from member[2], i gets set to 1. You now display the information from members[1]. This should work out OK as that location stores the information for the second member you entered and i gets set to 0. Now since your loop condition stops when i is 0, you will never get to see the first user entered into your database which exists as member[0].

    You need to change your loop a bit, this:

    Code:
    i = np;
    for (int i; i > 0; i--)
    {
        cout<< member[i].name;
    }
    Should be this:

    Code:
    for (i=np-1; i >= 0; i--)
    {
        cout<< member[i].name << endl;  // Added endl for readability on output
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    ah thanks! your a star! I dry ran the code through a couple of times, i dont know how i missed that.

    Let me just clear something up, the code

    Code:
     for (int i; i > 0; i--)
    declares the variable i? so i wouldnt need it at the beginning of the program?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Welshy
    Let me just clear something up, the code

    Code:
     for (int i; i > 0; i--)
    declares the variable i? so i wouldnt need it at the beginning of the program?

    If you wanted to, yes, you could get rid of the int i at the begining of main if you wanted and do it like your example above.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    alright, thanks hk_mp5kpdw, that'll save some lines of code in future.

    I've got another question about arrays, if i wanted to delete an entry in an array and so wanted the entrys above it to all move down one, what would be the best way to go about it? If you dont know what i mean ill try explaining a little better:

    0 A
    1 B
    2 C

    If i deleted A, how could i get B to move to position 0 and C to position 1?
    I have a rough coding:

    Code:
    for (int i; i < np; i++)
    {
                j = i + 1;
                member[i].name = member[j].name;
    }
    But it comes up with the error 'ISO forbids assignment of arrays'.

    I'd recommend to people new to C++ to try and make a database once they've learned about arrays, you learn a lot in terms of loops.
    Last edited by Welshy; 03-21-2005 at 03:38 PM.

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    First off you aren't initializing i in the loop to anything. You'll want to assign i to the index where you just deleted an item. Second, you are incrementing i one too many times, otherwise i+1 will pull an item thats not part of your array. Last, when done, you need to decrease np by one and possibly delete the last item in the array although that part is optional.

    So, something like this:
    Code:
    for (int i=deletedIndex; i < np-1; i++)
    {
                j = i + 1;
                member[i].name = member[j].name;
    }
    np--;

  7. #7
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    But it still comes up with the error 'ISO forbids assignment of arrays', surely you can assign new values to arrays?

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    When dealing with arrays, you can't simply assign one to another and expect the contents of one array to be replaced with the contents of another. In this case I would suggest you use strcpy.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    The array that it is complaining about is not the member array, but the char array name. You can't assign one char array to another without strcpy as hk_mp5kpdw points out.

  10. #10
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    yeah, i got it working now, strcpy did the trick. Thanks again guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM