Jan 11, 2012 at 5:22am UTC
Hi, I am building simple program that takes three inputs as sides of a triangle
to determine the triangle type (equilateral, isosceles, scalene).
When I compile, I get the error message:
invalid conversion from 'char' to 'const char*'
initializing argument 1 of 'long int atol(const char*)
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 45 46 47 48
#include <iostream>
using namespace std;
int main()
{
char side1[10], side2[10], side3[10];//USE strlen(c-string) VS length() for string object
int iSide1 = atol(side1[0]);//we need to convert the c-string to int (non-zero returned) b/c user can enter eg: 32#2 or @33 or 3.3
// b/c no functions to manipulate numeric values
int iSide2 = atol(side2[0]);//=>int side2
int iSide3 = atol(side3[0]);
cout << "Welcome, this program will read in three positive integers and "
<< "determine the triangle type" <<endl;
cout << "Enter side 1:" ;
cin >> side1;
cout << "Enter side 2:" ;
cin >> side2;
cout << "Enter side 3:" ;
cin >> side3;
if ( iSide1 == 0 || iSide2 == 0 || iSide3 == 0 )//NB: If I want to test specifically if is decimal or has alphabets and/or symbols, I need
// to parse each input
cout <<"Please enter only integers!" <<endl;
else // if they are integers
{
if ( iSide1 < 1 || iSide2 < 1 || iSide3 < 1 )
cout <<"Please enter integers > 0!" <<endl;
else
{
if ( iSide1 == iSide2 && iSide2 == iSide3 && iSide3 == iSide1 )
cout <<"Equilateral triangle" <<endl;
else if ( ( iSide1 == iSide2 && iSide1 != iSide3 && iSide2 != iSide3 ) ||
( iSide2 == iSide3 && iSide1 != iSide2 && iSide1 != iSide3 ) ||
( iSide1 == iSide3 && iSide1 != iSide2 && iSide2 != iSide3 )
)
cout <<"Isosceles triangle" <<endl;
else if ( iSide1 != iSide2 && iSide2 != iSide3 && iSide1 != iSide3 )
cout <<"Scalene triangle" <<endl;
}
}
cout << "Thanks for using Triangle (c)" <<endl;
return 0;
}
Any help appreciated!
Last edited on Jan 11, 2012 at 5:22am UTC
Jan 11, 2012 at 5:29am UTC
error at line 8, 10, 11
this is the prototype of atol
long int atol ( const char * str );
and what you are passing as argument is a character.
Inorder to pass the entire string, do this
atol(side1);
side1[0] is just a character and not a string.
Jan 11, 2012 at 9:13am UTC
Why are you entering information (cin >> side1;) _after_ atol ?
Jan 12, 2012 at 4:31am UTC
I'm using atoi bc I know that it returns 0 if a string contains something like: "abc30", I guess I could use string manipulation to check each character to ensure is numeric via isdigit.