convert from char* to integer

Hi, please can anybody help me with this code..How can I convert char* variable to int..If I set to the terminal for example ./sum 1 2 3 I expect 6 on the output..

thank you for you help..

#include <iostream>

using namespace std;


int main(int argc, char* argv[])
{
int i;
int a;

for(i = 1; i < 4; i++)
{
a += argv[i]
cout << a << endl;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char* argv[])
{ 
  int i;
  int a = 0;

  for(i = 1; i < 4; i++)
  {
    a += atoi(argv[i]);	
  }
  cout << a << endl;
}
Last edited on
It doesn't work out!

ladislav@ubuntu:~$ c++ -o sum'/home/ladislav/Desktop/sum.cpp'
/home/ladislav/Desktop/sum.cpp: In function ‘int main(int, char**)’:
/home/ladislav/Desktop/sum.cpp:13:14: error: invalid conversion from ‘char*’ to ‘int’
/home/ladislav/Desktop/sum.cpp:14:3: error: expected ‘;’ before ‘cout’
ladislav@ubuntu:~$

Did you try to just copy/paste moschops' code? It runs just fine on my end.
The code is fine. You must have copied it incorrectly, as faf suggests.
Topic archived. No new replies allowed.