Advertisement

Why does this print blah!?

Started by April 28, 2002 10:37 AM
1 comment, last by Wachar 22 years, 6 months ago
Why does this print blah for the name!?
  
#include <iostream>
using namespace std;
//------------------------------------Function Declarations-----------------------------------------

int humanSetData();
int humanSetUnit();
int alienSetData();
int alienSetUnit();


//------------------------------------------Classes-------------------------------------------------

/////////////////////////////////////////////HUMANS/////////////////////////////////////////////////

struct Humans
{
public:
	char * userName;
	int health;
	Humans()
	{}
}human1;
/////////////////////////////////////////////ALIENS/////////////////////////////////////////////////

struct Aliens
{
public:
	char * userName;
	int health;
	Aliens()
	{}
}alien1;
////////////////////////////////////////GLOBAL VARIABLES////////////////////////////////////////////


//--------------------------------------------MAIN--------------------------------------------------

int main()
{
	//Any temporary variables

	int choice;

	//Choose your race

	cout << "\"Come on!\"  You''re fellow beings are in a frenzy!  Help them survive!\n\n";
	cout << "Choose you''re race now; 1 - Humans OR 2 - Aliens: ";
	cin >> choice;

	switch(choice)
	{
	case 1:
		{
			humanSetData();
			break;
		};
	case 2:
		{
			alienSetData();
			break;
		}

	default:break;
	}
return(0);
}

int humanSetData()
{
	char temp_name[30];
	
	cout << "\n\n\"Alrighty then!  Let''s get youse registered!\nName: ";

	cin >> temp_name[30];
	human1.userName = temp_name;

	cout << "\n\n\"All right, welcome aboard you " << temp_name 
		<< "!.  Let''s get down to business quick like!";
	cout << " You know what''s happening out there. So get out and help us win this battle!\"\n\n";
	
	return(0);

}


int alienSetData()
{
return(0);
}
  
---------- Take it to the Xtreme! ----------
Wachar's Eternity <-<-<-<-<- Me own site!
If you mean ''blah'' as in garbage, (if you mean it actually prints the word blah, i have no idea) but there are some things i see that need fixing.
Let me first say, that you would probably (as far as i can see at this stage) be better of using STL string''s instead of char* for your names etc. That will almost immediately fix your problem...
But if you are going to use char* then:
cin >> temp_name[30]; <-- I''m quite certain that the [30] should not be here. If you wish to set a limit on the number of characters that can be read in on the next read (to prevent overflow) you can use cin.width(30); this will read in a maximum of 29 characters and place a null terminator (0) at the end.

human1.userName = temp_name; <-- you should use strcpy() (#include <string.h> to copy the string across. The way you currently have it copies the pointer, and when your function returns the area of memory that userName is pointing to will be free''d by the system to be used elsewhere. This also means you will need to allocate memory for userName before you call strcpy(), using either ''new'' or setting a hardcoded character array length in the class.

I think that should set things straight.
Advertisement
Part of your post was right. I didn''t mean to use the array[30]. I was supposed to use just array. Thanks!

Take it to the Xtreme!

Wachar's Eternity <-<-<-<-<- Me own site!

This topic is closed to new replies.

Advertisement