Sign in to follow this  
Arcand

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

Recommended Posts

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

[CODE]
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;
}
[/CODE]

Share this post


Link to post
Share on other sites
Serapth    6671
[CODE]
#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;
}
[/CODE]

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
JTippetts    12950
Also, I personally advise you to steer clear of C-style strings.

[code]

#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;
}
[/code]

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
Serapth    6671
[quote name='JTippetts' timestamp='1341844935' post='4957289']
Also, I personally advise you to steer clear of C-style strings.

[code]

#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;
}
[/code]

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.
[/quote]

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:
[CODE]
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;
}
[/CODE] Edited by Serapth

Share this post


Link to post
Share on other sites
Serapth    6671
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
DZee    194
[quote name='Serapth' timestamp='1341852191' post='4957320']
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.
[/quote]

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

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