Public Group

# looks like I remember less than I thought... *smiles*

This topic is 2348 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

in this code segment im getting an invalid conversion error on everything from ships[1][1] to ships[5][1]... why is this?

 using namespace std; #include <iostream> #include <string> int main() { const int CLASS = 6; const int NAME = 15; char ships[CLASS][NAME]; ships[1][1] = "Frigate"; ships[2][1] = "Destroyer"; ships[3][1] = "Cruiser"; ships[4][1] = "Battleshop"; ships[5][1] = "Dreadnaught"; cout << "Ship types as follows:\n"; cout << ships[1] << endl << ships[2] << endl << ships[3] << endl << ships[4] << endl << ships[5] << endl; cin.get(); return 0; } 

##### Share on other sites
It's because ships[] is a multi-dimensional array of char, not string, but you're trying to assign strings to the elements.

awesome thx man

##### Share on other sites
 #include <iostream> #include <string> using namespace std; int main() { const int CLASS = 6; const int NAME = 15; char ships[CLASS][NAME]; strcpy(ships[0],"Frigate"); strcpy(ships[1],"Destroyer"); strcpy(ships[2],"Cruiser"); strcpy(ships[3],"Battleship"); strcpy(ships[4],"Dreadnaught"); cout << "Ship types as follows:\n"; cout << ships[0] << endl << ships[1] << endl << ships[2] << endl << ships[3] << endl << ships[4] << endl; cin.get(); return 0; } 

That version works.

On very key mistake you have made, C based arrays start from 0. The only reason it didn't crash is because you CLASS value is 1 too high ( it should be 5 ). Edited by Serapth

##### Share on other sites
Also, I personally advise you to steer clear of C-style strings.

 #include <iostream> #include <string> using namespace std; int main() { const int CLASS = 6; const int NAME = 15; std::string ships[CLASS]; ships[1] = "Frigate"; ships[2] = "Destroyer"; ships[3] = "Cruiser"; ships[4] = "Battleshop"; ships[5] = "Dreadnaught"; cout << "Ship types as follows:\n"; cout << ships[1] << endl << ships[2] << endl << ships[3] << endl << ships[4] << endl << ships[5] << endl; cin.get(); return 0; } 

When you throw C-strings into the mix, you have to worry about buffer overruns and other flaws. For example, what if you add another ship type "Heavy Battlecruiser"? I'll tell you what happens. *STOMP* *STOMP* goes your memory, since the elements can only hold 15 chars. Such considerations disappear if you use a proper string class.

##### Share on other sites

Also, I personally advise you to steer clear of C-style strings.

 #include <iostream> #include <string> using namespace std; int main() { const int CLASS = 6; const int NAME = 15; std::string ships[CLASS]; ships[1] = "Frigate"; ships[2] = "Destroyer"; ships[3] = "Cruiser"; ships[4] = "Battleshop"; ships[5] = "Dreadnaught"; cout << "Ship types as follows:\n"; cout << ships[1] << endl << ships[2] << endl << ships[3] << endl << ships[4] << endl << ships[5] << endl; cin.get(); return 0; } 

When you throw C-strings into the mix, you have to worry about buffer overruns and other flaws. For example, what if you add another ship type "Heavy Battlecruiser"? I'll tell you what happens. *STOMP* *STOMP* goes your memory, since the elements can only hold 15 chars. Such considerations disappear if you use a proper string class.

I thoroughly endorse this message. I simply modified the sample source to work, not because it was a good way to do things, but because that was the approach he started with.

In general, stay the heck away from C style, well, everything if you can help it.

EDIT: JTippetts, how did you fix my rogue tab?

Cleaner, safer, modern C++, not answering the OP's question way of doing the same thing:
 int main() { const std::string ships[] = { "Frigate","Destroyer","Cruiser","Battleship","Dreadnaught" }; std::cout << "Ship types as follows:\n"; std::cout << ships[0] << std::endl << ships[1] << std::endl << ships[2] << std::endl << ships[3] << std::endl << ships[4] << std::endl; std::cin.get(); return 0; }  Edited by Serapth

##### Share on other sites
lua arrays ( well tables ) start from 1, and I would consider it a grown man's language.

I would also consider it a mistake on the lua designers behalf, but that's a completely different conversation.

##### Share on other sites

lua arrays ( well tables ) start from 1, and I would consider it a grown man's language.

I would also consider it a mistake on the lua designers behalf, but that's a completely different conversation.

Lol yeah, I can recall it's not the only language that has an index start of one. Although I agree, should be in an ISO standard somewhere for defining new languages. It's unnecessary trouble for us.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633708
• Total Posts
3013468
×