How to assign an infinite number of symbols to an array?

I'm extremely new at C++, and I'm currently taking a class to learn the basics.

I was asked today to write a program that asks a user for the number of rows and columns that they want in an array, then asks the user for a symbol to put in each space of the array.

My problem is that the user can choose any number. How would I assign each and every symbol entered into the array?

Here's my code so far, if that helps any:
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
#include <iostream>
using namespace std; 
int main()
{
    int x;
    int y;
    int rows;
    int columns;
    int characters;  
    characters=rows*columns;
    char symbol; 
    cout<<"How many rows do you want in your matrix?\n";
    cin>>rows;
    cin.ignore();
    cout<<"How many columns do you want in your matrix?\n";
    cin>>columns;
    cin.ignore();
char matrix [rows][columns];

for (x=0; x<rows; x++)
    {
        for (y=0; y<columns; y++)
        { 
            cout<<"Enter a character to put in your matrix.\n";
            cin>>symbol;
            cin.ignore();
            }
            cout<<"\n";
            }
    cin.get();
}

for (x=0; x<rows; x++)
    {
        for (y=0; y<columns; y++)
        {
            cout<< " "<<matrix [x][y]<< " "  ; 
            }
            cout<<"\n";


PS: What's a good compiler? I'm currently using DEV-C++; is this fine for beginners?
Last edited on
Please guys, I need some help
The instructor didn't provide any pointers whatsoever on how to accomplish this task?

There are two general means of accomplishing this task (with a variety of specific implementations available). One by dynamically allocating and managing the memory yourself. Two by way of a preexisting container (most commonly one of the STL containers).

Perhaps the courses I took years ago were exceptions to the norm, but all the assignments I ever received were related either to the material covered in the class itself, or the required reading specified as part of the assignment as well, at least with regards to the basics.

In the interest of providing assistance though, here's an arbitrary implementation for you, using an STL vector.

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
#include<iostream>
#include<vector>
using namespace std;

int main()
{
	int rows, columns;
	char symbol;
	cout << "How many rows do you want in your matrix?" << endl;
	cin >> rows;
	cout << "How many columns do you want in your matrix?" << endl;
	cin >> columns;
	cout << "Enter a character to put in your matrix." << endl;
	cin >> symbol;
	
	vector<char> matrix(rows*columns, symbol);
	
	for (int x=0; x< rows; x++){
		for (int y=0; y<columns; y++){
			cout << " " << matrix[x*columns+y] << " ";
		}
		cout << endl;
	}
	return 0;
}
OP, can you post your code.... also a loop will do it.

use two for loops inside of each other...

1
2
3
4
5
6
7
8
9
10
11
int bong[4][4];
int t;
cout << "pick a number for to input:";
cin >> t;
for (int i = 0; i <4; i++)
{
for(int b = 0; b < 4; i++)
{
bong[i][b] = t;
}
}



take note if you want to put in a character you have to use a char* type. And I'm not sure but when assignment in function comes up you might need to use the ' ' quotations.....

I hope this helps
Last edited on
@OP: If you must use a char[], allocate it dynamically (see tutorial on C++ in documentation) and then cycle through it with a for loop, getting each symbol. That's just about it.
Tried these, and they worked beautifully!

Thanks guys.
Topic archived. No new replies allowed.