Sign in to follow this  
frogtag

String Arrays???

Recommended Posts

i'm using Dev-c++ and OpenGL. i'm trying to create an array of strings but can't get it to work. I've got reference books but they aren't any help because they use the <string.h> header file, but that won't work within my OpenGL program, just an msdos console window. what i'm trying to do is create a function that adds a string to a list (array), via... ... void ConsoleList("This will be the new line"); ... What i'm trying to get the function to do is add the new line of text to the next available slot in the array. something like this... ... void ConsoleList(char* newline) { ConList[128][numberoflines] = newline; numberoflines++; } ... the associated lines in the header file relating to the function... ... void ConsoleList(char*); int numberoflines = 0; char ConList[128][20] // where 128 is the string length and 20 the array length ... Then when i'm ready use a for loop to display the array. The problem is it doesn't work. i get the message " invalid conversion from 'char*' to 'char' " or similar if i swap stuff around. This is probably very simple but can anyone help?

Share this post


Link to post
Share on other sites
In all my studying so far, I've picked up that any reference that refers to an include list that has a ".h" in it is outdated (deprecated), as the current C++ standard just uses the filename minus extension, as in Omid's example code.

Share this post


Link to post
Share on other sites
Concerning the headers. For the backwards-compatible C library string.h file (defining strlen, strcat, strcpy...), the C++ header to include is cstring. For the prestandard C++ string.h header, which is not mentioned by the standard at all (and thus, has only been kept as legacy by compilers since 1998), the standard C++ header to include is string. If you include string.h on one of today's compilers, I don't really know which of the two aforementioned legacy headers will be included, if any.

Concerning your design. This is not C, so you can and should encapsulate the behaviour of your objects correctly. Actually, you should have done that in C as well. In particular, the "available slots technique" is awkward and annoying to use in C++. You should use an object where only used lines are stored, and new lines are appended to the end.

Concerning your problem. std::vector and std::string correspond to your problem almost perfectly. An example of usage:


#include <vector>
#include <string>

typedef std::vector<std::string> console;
console conList[256];

void ConsoleList(const std::string& newLine)
{
conList[128].push_back(newLine);
}



Concerning your IDE: Dev-C++ is old and outdated. It has not been supported or maintaned for years. Freely available and superior alternatives are available, such as Visual C++ 2005 Express Edition, which is downloadable from Microsoft for free.

Concerning your design: you are using global variables. You will learn, in time, the havoc they wreak on your code and the rigidity they impose on it. Or so I hope. Nevertheless, even if you wish to keep global data, it would be in your best interest to encapsulate related global variables into single global objects. A freestanding function, such as ConsoleList, which alters unprotected global data, is a clear sign of danger in any C++ program, no matter the size. I strongly suggest that you group the global variables as non-static class members, create member functions to operate on them, and create a global instance of the global classes.

Concerning const-correctness. An argument type of char* or char[], as opposed to an argument type of const char* or const char[], clearly indicates that the function can and probabily will modify the data passed to it. When this happens, you should never pass a string literal to such a function, since modifying string literals can result in undefined behaviour, including random crashes, at runtime.

About your array indicing. In C++, an array is indexed starting at 0. This means that for an array of size 128, element 128 is invalid (the last accessible element being at index 127).

Share this post


Link to post
Share on other sites
The stuff you've all said to day has pointed me in the direction of vectors which i have looked up and using the code supplied instigated what, i think, i need. However i can't display it on the screen. I'm using the font routine supplied my nehe, which uses the glCallLists command. However, everytime i try to display the vector list i'm denied?
Can anyone tell me how to print/display the vector list?

Share this post


Link to post
Share on other sites
Quote:
Original post by frogtag
However, everytime i try to display the vector list i'm denied?


Please provide the exact error message, along with the code that caused it.

To display a vector of strings, you iterate through it like for any other array and display individual strings. To display a string, you usually call a function from one library or another. If the function has a C signature (taking a const char*), you'll have to use std::string::c_str() to gain access to a C-string representation.



Share this post


Link to post
Share on other sites
the error is...

cannot pass objects of non-POD type 'class console' throught '...'

the code...

#include <vector>
#include <string>

typedef std::vector<std::string> console;
console conList[256];

void ConsoleList(const std::char* newLine)
{
conList[128].push_back(newLine);
}

the '...' bit i think is in response to the char* fmt... command within the print command uses. If i bypass that and drectly use...

glCallLists(strlen(ConList[0]), BYTE, ConList[0]);

is get a similar error.

Share this post


Link to post
Share on other sites
Quote:
Original post by frogtag
void ConsoleList(const std::char* newLine)
{
conList[128].push_back(newLine);
}


That should be std::string.

As for the glCallLists, you should probably use:

glCallLists(line.size(),BYTE,line.c_str());

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Quote:
Original post by frogtag
void ConsoleList(const std::char* newLine)
{
conList[128].push_back(newLine);
}


That should be std::string.

As for the glCallLists, you should probably use:

glCallLists(line.size(),BYTE,line.c_str());


Don't forget to make it a const std::string reference '&' instead of a pointer '*':


void ConsoleList(const std::string & newLine) { ...

Share this post


Link to post
Share on other sites
Quote:
Don't forget to make it a const std::string reference '&' instead of a pointer '*'


The '*' was part of the bolded characters. I should've used underlining instead [smile]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this