I am writing a coin flip program and i want the user to input a series of numbers as below
1.input 2 numbers x and y as the number of rows and number of columns(Done)
2.input the initial state of the coins with 1(head) and 2(tail) like below:
1010
1111
0000
3.Taking the step 2's input and assign the 1s and 0s to the corresponding position in the 2D array as below
1(grid[0][0])0(grid[0][1])...
1(grid[1][0])1{grid[1][1])...
...
For 3, is it possible to do so? What i have done now is asking the user to input the numbers 1 by 1 as the below code shows.
for(int x=0;x<rows;x++)
{
for(int y=0;y<columns;y++)
{
cin >> grid[x][y];
}
}
Is it possible to read the position of the 1s and 0s using function like "char.at()" or any other advise?
You could do something like this:
int grid[10][10];
for (int x=0; x<10; ++x)
{
std::string s;
std::cin>>s;
///check for string correctness here
for(int y=0; y<10;++y)
{
grid[x][y]=s[y]-'0';
}
}
for (int x=0; x<10; ++x)
{
for(int y=0; y<10;++y)
{
std::cout<<grid[x][y];
}
std::cout<<"\n";
}
Basically you read x strings from input e use it then char by char. You can also read just one string with length = x*y if you want.