Jump to content
  • Advertisement
Sign in to follow this  
Arcand

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

#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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!