Can anyone help me convert char to double. Yes, I tried casting, it did not work. Anyway, this is the problem, the variable is declared as type double in the struct(database structure). However, it is defined as type Text in the class I'm modifying.

The variable in the class I'm using must remain that type because it is used by other functions in the class. Please help

Nis

Dani AI

Generated

MSVC warning C4244 ("conversion from 'double' to 'char', possible loss of data") means a double is being implicitly narrowed to a one‑byte character somewhere (assignment, function call, return, etc.). ’s note that the struct field is double while the class member is Text shows the real issue: the representation is inconsistent. As observed, Text is string‑like; as warned, hiding conversions with blind casts only masks logic bugs. Two safe strategies: keep the value numeric in the class (use double and format to text only at I/O boundaries), or keep text and provide explicit parsing/formatting accessors that validate input.

Example utilities (use whatever string type Text maps to; below uses std::string):

#include <string>
#include <cmath>
#include <climits>
#include <stdexcept>
#include <cstdlib>

// parse textual field to double
double parseTextToDouble(const std::string &s) {
    char *end = nullptr;
    double v = strtod(s.c_str(), &end);
    if (end == s.c_str()) throw std::invalid_argument("not a number");
    return v;
}

// safe narrowing from double to char with range check
char safeDoubleToChar(double d) {
    long r = lround(d); // round to nearest integer
    if (r < CHAR_MIN || r > CHAR_MAX) throw std::overflow_error("out of char range");
    return static_cast<char>(r);
}

Troubleshooting checklist: enable a high warning level (MSVC /W4 or /Wall) and optionally /WX to turn warnings into errors; search the compile diagnostic to find the exact call/site causing C4244; check function prototypes and overloads for parameter type mismatches; decide deliberately whether the class should own numeric or textual representation and implement a single conversion point (getter/setter) with validation. Avoid implicit conversions and use explicit casts only after range checks.

Recommended Answers

All 4 Replies

Type Text is probably a string of some sort, rather than type char per se'. This then becomes similar to converting type int to a string, whcih may be in a FAQ somewhere. In any event, I'd try using a stringstream (C++ style) or sprintf() (C style) to convert the double to a string and then convert the string to type Text, if necessary.

Thanks for the advice and I will not bash casting (cause it does and excellent job) but for this program its not the solution. It solves the run time warnings but logically, it does not generate the likely response.

Do you have any more ides.

Thanks for the advice and I will not bash casting (cause it does and excellent job) but for this program its not the solution. It solves the run time warnings but logically, it does not generate the likely response.

You cannot cast a string to an integer or floating point value or vice versa.

Do you have any more ides.

Uh, yeah. Follow the link I posted.

Thanks for the advice and I will not bash casting (cause it does and excellent job)

List your top 5 things that cast does an excellent job at. I'd speculate that 3-4 of them are used incorrect. (Of course I'd be happy to be wrong.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.