简体   繁体   中英

C++ Help. Arrays not working with integers

Here is my code: (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.

When I change all the "f"'s with [1] to test the code, it works. But when I use "f"'s instead, it returns a runtime error.

Not sure if that is my compiler. But hey - I am a 2 day old C++ coder.

Your sentence is 9 "slots" big (addressed as sentence[0] to sentence[8] ). You try to put something in the 10th slot ( sentence[9] ), which is a no-no.

(This pattern is repeated below with word .)

You most likely want to declare those arrays as 10-element ones.

That's because sentence and word contain nine units. But rand()%10 will produce 9 , when you use word[f] = sentence[f] , word[9] and sentence[9] are out of range. word[9] is the 10th element of array 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

char foo[2];

declares two characters. However, they are numbered 0 and 1, thus

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.

The second problem is that you declare 'f' twice.

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 (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.

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). ideone live demo here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM