• C

char array/char pointer

I have commented the rest of this program out... what you see is the part in question. This should be pretty straight-forward. In case you question why i've done this they way I have (as someone invariably does), it's because this is the way my professor has done it. The problem is that when I attempt to print the usa.key value, it prints the key and the name together. This makes no sense to me whatsoever. Any ideas?

[code]
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

class University
{
      public:

            char key[4];
            char name[100];
            char webAddress[100];
            char numStudents[7];
            char category[7];

            University(char* theKey, char* theName, char* theWebAddress, char* theNumStudents, char* theCategory)
            {
                  strcpy(key, theKey);
                  strcpy(name, theName);
                  strcpy(webAddress, theWebAddress);
                  strcpy(numStudents, theNumStudents);
                  strcpy(category, theCategory);
            };

            University()
            {
                  strcpy(key, "");
                  strcpy(name, "");
                  strcpy(webAddress, "");
                  strcpy(numStudents, "");
                  strcpy(category, "");
            };
};

int main()
{
      University usa = University("1101", "University of South Alabama", "http://www.southalabama.edu", "13000", "public");

      cout << usa.key;

      return(0);
};

[/code]
Heather_BAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cupCommented:
There are only 4 chars allocated for the key and you are putting in a key with 4 chars but a key with 4 chars needs 5 chars to be allocated: 4 for the content and 1 for the terminator.  The reason why the key and name are printed together is that the key has overflowed into the name and since the value is set after the key, it overwrites the terminator.

If it had been done the other way round, ie name  first then key, you'll get a key with no name.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rajeev_devinCommented:
First of all you should have put the question in C++ area.
rajeev_devinCommented:
If you can use string then why char* ?
Make all this
char key[4];
char name[100];
char webAddress[100];
char numStudents[7];
char category[7];

string key;
string name;
string webAddress;
string numStudents;
string category;

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

rajeev_devinCommented:
Then you can use simple assignment instead of strcpy(...). Like this
key = theKey;
rajeev_devinCommented:
The reason you are getting unwanted result is very clear from cup's comment.
Heather_BAuthor Commented:
This is only part of the program.. I have to write the objects to a binary hash file so i guess the prof did it that way so the objects would have fixed size... with only one day left to finish the assignment I think I'll stick with his method instead of trying to get creative. :)

My bad about putting this in the C area... I was barely functioning when I posted my question. :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.