之前和之后我开始在MS Studio中编程,我在CodeBloks中使用C ++进行了一些编程。目前,我正在编写一本小游戏,编写:编程:使用C ++的原理和实践。第5章的练习12。 我一直在互联网上寻找固定范围内存错误,它会一直发生我尝试的事情(参见printscreen链接)。 有关该程序的描述在代码本身中。 我也确实使矢量成为硬编码(如果我说的那么正确)的大小,使程序停止抱怨他们的内存范围错误。
注意:是的,这也是学校的练习/练习。但我宁愿问我做错了什么,然后从互联网上复制粘贴代码..
请查看我的代码并查看或者我对我的向量犯了一些错误,因为这是我的错误可能来自的地方。
更新:在本主题中作为答案放置。 Link to update
错误:
Unhandled exception at 0x7529CBC2 in Tweede project.exe: Microsoft C++ exception: Range_error at memory location 0x006FF510.
程序:
#include "std_lib_facilities.h"
int main(){
bool while_bit = false;
vector<int>bulls_cows(4);
vector<int>inputs_arr(4);
//Generate 4 random numbers.
for (int a = 0; a < 4; ++a){
bulls_cows[a] = a + 1;//randint(a) % 9 + 0; //random number between 0 and 9.
}
//bulls_cows[1] = randint(10);
cout << "For this game, called Bulls and Cows, you have to guess the four right numbers between 0 and 9.\n";
cout << "When one or more digets are right and in the right position the program will say the number of Bulls.\n";
cout << "When one or more digets are right but not in the right position the program will say the number of Cows.\n";
cout << "Please enter 4 number by filling in ONE positive diget and press enter. Do this four times and wait for the result.\n";
for (int z = 0; z < 4; ++z) {
cout << bulls_cows[z] << "\n";
}
while (while_bit == false) {
int input = 0; //Reset of input, cows and bulls every round.
int cows = 0;
int bulls = 0;
cin >> input;
//Test for legit input. If legit then it writes it to the array called "input_arr"
if (input < 0 || input > 9) {
cout << "Number must be between 0 and 9.\n";
}
else {
inputs_arr.push_back(input);
}
//Check or 4 numbers have been given.
if (sizeof(inputs_arr) < 4) {
//Check for equal numbers on same spot.
for (int b = 0; b < 4; ++b) {
if (inputs_arr[b] == bulls_cows[b]) {
bulls + 1;
}
}
//Check for a number in all spots.
for (int c = 0; c < 4; ++c) {
if (inputs_arr[c] == bulls_cows[0] || bulls_cows[1] || bulls_cows[2] || bulls_cows[3]) {
cows +1;
}
}
}
if (bulls < 4) {
cout << "You did not guess the right combination.\n";
cout << "Number of bulls: " << bulls << "\n";
cout << "Number of cows: " << cows << "\n";
inputs_arr[0, 0, 0, 0]; //Empty array.
}
if (bulls == 4) {
cout << "You guessed the right combination!\n";
while_bit = true;
}
}
keep_window_open(); //To keep terminal open since MS Studio doesn't itself.
return 0;
}
答案 0 :(得分:0)
将sizeof
运算符与inputs_array
向量一起使用将为您提供该对象在内存中的字节大小。也许您之前已经将它用于数组。
相反,我认为你想要推送到矢量中的项目数量。 std::vector
有一个名为size
的函数,它提供了推送给它的项目数。
答案 1 :(得分:0)
在Visual Studio中,您应该真的将警告级别设置为级别4.然后编译器会告诉您有关您尝试做的几件奇怪的事情。
例如:
warning C4552: '+': operator has no effect; expected operator with side-effect 1>
bulls + 1;
您计算结果,但从不将其存储在任何地方。奶牛也一样。
error C4548: expression before comma has no effect; expected expression with side-effect
inputs_arr[0, 0, 0, 0]; //Empty array.
这是不清空数组的方法。要删除所有元素,请执行inputs_arr.clear();
。
warning C4127: conditional expression is constant
if (sizeof(inputs_arr) < 4) {
就像我们已经看到的那样,sizeof
是矢量对象的大小(总是相同的),而不是它所拥有的元素的数量。
然后有一些逻辑错误。
vector<int>inputs_arr(4);
这会创建一个最初持有4个整数的向量。
但是后来你做的时候
inputs_arr.push_back(input);
它将更多元素添加到向量中,所以现在它可能最多可容纳8个整数。您必须先确定是否要使用完整尺寸创建它,或者在进行时添加元素。但不是两个。
另一个问题是条件
if (inputs_arr[c] == bulls_cows[0] || bulls_cows[1] || bulls_cows[2] || bulls_cows[3]) {
即使是语言,您可以在其中编写类似于此的条件,但在C ++中则不能。要将变量x
与其他几个值进行比较,您必须将其拼写出来:
if (x == y || x == z || x == w)
答案 2 :(得分:0)
更新:(不确定或者我必须将其添加到问题或答案中。如果错误,请告诉我。我是Stack的新手;)
似乎某些语句/循环括号未正确放置。我编辑了一下: 可悲的是,似乎我的中间人牛和公牛没有在第61和62行重置。任何人都知道我可能在那里做错了。
日志:
cin并没有循环。还编辑了else-statement&quot; inputs_arr.pushback(输入)&#39;并添加了它下面的所有for循环和语句。修复了一部分cin问题。将公牛,母牛和输入整数替换为程序顶部,并在false / if语句结束时重置。
将所有循环和语句放在if语句下面(#b = 0; b&lt; 4; ++ b)&#39;进入它。
编辑for循环到&gt; 4 couts&#34;数组大小&#34;看看它有什么价值。稍后删除,因为它仅用于测试。
#include "std_lib_facilities.h"
int main()
{
bool while_bit = false;
int input = 0;
int cows = 0;
int bulls = 0;
vector<int> bulls_cows(4); //size needed to prevent memory size error at line 11.
vector<int> inputs_arr;
//Generate 4 random numbers. Need to add seed later.
for (int a = 0; a < 4; ++a){
bulls_cows[a] = randint(a) % 9 + 0; //random number between 0 and 9.
}
cout << "For this game, called Bulls and Cows, you have to guess the four right numbers between 0 and 9.\n";
cout << "When one or more digets are right and in the right position the program will say the number of Bulls.\n";
cout << "When one or more digets are right but not in the right position the program will say the number of Cows.\n";
cout << "Please enter 4 numbers by filling in ONE positive diget and press enter. Do this four times and wait for the result.\n";
for (int z = 0; z < 4; ++z) { //Gives the generated numbers for testing.
cout << bulls_cows[z] << "\n";
}
while (while_bit == false) {
cin >> input;
//Test for legit input. If legit then it writes it to the array called "input_arr"
if (input < 0 || input > 9) {
cout << "Number must be between 0 and 9.\n";
}
else {
inputs_arr.push_back(input);
//Check or 4 numbers have been given.
if (inputs_arr.size() > 3) {
//Check for equal numbers on same spot.
for (int b = 0; b < 4; ++b) {
if (inputs_arr[b] == bulls_cows[b]) {
++bulls;
}
}
//Check for a number in all spots.
for (int c = 0; c < 4; ++c) {
if (inputs_arr[c] == bulls_cows[0] || inputs_arr[c] == bulls_cows[1] || inputs_arr[c] == bulls_cows[2] || inputs_arr[c] == bulls_cows[3]) {
++cows;
}
}
/*for (int x = 0; x < 4; ++x) { //Couts again the fresh entered numbers for a better overview.
cout << "Size of array: " << inputs_arr[x] << "\n";
}*/
if (bulls < 4) {
cout << "You did not guess the right combination.\n";
cout << "Number of bulls: " << bulls << "\n";
cout << "Number of cows: " << cows << "\n";
int cows = 0; //Reset of cows and bulls each round.
int bulls = 0;
inputs_arr.clear(); //Empty the array.
cout << "Please enter 4 numbers:\n";
}
if (bulls == 4) {
cout << "You guessed the right combination!\n";
while_bit = true;
}
}
}
}
keep_window_open(); //To keep terminal open since MS Studio doesn't itself.
return 0;
}