Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Structuce Manipulation
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
rar




PostPosted: Sun Mar 14, 2010 11:39 am   Post subject: Structuce Manipulation

Not really manipulating the structure but...

I'm making a program that works like an address book. It stores the contact's first name, last name, address, postal code, and phone number. Each of these is in a different array (or string, as it may be) in the structure. I'm accessing the array using contacts[x]. So for last name, at contact x, I'd type:
contacts[x].lname;

Now say I'm sorting my contacts alphabetically by last name. I'm checking if (strcmp(contacts[x].lname, contacts[x+1].lname) > 0) and if so, commence swapping. The way I'm doing it, it swaps the last names, then the first names, then the addresses, then the postal codes, and then the phone numbers. So I do it one element at a time. I don't know if you can swap the entire contact at once, so I went with this method.
Also, I have it so that this sort (which is in the same function as the Adding Contacts) only works if there is more than one contact.

Now when I put two contacts, it leaves the second contact's information where it is, but the first contact comes up blank. Why is this contact being cleared?
Sponsor
Sponsor
Sponsor
sponsor
Alexmula




PostPosted: Sun Mar 14, 2010 12:06 pm   Post subject: RE:Structuce Manipulation

i would suggest using pointers when swapping the contacts
TerranceN




PostPosted: Sun Mar 14, 2010 12:33 pm   Post subject: Re: Structuce Manipulation

Could you post your code, because your logic seems like it would work fine.

WARNING : I do not know all the differences between C and C++, so what I say from here on in may be wrong.

Quote:
I don't know if you can swap the entire contact at once


I think you can do this if you use pointers:

Hopefully this C++ code translates to C well (other than stuff like i/o)
c++:
#include <iostream>

using namespace std;

struct SomeType
{
        char *someData;
        int someMoreData;
        float evenMoreData;
};

SomeType CreateSomeType(char* newData, int moreNewData, float evenMoreNewData)
{
        SomeType t;

        t.someData = newData;
        t.someMoreData = moreNewData;
        t.evenMoreData = evenMoreNewData;

        return t;
}

void PrintSomeType(SomeType t)
{
        cout << t.someData << " " << t.someMoreData << " " << t.evenMoreData << "\n";
}


int main()
{
        SomeType *instance = &CreateSomeType("foo", 56, 3.1415F);
        SomeType *instance2 = &CreateSomeType("bar", 63, 6.2830F);

        PrintSomeType(*instance);
        PrintSomeType(*instance2);
        cout << "\n\n";

        SomeType *temp = instance;
        instance = instance2;
        instance2 = temp;

        PrintSomeType(*instance);
        PrintSomeType(*instance2);

        cin.get();

        return 0;
}


Hope that helps.
rar




PostPosted: Sun Mar 14, 2010 7:07 pm   Post subject: RE:Structuce Manipulation

I figured out my problem, it ended up being that I was going one too many elements in my swap. I do want to try it out with pointers though, so I might do that just as extra practice.

Thanks!
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: