Facing some problem

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
You really should post the error message as the compiler tells you what's wrong.

You're missing:
#include <stdlib.h>
No, I am not missing it. Error massage is given below

error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
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]);
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.
I showed you the exact problem. atoi() takes a C string (char *), you're passing a single char.
now what will I do?
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
}
I mean do you suggest for using pointer?
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.