I've created this array of strings, and was wondering if i could convert this completely into a 2d array whilst maintain functionality of the value swapping?
I've also noticed that when entering some values it seems to output some extra array layers on top of the previous ones. Why is this?
#include <iostream>
#include <string>
#include <vector>
usingnamespace std;
int main ()
{
bool exit = true;
int row, col;
char change;
constint size = 8;
string displaystring0(8,'*');
string displaystring1(8,'*');
string displaystring2(8,'*');
string displaystring3(8,'*');
string displaystring4(8,'*');
string displaystring5(8,'*');
string displaystring6(8,'*');
string displaystring7(8,'*');
while (exit)
{
string arr[size];
int rows = 0;
arr[rows++] = displaystring0;
arr[rows++] = displaystring1;
arr[rows++] = displaystring2;
arr[rows++] = displaystring3;
arr[rows++] = displaystring4;
arr[rows++] = displaystring5;
arr[rows++] = displaystring6;
arr[rows++] = displaystring7;
for (int i = 0; i < rows; i++)
{
cout << arr[i] << endl;
}
cout << endl;
cout << "Please select a row 0-7 (-1 to exit): ";
cin >> row;
cout << endl;
if (row == -1)
{exit = false; break;}
else
{
cout << "Please select a column 0-7: ";
cin >> col;
cout << endl;
cout << "Please enter a character: ";
cin >> change;
cout << endl;
if (row == 0)
{
displaystring0 [col] = change;
for (int i = 0; i < row-1; i++)
{
cout << arr << endl;
}
}
elseif (row == 1)
{
displaystring1 [col] = change;
for (int i = 0; i < row-1; i++)
{
cout << arr[i] << endl;
}
}
elseif (row == 2)
{
displaystring2 [col] = change;
for (int i = 0; i < row-1; i++)
{
cout << arr[i] << endl;
}
}
elseif (row == 3)
{
displaystring3 [col] = change;
for (int i = 0; i < row-1; i++)
{
cout << arr[i] << endl;
}
}
elseif (row == 4)
{
displaystring4 [col] = change;
for (int i = 0; i < row-1; i++)
{
cout << arr[i] << endl;
}
}
elseif (row == 5)
{
displaystring5 [col] = change;
for (int i = 0; i < row-1; i++)
{
cout << arr[i] << endl;
}
}
elseif (row == 6)
{
displaystring6 [col] = change;
for (int i = 0; i < row-1; i++)
{
cout << arr[i] << endl;
}
}
elseif (row == 7)
{
displaystring7 [col] = change;
for (int i = 0; i < row-1; i++)
{
cout << arr[i] << endl;
}
}
else
{
cout << "You have selected an invalid value. Please try again" << endl;
}
}
}
}
Also is there a way of checking the values against a range to make sure its valid? Entering a value for column or row that isnt in the range leads to some dizzying results!