Hmmm, array help =)

Started by
9 comments, last by ThomasSauder 21 years, 5 months ago
Hey, I'll get right to the point. I have a function that returns either 1, 2, or 3, now I want to convert that information (1, 2 or 3) to words. This is my code to do so, but I can't seem to be able to create an allocated array eg. char *pClass = new char Class[]; Well here is error: GameCore.cpp c:\monsterfight\darkterrorult\gamecore.cpp(7) : error C2143: syntax error : missing ';' before '=' c:\monsterfight\darkterrorult\gamecore.cpp(15) : error C2440: '=' : cannot convert from 'char [7]' to 'char' This conversion requires a reinterpret_cast, a C-style cast or function-style cast c:\monsterfight\darkterrorult\gamecore.cpp(17) : error C2440: '=' : cannot convert from 'char [7]' to 'char' This conversion requires a reinterpret_cast, a C-style cast or function-style cast c:\monsterfight\darkterrorult\gamecore.cpp(19) : error C2440: '=' : cannot convert from 'char [10]' to 'char' This conversion requires a reinterpret_cast, a C-style cast or function-style cast Error executing cl.exe. DarkTerrorUlt.exe - 4 error(s), 0 warning(s) Code: #include <iostream> #include "GameCore.h" #include "CharacterChoice.h" int GameCore() { char *pClass; = new char Class[]; int PlayerType; using namespace std; cout << " \nWelcome Adventurer. The world of Kaar\n"; cout << "is in jeapordy. They are getting attacked by unkown\n"; cout << "creatures, what class are you, by the way?\n\n\n"; PlayerType = CharacterChoice(); if (PlayerType = 1) *pClass = "Wizard"; if (PlayerType = 2) *pClass = "Archer"; if (PlayerType = 3) *pClass = "Swordsman"; cout << "Ah, it is clear that you are the mighty " << *pClass << "\n\n"; cout << "Well, none the less, the road will be hard. You must find out\n\n"; cout << "Who and what is making our world in despare. Good luck!\n"; return PlayerType; } Thanks a lot!!!! edit: fixed 2 errors [edited by - ThomasSauder on November 6, 2002 10:36:14 PM]
Advertisement
The reason why I''m asknig the community this question is because I learn by trial and error. I try and learn from books, and apply it without looking. So Thanks for all your answers, any suggestions are welcomed.

Hopefully once I fully understand this type of allocating memory with arrays and pointers, nothing can stop me.... (Other than syntax)

Thomas Sauder
When dynamically allocating memory with new, you are given a pointer to an array of data formatted by the specified type. You must also provide how many elements you wish to reserve (which can either be a constant or, more interestingly, a variable).

int *pClass = new char Class[Class_Name_Lenght];

Though in the case you presented us with, it would be easier to simply use a static string instead of a dynamic one.

char ClassName[MAX_LENGTH];

Not only will it be much easier to work with, you won''t have to worry about as many things as with a dynamically allocated section of memory, such as making sure to deallocate when you''re done and destroying its pointer.
didn''t help at all.....

Hmmm.. I can''t figure this out
char *pClass; = new char Class[];


for starters, get rid of that first semicolon.
I think the problem is that you have two types after new when you only really want one. What you should have is
char * pClass=new char[];


Apparently my sig must use proper programming.
srand(time(NULL));
int i=rand()%100;
PickWittyQuote(i);
"Pfft, Facts! Facts can be used to prove anything!" -- Homer J. Simpson
Remove the comma and "Class" where you allocate memory.

Also, you must specify the amount of memory you want allocated (in this case, the least it should be is the number of bytes for the length of the class with most letters, read characters, in it, plus add another byte to hold the string terminator (NULL char) at the end of the string).

Like this:

char *pClass = new char[100];

Another problem is when using array of chars you can''t just assign a string to the array:

*pClass = "Wizzard"; // this is erronous code

The correct way is to use strcpy():

strcpy( pClass, "Wizzard" );

If you want to merge two strings you would use the following:

strcat( pClass, " is dangerous. Watch out!" );

// pClass now contains ''Wizzard is dangerous. Watch out!\0''

To make another point if you want to compare two array of characters you cannot use the "==" equation form but you have to use strcmp() which returns 0 if the strings are equal.
strcpy( pClass, "wizzard" );if( 0 == strcmp( pClass, "wizzard" ) )  {  cout << "Excellent. We got ourselves equal strings";  } 

That''s about all you need to know about array characters at the moment.

By the way, if you want to test if something is equal to something else you use "==" and if you want to assign something to something else you use "=".

e.g.

int myInt;

myInt = 5; // assignment; see ''=''

if( myInt == 5 ) // equality test; see ''==''
.. yes myInt holds the value of 5

Now go back to where you test for your player type and change those assignments to equality tests.

-Viktor
Well, thanks to the last post. I knew about ==, I just wasn''t too clear on declaring new char[100] to *pClass. Thanks for the quick answers!

Thomas Sauder
Using strings in c++ is easier to do like this:

    #include <iostream>#include <string>int main(void) {  string some_text("This is a test string!");  some_text += " Text is like cool or something.";  cout << some_text << endl;  if(string("asdf")==string("asdf"))   cout << "strings are the same." << endl;    if(string("foo")==string("bar"))   cout << "strings are different." << endl;  return 0; }  


quote:*pClass = "Swordsman";

You are throwing away the pointer to the data you alocated, and pointing to the constant. This is a memory leak!!!

You could leave this line as is, and not bother allocating memory before, but you must not modify the string (your program will either crash or every thing that says "Swordsman" will change also), and you must not try to free/delete it.

If you want to have your own copy of the string, you could use pClass = strdup("Swordsman"); which will duplicate a string, but you must call free(pClass); when you are done with it, or again you will have a memory leak.

[edited by - smart_idiot on November 7, 2002 10:09:38 AM]
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
how do you do that code box with the syntax hilighting???


-= Twisted Matrix =-
- Twisted Matrix

This topic is closed to new replies.

Advertisement