1

I am trying to convert a string to a double. The code is very simple.

            double first, second;
            first=atof(str_quan.c_str());
            second=atof(extra[i+1].c_str());
            cout<<first<<" "<<second<<endl;
            quantity=first/second;

when trying to convert extra, the compiler throws this gem of wisdom at me:

error: request for member c_str in extra.std::basic_string<_CharT, _Traits, _Alloc>::operator[] [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator](((unsigned int)(i + 1))), which is of non-class type char

I have no idea what that means. if I cout extra[i+1], I get 3. If I leave extra as a string, the program tries to div first (2) by 51 (ascii for 3). What the heck is going on?

James McNellis
  • 348,265
  • 75
  • 913
  • 977
piratebill
  • 31
  • 1
  • 3
  • 8
    My guess is: extra is a string, `extra[i + 1]` is a char, and a char doesn't have a `c_str` method. If you want to convert part of the string, you can probably do something like `atof(extra.c_str()[i + 1])` – UncleBens Nov 07 '09 at 23:29
  • 4
    That code is not complete - you're missing the definition of "extra". – nobody Nov 07 '09 at 23:31
  • Just tried that. error: invalid conversion from const char to const char* error: initializing argument 1 of double atof(const char*) – piratebill Nov 07 '09 at 23:32
  • 1
    @UncleBens - +1 for the selfless act of putting the correct answer as a comment – Steg Nov 07 '09 at 23:32
  • The code is complete, I just did not post all of it. I can if you think that will help. – piratebill Nov 07 '09 at 23:32
  • Sorry, make that `atof(extra.c_str() + i + 1)` or you'll need to take the address if you want to index the char array: `atof(&extra.c_str()[i + 1])` – UncleBens Nov 07 '09 at 23:34

4 Answers4

7

It sounds like extra is a std::string, so extra[i+1] returns a char, which is of non-class type.

It sounds like you are trying to parse the string extra starting from the i+1th position. You can do this using:

second = atof(extra.substr(i + 1).c_str());
James McNellis
  • 348,265
  • 75
  • 913
  • 977
1

Based on the error message, extra is of type std::string. When you do extra[i+1], the result is of type char and contains the character in the string extraat position i+1.

Probably not what you intended.

As a fix, replace extra[i+1] with a new string, as follows:

extra2 = extra.substr(i+1);
// ...
second=atof(extra2.c_str());

A better way to handle this conversion (which is less prone to error) is to use stringstreams:

stringstream ss(str_quan);
double first;
ss >> first;
greyfade
  • 24,948
  • 7
  • 64
  • 80
1

You haven't said, but I think "extra" is of type std::string. So the expression "extra[i+1] is character "i+1" of "extra", which seems to be '3'.

What are you actually trying to do?

If you just want characters i+1 through to the end of "extra" (and you know how long extra is), you want something like:

 second = atof(extra.c_str() + i + 1);
janm
  • 17,976
  • 1
  • 43
  • 61
  • It is preferable to use `substr` for this purpose since it will throw `std::out_of_range` if `i + 1` is past the end of the string. – James McNellis Nov 07 '09 at 23:40
  • At the cost of a temporary; that's why I said "and you know how long extra is". However, I take your point; an alternative would be "assert(i + 1 < extra.size())" before the call to atof(). As we make this code more idiomatic C++, we'd ditch atof(), and possibly use boost::iterator_range and boost::lexical_cast ... – janm Nov 08 '09 at 00:15
0

When asking for help it is often reallyt usfull to provide a compilable example.

My guess (and I emphasis guess becuase there is not enough type information or line numbers).

second=atof(extra[i+1].c_str());

// Guess 
// extra is std::string.

Thus extra[x] is returning a char which does not have a method c_str()

Martin York
  • 257,169
  • 86
  • 333
  • 562