Facing some problem

Mar 23, 2011 at 3:39pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<cstring>
using namespace std;
int eval(char X[],int l,int r);
int main(){
	char X[10];
	cout<<"Please enter an equation:";
	cin>>X;
	//cout<<atoi(X)+atoi(X)<<endl;
	int s=strlen(X);
	int l=0,r=s-1;
	int box=eval(X,l,r);
	cout<<box<<endl;
	return 0;
}
int eval(char X[],int l,int r){

	return atoi(X[l]);
}


/*why my code shows an error massage for atoi function?
Please help me. I have fallen in a trouble.*/
the error massage is error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
Last edited on Mar 23, 2011 at 4:24pm
Mar 23, 2011 at 3:55pm
You really should post the error message as the compiler tells you what's wrong.

You're missing:
#include <stdlib.h>
Mar 23, 2011 at 4:04pm
No, I am not missing it. Error massage is given below

error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
Mar 23, 2011 at 4:06pm
You didn't #include <cstdlib> anywhere, so you're counting on some other header including it, which is wrong.

Anyway, the problem is here, as the message says: return atoi(X[l]);
Mar 23, 2011 at 4:19pm
No, it's not working. It shows same error massage. But it's work when I write return atoi(X). But it gives whole number of this array. But I want to show only a specific number.
suppose my array has 1,2,3. and I want to show 2 that's slot number is 1. and my executed code will show 2 not 123. If I apply this so it shows 123.
Mar 23, 2011 at 4:22pm
I showed you the exact problem. atoi() takes a C string (char *), you're passing a single char.
Mar 23, 2011 at 4:31pm
now what will I do?
Mar 23, 2011 at 4:35pm
You can write a simple function to convert a char (assuming it's a digit) to an int:

1
2
3
4
5
int char_to_int(char ch)
{
    if(isdigit(ch)) return ch - '0';
    else // deal with error
}
Mar 23, 2011 at 4:40pm
I mean do you suggest for using pointer?
Mar 23, 2011 at 4:50pm
If arr is an array of chars, its name converts to a pointer to char, but arr[i] is always a single char, not a pointer.
Topic archived. No new replies allowed.