How to convert char to int?

How can I gather input from a user, say "1 2 3", and put it into an array of int? So far I've tried using cin.getline() and putting the input into an array of type char. But this required me to make a char array with some value (I chose 100). To gather the numbers in this array of char I have to itterate through each element (all 100 in this case) of the array. This seems inefficient and I feel there's a better solution out there. Can anyone help? Here's my code:

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

const int MATRIX_SIZE = 3;
const int MAX_MATRIX_SIZE = 100;

bool isRowGood(char rowValues[MAX_MATRIX_SIZE]);

int main() 
{
   char rowOfNumbers[MAX_MATRIX_SIZE];
   
   cout << "Input an 3 x 3 matrix of integers to check." << endl;
   cout << "Values should be entered in their 'natural'" << endl;
   cout << "form, on 3 lines with 3 values per line." << endl;
   cout << "[";
   cin.getline(rowOfNumbers,sizeof(rowOfNumbers));


   isRowGood(rowOfNumbers);
}

bool isRowGood(char rowValues[MAX_MATRIX_SIZE]){
	
	for(int i=0; i <= MAX_MATRIX_SIZE; i++){
		// This is where I stopped due to realization
		// of having to loop through 100 elements for just 3 numbers.
	}
	
	return false;
}


}
1
2
int x;
cin >> x;
if its like "1 2 3" you can use cin and a loop to assign it to variables
Topic archived. No new replies allowed.