First of all, i need to say, that i am a complete noob. I am trying to make some naval combat simulation to do that i created a random letter and number generator Here's the code. By the way, what i want to achieve is to have only one variable (Guess) to be confronted with the ship places that the user specified.
char letters[]= {'A','B','C','D','E','F','G','H','I','L'};
\\ lots of code
//RandomAI
int G = rand() % 10 + 1;
int nOut = rand() % 10 + 1;
char lOut = letters[G];
string Guess = lOut + nOut;
return 0;
答案 0 :(得分:3)
string Guess = lOut + nOut;
adds an int
and char
types which does not produce a std::string
. One way to address this is to create a string and then append to it:
std::string guess = lOut + std::to_string(nOut);
This will solve your compiler error, but you still have a logic error here:
int G = rand() % 10 + 1;
rand() % 10 + 1
will produce a value between 1 and 10 inclusive. You want a number between 0 and 9 inclusive, because indices in C++ begin at 0, not 1. So drop the +1
portion:
int G = rand() % 10;
Otherwise you may accidentally attempt to access an out-of-bounds index in letters
答案 1 :(得分:0)
假设您想要一个类似'A7'或'F2'的输出字符串,实现此目的的一种方法是将所有内容转换为string
(因为您无法添加int
和{{ {1}})。对于那些无法访问C ++ 11和char
的人,您可以使用:
std::to_string()