Hey again guys and gals,

getting a really weird error when i attempt to print out doubles

in my program i have to store several double values, but when ever i print then out all i get is

-9.25596e+061 instead of what should be printed out.

string line;
	stringstream stream;
	cout << "Please Enter the Mortgage Ammount:" << endl;
	getline(cin,line);
	stream << line;
	stream >> mortgageAmmount;
	cout << "Please Enter the Mortgage Term in Years: " << endl;
	getline(cin,line);
	stream << line;
	stream >> mortgageTerm;
	cout << "Please Enter the Interest Rate: " << endl;
	getline(cin,line);
	stream << line;
	stream >> interestRate;

thats the code that receives the input mortgageTerm, interestRate and mortgageAmmount are all stored as doubles.

cout << "Mortgage Ammount: " << mortgageAmmount << " GBP"   << endl
			 << "Mortgage Term: "    << mortgageTerm	<< " Years" << endl
			 << "Interest Rate: "    << interestRate    << "%"      << endl

is the code that prints out the data.

in my test run i entered
mortgageAmount as 20,000
mortgageTerm -9.25596e+061
interestRate -9.25596e+061

and it prints out
mortgageAmount as 20,000
mortgageTerm 20
interestRate 5.5

thanks in advance for any help

-Midi

Dani AI

Generated

Short answer: the odd large negative number usually means the parse failed and the double was left with garbage (or never initialized). The root cause in this thread is reusing one stringstream and appending new lines into it without resetting its buffer or status; after the first extraction the stream can be at EOF or in a fail state, so subsequent operator>> calls fail.

As suggested, reading directly into a double with cin >> avoids that particular pitfall (and, as found, fixed the symptom). If you want to keep the safer "read a whole line then parse" approach, either construct a fresh istringstream per line or reset the existing stream before each use:

std::string line;
std::getline(cin, line);
std::istringstream iss(line);
double d;
if (!(iss >> d)) {
    // handle bad input
}

Or, if reusing a std::stringstream object:

stream.clear();         // clear eof/fail bits
stream.str(line);       // replace internal buffer and reset read position
stream >> d;

Extra practical tips: remove thousands separators (commas) from the input before parsing; use std::stod if you prefer exception-based parsing; always initialize doubles (double t = 0.0;) so a failed parse won't leave them as uninitialized memory; and be careful mixing getline with operator>> (consume leftover newlines with cin.ignore() when needed). For debugging, print the raw line and check stream.fail()/iss.fail() immediately after extraction to detect parse errors.

Recommended Answers

All 2 Replies

Why are we not collecting the inputs into some 'double' variables directly? why so many string operations?

because i read that reading in strings first then converting by using the stream works better than cin due to problems with cin.

i'm currently rewriting it to use cin now.

when reading in my integers for my menu using strings and streams works perfectly.

i'll post back when i've tried it with cin

-Midi

edit: I've changed them all to cin and it works, guess it was me just trying to make it more complex than it really is. still not sure why mortgageAmount worked and the others didnt even though they used the same read method and were all doubles but it works now so i wont spend long wondering why it didnt work

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.