1

In the following code

#include <stdlib.h> //atoi #include <string> using namespace std; namespace roman { string convert( int input ) { string inputStr = to_string(input); if(inputStr.size()==4) { return string( atoi( inputStr[0] ), 'M')+convert(stoi(inputStr.substr(1, npos)));//error here } } } 

I am getting the titular error in the return line. I think it has something to to with the atoi function. It takes a const char* as the input value. I need to know how to turn the first character in inputStr into a const char*. I tried appending .c_str() to the end of inputStr[0], but that gave me the error request for member c_str which is of non-class type char. Anyone have some ideas?

1
  • The error message is pretty clear. Get a good book on C++ on C++ and read it. Commented Dec 21, 2014 at 18:38

2 Answers 2

3

inputStr[0] is a char (the first char of inputStr); atoi wants a pointer to a null-terminated sequence of chars.

You need inputStr.c_str().

EDIT: If you really want just the first character and not the whole string then inputStr.substr(0, 1).c_str() would do the job.

Sign up to request clarification or add additional context in comments.

1 Comment

Rather than substr, just inputStr[0] - '0' will convert a single digit to its numeric value.
0

You are indexing

inputStr[0] 

to get at a single character. This is not a string and atoi() cannot digest it.

Try constructing a string of one character and call atoi() with that.

Something like,

atoi( string(1, inputStr[0]) ); 

might work. But, that is not the only or best way as it creates a temporary string and throws it away.

But, it will get you going.

1 Comment

It appears that you want to do atoi() on the entire string. If so, the previous answer should work fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.