[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

char array/char pointer

Posted on 2006-04-26
6
Medium Priority
?
303 Views
Last Modified: 2010-04-15
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]
0
Comment
Question by:Heather_B
  • 4
6 Comments
 
LVL 11

Accepted Solution

by:
cup earned 500 total points
ID: 16550302
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.
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16550488
First of all you should have put the question in C++ area.
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16550489
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;

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16550492
Then you can use simple assignment instead of strcpy(...). Like this
key = theKey;
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16550496
The reason you are getting unwanted result is very clear from cup's comment.
0
 

Author Comment

by:Heather_B
ID: 16553078
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. :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question