user740858
user740858

Reputation:

Array is not printing out through cout

I am running into some trouble trying to print out this code. I have a function that generates a search space with ranges 1 to 8. But when I try to output it, the program quits on me.

#include <iostream>;
using namespace std;

char yOrN;
int answer;
const int LENGTH=4096;
int guess[LENGTH];

void searchspace(int guesses[],int length){
int count = 0;
for(int i=1; i=8;i++){
    for(int j=1; j=8; j++){
        for(int k=1;k=8;k++){
            for(int l=1;l=8;l++){
                guesses[count]=1000*i+100*j+10*k+l;
                count++;
            }
        }
    }
}
}

int main(){
searchspace(guess,LENGTH);
for(int i = 0; i<4096;i++){
    cout<<guess[i]<<endl;
}
 }

Upvotes: 0

Views: 87

Answers (3)

Roman
Roman

Reputation: 1867

Fixed your code:

  • for loops
  • yOrn , answer were not used, hence removed
  • removed ';' after #include

here it is:

#include <iostream>
using namespace std;

const int LENGTH=4096;
int guess[LENGTH];

void searchspace(int guesses[],int length){
    int count = 0;
    for(int i=1; i<=8;i++){
        for(int j=1; j<=8; j++){
            for(int k=1;k<=8;k++){
                for(int l=1;l<=8;l++){
                    guesses[count]=1000*i+100*j+10*k+l;
                    count++;
                }
            }
        }
    }
}

int main(){
    searchspace(guess,LENGTH);
    for(int i = 0; i<4096;i++){
        cout<<guess[i]<<endl;
    }
}

I compiled and ran it - works perfectly ( at least I assume that is what you wanted to do).

Upvotes: 0

Paul R
Paul R

Reputation: 213059

Your for loops are all wrong, e.g. you need to change:

for(int i=1; i=8;i++){

to

for(int i=1; i<=8;i++){

and similarly for the others.

Upvotes: 2

Luchian Grigore
Luchian Grigore

Reputation: 258618

This loop (and the others)

for(int j=1; j=8; j++)

finishes when j=8 evaluates to true. Which is always.

Did you mean:

for(int j=1; j<=8; j++)

Upvotes: 4

Related Questions