-2

I want to prompt the user for 9 integers between 1 and 9, and I want to output whether the integers are valid or invalid, depending on whether there is one of each number or not. Here is my code now:

#include <iostream>
#include "conio.h"
using namespace std;

int main(){
int sudoku[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int input;
int check[9];
for (int i = 0; i < 9; i++){
    cout << "Enter 9 digits between 1 and 9: ";
    cin >> input;
    if (input > 1 || input > 9){
        cout << "incorrect";
    }
    check[input-1]++;
}
if (check != { 1, 2, 3, 4, 5, 6, 7, 8, 9 }){
    cout << "incorrect";
}
else if (check == {1, 2, 3, 4, 5, 6, 7, 8, 9}){
    cout << "correct";
}
_getch();
return 0;
}        

So I'm pretty sure I know what's wrong: The check array. However, I don't know how to fix it. Any advice? I'm a beginner, so I don't know a lot of code.

EDIT: Sorry, I'm not sure what kind of question I should ask instead on stack overflow. What I wanted to know was how to check if arrays contain numbers in the exact order.

Stephanie Fu
  • 79
  • 1
  • 9
  • 1
    I know it can be frustrating being a beginner, but "how do I make my code work?" questions like this just aren't on-topic here. – Crowman Nov 02 '14 at 00:22
  • First thing - when the user correctly enters nine different numbers, the resulting `check[]` would be `{1, 1, ..., 1}`, not `{1, 2, ..., 9}`. Second thing - You have to compare the array element by element, not `check == { ... }` way. – Roman Hocke Nov 02 '14 at 00:25

1 Answers1

1

Three problems:

if (input > 1 || input > 9)

This checks if input is larger than 1 or larger than 9, nonsense.

Second

check[input-1]++;

check is array of uninitialized values and if you increment some indices by 1 then the result would be {1,1,1,1,1,1,1,1,1} if you are lucky, anything else otherwise. You need to initialize values to something first.

Third

check is array, if you compare it with something you will just compare it's pointer. You need to iterate over it and check if it contains what you need. Related question here

Community
  • 1
  • 1
Kyborek
  • 1,519
  • 11
  • 20