using pointers and 1 dimensional arrays, while filling the positions with chars

closed account (SGTCko23)
I don't understand why I am told to use char for the new_input but it is created an error from that.
error:
tictactoe.cpp: In function ‘int main()’:
tictactoe.cpp:54:27: error: invalid conversion from ‘char’ to ‘int*’ [-fpermissive]
p_p_tictactoe[i] = new_input1;
^
tictactoe.cpp:55:27: error: invalid conversion from ‘char’ to ‘int*’ [-fpermissive]
p_p_tictactoe[j] = new_input2;


#include <iostream>

using namespace std;

int main()
{
// TODO: Below each p_tictactoe` of type pointer-to-a-pointer-to-a-char
int **p_p_tictactoe;


// 2. Prompt your user to enter a number of rows, then a number of columns.
// store their answers in the variables `rows` and `cols`.
char rows;
char cols;
cout << "Please enter a number of rows: ";
cin >> rows;
cout << "Please enter number of columns: ";
cin >> cols;
// 3. Allocate a 1-dimensional array of pointers-to-chars (length == `rows`)
// and store its address in `p_p_tictactoe`
p_p_tictactoe = new int*[rows];
// 4. Use a for-loop to allocate a dynamic array of chars (length == `cols`)
// for i from 0 to rows - 1 and store its address in `p_p_tictactoe[i]`.
for (int i = 0; i < rows - 1; i++)
{
p_p_tictactoe = new int*[cols];
}
// 5. Use a for-loop to prompt the user to enter a char for each position in
// the array, displaying the message, "Enter a single character for position
// (" << i << ", " << j << "): "
// As you read each char, store it in the array.
// 6. Use a nested for-loop to print the array, one row per line. The chars
// for each row should be space-separated. For example, if the array is
// 2 x 3 and stores the values A, B, C, X, !, &, the output should look
// like:
// A B C
// X ! &
char new_input1;
char new_input2;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << "Enter a single character for position( << i << ): ";
cin >> new_input1;
cout << "Enter a single character for position( << j << ): ";
cin >> new_input2;
p_p_tictactoe[i] = new_input1;
p_p_tictactoe[j] = new_input2;

}
cout << rows << " " << cols << endl;
}
// *** Prevent memory leaks by deallocating dynamic memory when you are done
// using it. ***

// 7. Use a for-loop to delete each row of the dynamic array.


// 8. Delete the pointer-to-a-pointer to release the array of row pointers,
// and set its value to NULL to avoid accessing invalid memory.
for (int i = 0; i < 3; i++)
{
delete[] p_p_tictactoe[i];
delete[] p_p_tictactoe;
}
cout << "Bye!" << endl;
return 0;
}
Last edited on
Hey. Please edit your post and always use code tags for all of your code - http://www.cplusplus.com/articles/jEywvCM9/

The error is very clear.

1
2
p_p_tictactoe[i] = new_input1;
p_p_tictactoe[j] = new_input2;


p_p_tictactoe is an array of integer pointers. new_input1 and 2 are chars. Since char is not a integer pointer, it won't work.
Topic archived. No new replies allowed.