I was thinking the problem was around p_p_tictactoe = new int*[cols];
result:
Please enter a number of rows: 4
Please enter number of columns: 3
Enter a single character for position( << i << ): a
Enter a single character for position( << j << ): b
Segmentation fault (core dumped)
#include <iostream>
usingnamespace std;
int main()
{
// TODO: Below each Bp_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 = newint*[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 = newint*[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;
}
}
// *** 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.
}
// TODO: Below each Bp_tictactoe` of type pointer-to-a-pointer-to-a-char int **p_p_tictactoe;
You have ptr to ptr to int, not char
21 22 23 24 25 26 27 28 29
// 3. Allocate a 1-dimensional array of pointers-to-chars (length == `rows`)
// and store its address in `p_p_tictactoe`
p_p_tictactoe = newint*[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 = newint*[cols];
}
3. int again, need char - they are not quite the same thing.
4. the brace on line 25 introduces a new scope, and you have allocated more memory with the same variable name. Don't do this: It's a major problem, and probably the cause of the crash on lines 48 and 49 .
When using new, the declaration and assignment can be on the same line:
int* MyIntPtr = newint*;
// 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.
p_p_tictactoe = newchar*[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 = newchar*[cols];
}
// 5. Use a for-loop to prompt the user to enter a char for each position in
// (" << 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 << *p_p_tictactoe[i] <<endl;
}
}
@veasy62 it's probably better if you post your entire code instead of just a bit of it. Also you forgot the ] at the end of [/code
Edit: Yeh the entire code is at the top of the page, but that is the old version, you changed stuff. Even if you only included the changes in the post above me, might as well not be an inconvenience and include the entire program so one doesnt have to take parts from the top program and the one above me.
#include <iostream>
usingnamespace std;
int main()
{
// TODO: Below each Bp_tictactoe` of type pointer-to-a-pointer-to-a-char
char **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 = newchar*[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 = newchar*[cols];
}
// 5. Use a for-loop to prompt the user to enter a char for each position in
// (" << 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 << *p_p_tictactoe[i] <<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.
}
// 3. Allocate a 1-dimensional array of pointers-to-chars (length == `rows`)
// and store its address in `p_p_tictactoe`
p_p_tictactoe = newchar*[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 = newchar*[cols];
}
This doesn't seem right at all. I mean, to begin with - _p_tictactoe = newchar*[rows]; is pointless since you overwrite it inside the for-loop. And inside the for-loop all you do is recreate the array over and over again, Im pretty sure that's not what the instructions says. They say to create an additonal array of char, what you are doing inside the for-loop is creating an array of pointers-to-char