[英]C++ Help. Arrays not working with integers
Here is my code: (C++) 这是我的代码:(C ++)
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
string sentence[9];
string word[9];
inb b[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int f = 0;
for (int i = 1; i <= 10; i += 1){
cin >> sentence[i - 1];
}
for (int a = 10; a > 1; a = a - b[f]){
b[f] = 0;
int f = rand() % 10;
b[f] = 1;
word[f] = sentence[f];
cout << world [f] << endl;
}
}
However, when I run this I get a "runtime error". 但是,当我运行此程序时,出现“运行时错误”。 That's it, no line, no further error.
就是这样,没有一行,没有进一步的错误。 Nothing.
没有。
The Arrays in the bottom side of the code, like word[f] and b[f] do not work if I use f inside the "[]"'s. 如果我在“ []”的内部使用f,则代码底部的数组(如word [f]和b [f])将不起作用。
When I change all the "f"'s with [1] to test the code, it works. 当我用[1]更改所有“ f”以测试代码时,它可以工作。 But when I use "f"'s instead, it returns a runtime error.
但是,当我使用“ f”代替时,它将返回运行时错误。
Not sure if that is my compiler. 不知道那是我的编译器。 But hey - I am a 2 day old C++ coder.
但是,嘿-我是2天大的C ++编码器。
Your sentence
is 9 "slots" big (addressed as sentence[0]
to sentence[8]
). 您的
sentence
大了9个“插槽”(地址为sentence[0]
至sentence[8]
)。 You try to put something in the 10th slot ( sentence[9]
), which is a no-no. 您尝试在第10个插槽中放入某些内容(
sentence[9]
),这是禁止的。
(This pattern is repeated below with word
.) (此模式在下面用
word
重复。)
You most likely want to declare those arrays as 10-element ones. 您最有可能希望将这些数组声明为10个元素的数组。
That's because sentence
and word
contain nine units. 这是因为
sentence
和word
包含9个单位。 But rand()%10
will produce 9
, when you use word[f] = sentence[f]
, word[9] and sentence[9] are out of range. 但是,当您使用
word[f] = sentence[f]
, rand()%10
将产生9
,而word [9]和句子[9]不在范围内。 word[9]
is the 10th element of array word
. word[9]
是数组word
的第十个元素。
There are several problems with your code. 您的代码有几个问题。 Firstly, sentence and word only have 9 entries, but you try to use 10. Array declaration is 1-based, eg
首先,句子和单词只有9个条目,但是您尝试使用10。数组声明是基于1的,例如
char foo[2]; char foo [2];
declares two characters. 声明两个字符。 However, they are numbered 0 and 1, thus
但是,它们的编号为0和1,因此
char foo[2];
foo[0] = 'a'; //valid
foo[1] = 'b'; //valid
foo[2] = 'c'; //very bad.
this problem might be obfuscated for you by the fact that you are making 'b' an auto-sized array. 由于您正在使“ b”成为自动调整大小的数组,这一问题可能会使您感到困惑。
The second problem is that you declare 'f' twice. 第二个问题是您两次声明“ f”。
int f = 0;
for (int i = 1; i <= 10; i += 1){
and inside the loop 在循环内
int f = rand() % 10;
b[f] = 1;
Your for loop, then, is broken: 那么,您的for循环已损坏:
for (int a = 10; a > 1; a = a - b[f]){ for(int a = 10; a> 1; a = a-b [f]){
it uses the external 'f', which is always 0, to access element zero of b and subtract that from a. 它使用始终为0的外部“ f”访问b的元素零,并从a中减去该元素。
Here is how I would write the code you're trying to write: 这是我要编写的代码:
I honestly don't understand what your code is supposed to do, but here is how I might write a simpler version of the same thing: 老实说,我不明白您的代码应该做什么,但是这是我可能会写一个更简单的版本的方法:
#include <iostream>
#include <stdlib.h>
#include <array>
//using namespace std; <-- don't do this.
int main(){
std::array<std::string, 10> sentence; // ten strings
// populate the array of sentences.
for (size_t i = 0; i < sentence.size(); ++i) { // use ++ when ++ is what you mean.
std::cin >> sentence[i];
}
for (size_t i = 0; i < sentence.size(); ++i) {
size_t f = rand() % sentence.size(); // returns a value 0-9
std::cout << sentence[f] << " ";
}
std::cout << std::endl;
}
Requires C++11 (-std=c++11 compiler option). 需要C ++ 11(-std = c ++ 11编译器选项)。 ideone live demo here
ideone现场演示在这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.