char[] to int -> algorithm

Hello, pleasse help me,
I need an algorithm who takes each char out of a char[] and converts it into one int.

javascript:tx('code')
#include <iostream>
#include <string>

using namespace std;

int main()
{
char input[50];
int nr;

cin >> input [50];

for (int b = 0; b < 50; b++)
if (isdigit(input[b]))
nr *= 10 + (input[b] - '0');

cout << nr;


return 0;
}
javascript:tx('output')
Ex: input: 123
output: 4196691


First, you need to initialize nr. Right now it's a junk value.

Second, you are trying to put an element into an index of input that is out of bounds.
I believe you just mean cin >> input;

Third, if input[b] is 0 ('\0'), you should stop iterating because that marks the end of the string you just entered.

Now... boilerplate aside, the actual logic needs to be discussed. You are multiplying nr by its previous value each time. What should the initial value of nr be? If it's 0, you're multiplying by 0 each time.
Perhaps you meant
nr = nr * 10 + (input[b] - '0');

But, this just gets you back 123, as an integer. It certainly doesn't produce 4196691. Where does that number come from? Can you explain it in words?
Last edited on
I want to make an algorithm who calculate a linear mathematical expression, for example:

char input[] = '1+7-3*2=' // 1+7-3*2= 8-3*2= 5*2=10
output: 10

and I'm stuck at this point, I can't put a char into an int.
I use Qt IDE and qmake to build this project.
Excuse me if I made big grammar mistakes, I have a bad English.
Something along these lines, perhaps:

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
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>
#include <sstream>

int eval( const std::string& str )
{
    // put the string into a string stream (we want to ead integers and operators from it)
    std::istringstream stm(str) ;

    int left = 0 ;

    if( stm >> left ) // read the first number (lhs)
    {
        char op ;
        int right ;
        while( stm >> op >> right ) // in a loop, read the operation, next number (rhs)
        {
            // note: error handling elided for brevity

            // noisy stuff to trace what is going on. may be commented out
            std::cout << left << ' ' << op << ' ' << right << " == " ;

            // evaluate, make the first number equal to the result
            if( op == '+' ) left += right ;
            else if( op == '-' ) left -= right ;
            else if( op == '*' ) left *= right ;
            else if( op == '/' && right != 0 ) left /= right ;

            // noisy stuff to trace what is going on. may be commented out
            std::cout << left << '\n' ;
        }
    }

    return left ; // nothing more to be read, return the result
}

int main()
{
    const std::string str = "1+7-3*60/6+4*12+18/11-28" ;
    std::cout << "evaluate " << str << "\n\n" ;

    const int result = eval(str) ;
    std::cout << "\nresult == " << result << '\n' ;
}

http://coliru.stacked-crooked.com/a/748d2fc5c7fe6be6
thanks a lot, it was't so complicated, I have to work harder...
the language will do this for you, if you didnt know that. that is, a string "123" can be converted to the integer 123 using c++ language tools.
Topic archived. No new replies allowed.