const char * str; int kolich = help.size(); double znachen(0.0); for(size_t i(0); i <= help.size(); i++) { str = help[i]; znachen += atof(str) * pow(10.0, kolich); kolich--; } return znachen; 

This function takes a vector of type char and returns a number. Each cell of the vector stores one digit. To convert from char to double, I use the atof function. But it takes const char * as an argument. And then there is an error and a question. How does my char element make const char *?

  • str = &(help[i]); - Sublihim
  • one
    Pay attention to std::string instead of std::vector and the function std::stod . - αλεχολυτ
  • i <= help.size() - oh, something is wrong with the condition :) - Sublihim
  • @Sublihim What's wrong with the condition? - Alexeika74
  • @ Alexeika74 when i becomes equal to size () what will be in help [i]? - Sublihim

2 answers 2

If you have std::vector<char> , then you need to take the address from the element.

 str = &(help[i]); 

This is not enough for the atof function, since it receives an 0-determined string at the input.

In your case, if you can’t give up std::vector<char> and you really want to convert char to a number, you can use a not very optimal trick - convert char to std::string

 for(size_t i = 0, l = help.size(); i < l; ++i) { std::string str(1, help[i]); znachen += atof(str.c_str()) * pow(10.0, kolich); kolich--; } 
  • one
    Remember that the string must be null-terminated. - αλεχολυτ
  • This trick is known to me. He is interested in char - Alexeika74

You have a clear discrepancy. If you have character vectors, and they all together make up a double type, then @Sublihim answered you, but it looks silly to go through all the elements of the vector in a loop using pow and not processing the point, etc.

If you want for each element of the vector to get a single-digit number - from '1' , for example, 1 - then just write

 int n = help[i] - '0'; 
  • The bottom line is that I read from a file into a vector of type char. After I have to do the conversion with the read numbers, but for this we need to overtake them in double. From the file I read character by character and it turns out that one number is stored in each cell of the vector. - Alexeika74
  • Number or symbol? In my opinion, you yourself are a little confused ... - Harry
  • I write from the point of view of mathematics. In each cell number. Throughout the number vector - Alexeika74
  • @ Alexeika74 so it can be approached from the point of view of mathematics? Convert a char array to a number and produce the necessary manipulations bit by bit? It seems to me that the processor is much more willing to work with numbers than with symbols :) - Sublihim
  • @Sublihim So I want it like that, but I can't get from char to double to go. - Alexeika74