how to create multidimensional array of char and int datatype and use it for cin

How to create multidimensional array of char and int datatype and use it for cin ?
When for i need alphabet and numbers and j will be only numbers.

1
2
3
4
5
6
  for (i = 0; i < col; i++)
	{
		for (j = 0; j < row; j++)
		{
			cin >> arr[i][j];
		}
1
2
3
4
5
6
7
8
9
10
int arr[10][10];
char Narr[10][10];

for (i = 0; i < col; i++)
{
	for (j = 0; j < row; j++)
	{
		cin >> arr[i][j];
	}
}


All an array is is a variable that holds several values of itself.
Last edited on
struct maybe
{
int i;
char c;
};

maybe m[1000][23][11]; //a multidimensional array off ints & chars? is this what you seek?
How to create multidimensional array of char and int datatype and use it for cin ?
When for i need alphabet and numbers and j will be only numbers.

What does it mean?
‘i’ and ‘j’ are just indexes: they identify an element, not represent it.

- - -
Do you want a 2D container where the first element of each column (or the first of each row, it is the same) is ‘char’ and all the others are ‘int’?
IF so, you could:
a) use (signed) ints and cast your chars to int when needed - I think usually casting (signed) chars to (signed) int is pretty safe;
b) use a union { char; int; };
c) use std::variant --> https://en.cppreference.com/w/cpp/utility/variant
c) use std::any --> https://en.cppreference.com/w/cpp/utility/any
I need first column, i don't know why but in c++ for some reason columns and rows are reverted than normal. Column = vertically.
So i need like on column Person 1 - Person 10, so first array dimension has to be capable display both alphabets and number and second store just numbers.
But i didn't find type that could store both number and alphabet.
Last edited on
So i need like on column Person 1 - Person 10, so first array dimension has to be capable display both alphabets and number and second store just numbers.
Is this what you mean?
* You have a type Person.
* A Person has a name (or some other alphabetical data) and an age (or some other numerical data).
* You need to store n Persons in an array.

If this is what you need then:
1
2
3
4
5
6
7
8
9
10
class Person{
public:
    char name[256]; //Alternatively: std::string name; I wanted to focus on the most basic types.
    int age;
};

int main(){
    Person people[10];
    //...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Person{
public:
    char name[256]; //Alternatively: std::string name; I wanted to focus on the most basic types.
    int age;
};

int main()
{
    int i;
    for (i = 0; i < 10; i++)
    {        
    cin >> Person people[i];
    }
    //...

}

I run it quickly through online compiler and it says:"error: expected primary-expression before ‘people’"
I wanted 2 dimensional array so at 10 columns can be name Person 1-10 and behind it on each row how much pankakes they eaten.
I was only able to google multidimensional array with numbers and not aplhanumerical.
Last edited on
I wanted 2 dimensional array so at 10 columns can be name Person 1-10 and behind it on each row how much pankakes they eaten.
Then it sounds like all you need is a one-dimensional array holding the number that each person ate.
1
2
3
4
5
6
7
8
9
int main(){
    int pancakes[10];
    for (int i = 0; i < 10; i++){
        std::cout << "How many pancakes did person " << (i + 1) << " eat? ";
        std::cin >> pancakes[i];
    }
    for (int i = 0; i < 10; i++)
        std::cout << "Person " << (i + 1) << " ate " << pancakes[i] << " pancakes.\n";
}
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
#include <iostream>

class Person
{
public:
   char name[256];
   int age;
};

int main()
{
   Person people[3] {};

   for (int i = 0; i < 3; i++)
   {
      // if the name has spaces in it, such as first and last names,
      // using std::cin will cause input problems
      std::cout << "Enter a name: ";
      std::cin >> people[i].name;

      std::cout << "Enter an age: ";
      std::cin >> people[i].age;

      std::cout << '\n';
   }

   for (int i = 0; i < 3; i++)
   {
      std::cout << people[i].name << ", " << people[i].age << '\n';
   }
}
Enter a name: Joe
Enter an age: 18

Enter a name: Sarah
Enter an age: 22

Enter a name: Norm
Enter an age: 36

Joe, 18
Sarah, 22
Norm, 36
Enter a name: Joe d'Ragman
Enter an age:
Enter a name: Enter an age:
Enter a name: Enter an age:
Joe, 0
, 0
, 0
Last edited on
Okay thanks! Tho this still isn't alphanumerical array, it seems it is not even possible. Didn't find 1 article about it. Like person 1, probably isn't needed for anything than. Just this exercise wants me to name person 1 so i thought it has some usage, without it i could do this aswell. http://www.cplusplus.com/forum/articles/12974/
I probably start to learn from this site anyways: learncpp.com
I though practice is best way to learn, but i have no idea how to do a particular thing, while i was familiar with requirments, i mean by that that i read resorces i need to finish exercise by this article, i am looking for a more complete tutorial. Because from what i usually google i don't understand 1 thing.
Last edited on
Just this exercise wants me to name person 1
No, it doesn't. Read it again.

Write a program that asks the user to enter the number of pancakes eaten for breakfast by 10 different people (Person 1, Person 2, ..., Person 10)

I'll grant you that maybe this was somewhat ambiguous phrasing on the part of the writer, but nowhere does it say that you have to store or assign a name to anything. The writer says that the names of the people are "Person 1", "Person 2", etc., but it doesn't say that the program needs to store this as character information (there are other ways to relate a position in the array to Person n, as I did above).
I need first column, i don't know why but in c++ for some reason columns and rows are reverted than normal. Column = vertically.

'normal' depends on what you are used to. c++ is row major. Fortran is column major. As far as I know, most programming languages are row major, but that could just be the ones I know.

alphanumeric is a database type, or a gui-widget type -- it is a high level type that is just a filter applied to lower level types. You can define an alpha-numeric string class and produce a string that can only contain A/N characters, just like you can create a type that only stores values -10 to 10 by applying an enforcement rule on top of the existing tools (an integer in this second case).

a string is fine for alpha numeric data. You can enforce this yourself (it can be done trivially in ascii, its more trouble in Unicode). But I believe you misread the problem, as others said. I am just trying to answer your side-note questions.


Last edited on
Topic archived. No new replies allowed.