Multiple inputs in 1 line

I'm starting a new project that will solve Sudoku puzzles for me. I just started, I'm curious on how could I get the user to enter input for 9 different variables and store them in an array using the same line.
Ex. The user can enter 12345 vs
1
2
3
4
5


My program currently has 2 simple functions. 1 for input and 1 for output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

void output(int ** array);
void input(int ** array);


/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) 
{
    int** array;
    array = new int*[9];
    for (int i=0;i<9;++i)
        array[i] = new int[9];
    input(array);
    output(array);
    delete []array;
    std::cin.ignore();
    getchar();
    return 0;
	
	return 0;
}

void output(int ** array)
{
    for (int i= 0;i<9;++i)
    {
        for (int a= 0;a<9;++a)
        {
            std::cout << "Array[" << i << "][" << a << "] = " << array[a][i] << std::endl;
        }
    }
}

void input(int ** array)
{
    for (int i= 0;i<9;++i)
    {
        for (int a= 0;a<9;++a)
        {
            std::cout << "Array[" << i << "][" << a << "] = " << std::endl;
            std::cin >> array[i][a];
        }
    }
}
Well. If you use cin. cin takes any value up to the first whitespace basically. So if you input 1 2 3 4 5 6 7 8 9 with spaces. It will read the first number into the first variable and continue to the next because the rest of the numbers are still in the stream ready to be used.
Ahhh, I forgot that and I still used cin.ignore() in my code. I guess I will use this method for now.

Maybe you could help me with another part of my code.
The input and output works as it should, except for when I enter the same repetition for each row.
Ex: I want to enter "1 2 3 4 5 6 7 8 9" for array[0][0] through array[0][8]. I proceed and do this for each row. This outputs 1s for [0][0] through [0][8], 2s for [1][0] through [1][8], 3s for [2][0] through [2][8], and the pattern continues as stated
Last edited on
You can read input to a string using getline(cin, string) function in your "input" function and then parse it using stringstream to your array
1
2
3
4
5
6
7
8
string str = "1 2 3 4 5 6 7 8 9";// or getline(cin, str); if you want input in one line with spaces
stringstream ss(str);
int  a = 0;
while(ss >> array[i][a]){
a++;
if(a>=9) break;//in case you entered more than nine numbers
}

and don't forget to include sstream library
Topic archived. No new replies allowed.