• 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?

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

class University

            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);

                  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;


Who is Participating?

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

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.

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
First of all you should have put the question in C++ area.
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.

Then you can use simple assignment instead of strcpy(...). Like this
key = theKey;
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

From novice to tech pro — start learning today.