2

I'm reading in user input for 2 coordinates it goes as follow:

Enter a coord: 10 5

I want to be able to read in the 10 as the row, and the 5 as the column.

Here's what I have.

  char coord[5];

  ...

  cout << "Enter a coord: ";
  cin.getline(coord,sizeof(coord));
  row = atoi(coord[0]);

So the code is giving me an error on atoi() and if a user enter a number like 10 char can't really read it from the indexes, how would you guys do this? Thanks.

user2963379
  • 155
  • 1
  • 12

1 Answers1

5

Well, you could actually use ints to represent your coordinates variables and read them like this:

int row;
int column;
std::cout << "Enter a coord: ";
std::cin >> row >> column;
std::cout << "Coords: " << row << " " << column << std::endl;

As of your error: you get an error on atoi because it gets a const char *str as parameter, and as you are using a char array, you'd need to pass it like int row = atoi(coord); // which will read only the first value, as arrays are implicity converted a pointer to the first array element (often said "decays" to a pointer, because you then lose the ability to call sizeof() on the item) when passed to functions this way.

Community
  • 1
  • 1
Natan Streppel
  • 5,759
  • 6
  • 35
  • 43