I have to write a code to build an array like this:

1,6 1,0 2,3
3,4 5,6 4,5
7,6 1,2 9,7

Where the numbers are coordinates which have to be "separated" when taken in consideration
Like:

cell[2][2]---> x=5 y=6

I decided to make a char array, so that with atoi() I then convert it to numbers, but I cannot declare the array:

char iaMatrix[iSize][iSize][3]={{'1,2','1,0'}
				{'1,0','6,4'}};

It gives me error:
missing } for lots of times.

What should I do? Thank you.

Dani AI

Generated

Short summary and a practical, modern fix.

The problem is representing a 2D grid where each cell holds an (x,y) coordinate and printing/traversing it. Storing coordinates as numeric types (ints) is more robust than storing C-style char strings and calling atoi everywhere. was right to suggest a structured type; correctly discovered the char-vs-string literal pitfall (use double quotes for string literals — multi-character character constants are not what you want).

A compact, safe approach uses std::vector for dynamic sizes and std::pair<int,int> (or a small struct) for each cell. The example below shows how to parse a textual table like "1,2 2,3 4,7 4,3" into a grid of integer pairs and then print it as a table:

#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <utility>

std::pair<int,int> parse_coord(const std::string &tok) {
    std::istringstream ss(tok);
    int a, b;
    char comma;
    if (ss >> a >> comma >> b && comma == ',') return {a, b};
    throw std::invalid_argument("bad token");
}

int main() {
    std::vector<std::string> rows = {
        "1,2 2,3 4,7 4,3",
        "1,8 9,2 5,1 8,5",
        "9,3 3,0 2,1 3,5"
    };
    std::vector<std::vector<std::pair<int,int>>> grid;
    for (const auto &r : rows) {
        std::istringstream row(r);
        std::string tok;
        std::vector<std::pair<int,int>> line;
        while (row >> tok) line.push_back(parse_coord(tok));
        grid.push_back(std::move(line));
    }
    for (const auto &line : grid) {
        for (const auto &p : line) std::cout << p.first << ',' << p.second << ' ';
        std::cout << '\n';
    }
}

Notes and tips:

  • If you must use C-style char[], remember the terminating '\0' and that single quotes are not strings.
  • Prefer numeric storage when you need to index or move to the cell coordinates — parsing once and storing ints avoids repeated conversions.
  • Use nested loops to print/traverse the 2D container and always validate parsed tokens (bounds and format).

Recommended Answers

All 7 Replies

Store the data in a structure

struct mys
{
unsigned int x;
unsigned int y;
};

struct mys the_s[10] = {{1, 2}, {3, 4}, {5, 6}, ...};

Store the data in a structure

struct mys
{
unsigned int x;
unsigned int y;
};

struct mys the_s[10] = {{1, 2}, {3, 4}, {5, 6}, ...};

But does that allow me to make it Bydimensional? I cannot see it... I mean:
I cannot print it as I want.

But does that allow me to make it Bydimensional? I cannot see it... I mean:
I cannot print it as I want.

You can make the array as many dimensions as you like but remember that each element contains a structure that has both an int x and a int y. For printing try..Note for the demonstration I'll use a two dimensional example.

std::cout << "x->" << the_s[1][1]->x << " y->" << the_s[1][1]->y << std::endl;

I'm sorry, I've not explained myself:

I need to print a table like this:
1,2 2,3 4,7 4,3
1,8 9,2 5,1 8,5
9,3 3,0 2,1 3,5

(random numbers)

And to make that, I need those number (1,2) to be char[]

But How can I make it? I mean the struct Idea is fine

struct cChar{
   char cCoordinates[3];
};
int main(){
	cChar iaMatrix[iSize][iSize]={{'1,2','2,3'}};
}

And it works, I mean it compiles. But I cannot print it as a table!
How could I do it? Maybe without a struct, cause the homework doesn't ask it...

What I need to do is: I get a table row column
[1][3]
And then I have to read the values as separeted coordinates to go to the cell they say if it exists.
So I thought of char, but maybe there's a better solution?

To print the table use a nested for statement...one for statement for each dimension.

I've found it myself, I simply put ' instead of "
for anybody who may need it:

const int iSize=8;
const int iSizeChar=3;

int main(){
	char iaMatrix[iSize][iSize][4]={{"1,2","2,3"}};
	cout << iaMatrix[0][0] << endl;
	system("pause");
	return 0;
}

works fine.
Just remember that the size of the string (char[]) must be one more of what you need for the terminator character '\0'.
Night.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.