//Function Draw Car
void carinitialize ()
{
char carimage[2][3]={{ '-','-','-'},
{'|','_','_'}};
int row=0,
int col=0;
for (row=0;row<2;row++)
{
for (col=0;col<3;col++)
{
cout<<carimage[row][col];
}
}
}
either put a semi-colon on line 8 or remove the int on line 9. Also you don't exactly need the second for loop character arrays have a special operator << where you can output the entire array.
Oh yes, I now that icN use string, but I wanted to try a different method and work on a different approach from something I'm not aware of. In the main function, I would probably declare an array. How can I add parameters to this void function?
A null terminator is a null (binary value of 0) character appended to the end of a string to mark the end of the string. Without it... when you pass an array of characters to cout or to any function which takes a C string... it has no way to know where the string data ends.
Adding a newline will not work because a newline is not a null terminator.
null terminator means that the string ends with a null character '\0' or 0 in ascii. Also what do you mean you can't perform it? I can assume the problem is that you have to dynamically create it AFAIK since those types of arrays the compiler needs to know the size ahead of time so it can reserve the space.
*edit guess I took to long typing disch beat me :P
Thanks for your responses giblet and disch!!! Definitely learning more stuff through you guys. Once again thank you!!! One more question, I have been looking over strings and c-strings for the past few days. My professor told me strings are better then character arrays to use. Is this true, because im finding c-strings more eaiser to use?
He probably means because strings have methods you can use on them and algorithms without having to make them yourself. Other than that they are about the same speed (char array might be maybe a usecond faster).
One more question, I have been looking over strings and c-strings for the past few days. My professor told me strings are better then character arrays to use. Is this true, because im finding c-strings more eaiser to use?
There are pros and cons to each. std::string typically easier to use because:
1) It handles memory management for you (don't have to worry about freeing up dynamically allocated memory)
2) It can hold a string of any size (wheres with a char array, you have a maximum cap before you need to reallocate a larger buffer)
3) There is less risk of overflowing. IE:
1 2 3 4 5 6 7
char bad[] = "foo"; // fine
strcpy( bad, "example" ); // BAD, memory corruption! Possible program explosion!
// vs.
string good = "foo"; // fine
good = "example"; // also fine
Basically the benefit to string is that you don't have to worry about how big the string data is... a string will always be able to hold it. You also don't have to worry about allocation and cleanup or bad pointers.
So yeah.. strings are generally safer and easier. I'm a little weirded out that you think char arrays are easier. In fact, you questions in this thread suggest otherwise (ie: not knowing about null terminators, not knowing how/why outputting carimage[rows] would work, etc). There are a lot of subtleties with char arrays that if you get them wrong, things can go very, very bad. But with std::string you usually don't have to worry about them.