Store numbers from a char array into single variable

Hey guys, was wondering if any one had a few moments to show me how to store some nummbers from a char array, into a single variable?

I'm in a coding class now and I was an intermediate coder a while back but I forgot much of what I learned.

Anyway, this code prompts the user to store digits into each index of the char array, but I want to be able to put that array in a single variable, in the index order..

i.e

char array1[3] = {'2', '3', '1'};

Then make a variable that reads the char array and has the value 231.


This is what I have so far:

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
 
  unsigned char num1[10];
  unsigned char num2[10];
    int length = 0, sum = 0, i = 0, total = 0;
 
    
    
    cout << "==========================================\n\n\n";
    
    cout << "Enter FIRST number's length: ";
    cin >> length;
    
    cout << "Enter FIRST binary number: \n";
    for (int i = 0; i < length; i++)
    {
        cout << i + 1 << " digit: ";
        cin >> num1[i];
    }
    
    cout << "FIRST number: ";
    for (int i = 0; i < length; i++)
    {
        cout << num1[i];
    }
    cout << endl;


// potential function?

 for (int i = 0; i < length; i++)
    {
        num1[i] == total;
    }

cout << total << endl;
Last edited on
1
2
    cout << "Enter FIRST number's length: ";
    cin >> length;

What happens when length is greater than 9 (10 - 1 character for null terminator)?

Say you have char ch = '2';. To convert that to the number 2, do int n = ch - '0';.
Topic archived. No new replies allowed.