using ATOI to convert string element to int

Dec 22, 2009 at 5:56pm
..hey guys, my code is below. The specific problem I am having is with line 14, the j = atoi (x[i]); statement in the for loop. The compile error I'm getting says: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *' Not sure if I should be using a pointer here, or what? Any info appreciated...heres the entire code...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//this program sums digits entered by user
#include <iostream>
using namespace std;
int main()
{
	int sumNums = 0;
	int j = 0;
	char* strPtr = 0;
	char x[5];
	cout << "Enter some numbers, and I'll add them up...";
	cin >> x;

	for(int i=0; i<5; i++){
		j = atoi (x[i]);
		j += sumNums;
	}
	cout << sumNums << endl;
}
Dec 22, 2009 at 6:11pm
x is an array of characters, x[i] is a single character.
Anyway, use stringstreams instead of itoa http://www.cplusplus.com/articles/numb_to_text/ http://www.cplusplus.com/forum/articles/6046/
Dec 22, 2009 at 6:21pm
Hey bazzy maybe you can clarify this for me. I had an assignment to create a minesweeper game
http://www.aug.edu/~mcsmld/1302_hw.html)

it has to do with hw1 and 2

it says to change an int to a char do the following: 5 + '0';

it had to do with the fact that the minesweeper table was an array of chars. and we had to call a near miss function that return how many mines were near it and it returned an integer that we were supposed to display in that position(which is a char) so we had to take the number returned and add '0' then input it into that positon.

Is that kind of like what this guy is doing??
Dec 22, 2009 at 6:42pm
The OP says string element to int and atoi does that with C strings ( http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/ )
Converting a character to a integer is not the same thing - as a character is already an integer -
n+'0' converts a single digit integer into its ASCII representation
Dec 22, 2009 at 6:49pm
Have you read this?
http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

EDIT : Probably should refresh the page after hours, before posting a comment :P
Last edited on Dec 22, 2009 at 6:50pm
Dec 22, 2009 at 11:47pm
Also, I think you have line 15 a little backwards (and don't forget to return an int from main).
Last edited on Dec 22, 2009 at 11:49pm
Dec 25, 2009 at 3:20pm
...thanx for all the replies. I rewrote this with stringstream, but still not working. I am not getting any compile errors, but line 14 is returning 0. What am I missing?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//this program sums digits entered by user
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
	int sumNums = 0;
	int j = 0;
	char x[5];
	cout << "Enter a string of numbers, and I'll add them up...";
	cin >> x;

	for(int i=0; i<4; i++){
	istringstream(x[i]) >> j;
	sumNums += j;
	}
	
	cout << sumNums << endl;

	return 0;
}
Dec 25, 2009 at 3:55pm
Again, x[i] is a single character
Dec 25, 2009 at 4:05pm
...yes, thats what I want to do, is add up each character in the string...
Dec 25, 2009 at 4:30pm
Can you give an example of input and the output you would expect?
Dec 25, 2009 at 7:23pm
...sure, if the user enters '234', the result would be '9' (2+3+4)...
Dec 25, 2009 at 7:34pm
Then you need the char to int conversion:
1
2
3
for(int i=0; i<4; i++){
    sumNums += x[i]-'0'; // eg: ASCII '1' - ASCII '0' = 1 (number)
}

Dec 26, 2009 at 4:26am
...thanx Bazzy, that works...sort of. Works OK if I use 4 digits, but not if I do less than 4 digits. So what exactly does the -'0' do?
Dec 26, 2009 at 2:49pm
For example, '2' - '0' subtracts the ASCII values of the two digits: 50 - 48 which returns the numerical value of that digit ( 2 )
If you enter less than 4 digits, loop until strlen(x)
Dec 26, 2009 at 2:57pm
...cool Bzz...thanx for the help...
Topic archived. No new replies allowed.