Link to home
Start Free TrialLog in
Avatar of Heather_B
Heather_B

asked on

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]
ASKER CERTIFIED SOLUTION
Avatar of cup
cup

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rajeev_devin
rajeev_devin

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;

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.
Avatar of Heather_B

ASKER

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. :)