stringstream >> asignment

E

eric

Dear advanced C++ programers:

I copied a piece simple template and stringstream program from book
(c++ cookbook) on chapter 3, section 5: Parsing a String Containing a
Number in Scientific Notation.
-------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

//double sciToDub(const string& str) {
template<typename T>
T strToNum(const string& str) {


stringstream ss(str);
T tmp;
ss >> tmp;

// double d = 0;
// ss >> d;
if (ss.fail()){
string s = "Unable to format ";
s += str;
s += " as a number!";
throw(s);
}
return(tmp);
}
int main() {
try {
/*
cout << sciToDub("1.23456789e5") << endl;
cout << sciToDub("6.02987654e-5") << endl;
cout << sciToDub("asdf") << endl;
*/
double d = strToNum<double>("7.0"); cout << d << endl;
float f = strToNum<float>("7.0"); cout << f << endl;
int i = strToNum<int>("7.0"); cout << i << endl;
char c = strToNum<char>("7.0"); cout << c << endl;
}
catch (string& e) {
cerr << "Whoops: " << e << endl;
}
}
------------------------------------------------------------------------------------------------------
on my test of g++,
all 4 show 7
why , especially first 2 (double and float)?
thanks your help in advance a lot
Eric
 
S

Saeed Amrollahi

Dear advanced C++ programers:

   I copied a piece simple template and stringstream program from book
(c++ cookbook) on chapter 3, section 5: Parsing a String Containing a
Number in Scientific Notation.
-------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

//double sciToDub(const string& str) {
  template<typename T>
  T strToNum(const string& str) {

  stringstream ss(str);
  T tmp;
  ss >> tmp;

//  double d = 0;
//  ss >> d;
  if (ss.fail()){
    string s = "Unable to format ";
    s += str;
    s += " as a number!";
    throw(s);
  }
  return(tmp);}

int main() {
  try {
   /*
    cout << sciToDub("1.23456789e5") << endl;
    cout << sciToDub("6.02987654e-5") << endl;
    cout << sciToDub("asdf")    << endl;
   */
   double d = strToNum<double>("7.0"); cout << d << endl;
   float f = strToNum<float>("7.0");   cout << f << endl;
   int i = strToNum<int>("7.0");       cout << i << endl;
   char c = strToNum<char>("7.0");     cout << c << endl;
  }
  catch (string& e) {
    cerr << "Whoops: " << e << endl;
  }}

------------------------------------------------------------------------------------------------------
on my test of g++,
all 4 show 7
why , especially first 2 (double and float)?
thanks your help in advance a lot
Eric

Hi

I believe the function strToNum() returns numbers with appropriate
precisions. You can check it with debugger and trace the
variables.
I mean the following statement:
double d = strToNum<double>("7.0");
really returns 7.0000000000000000,
but cout rounds d to 7:
cout << d << endl; // output 7
If you write the following statements:
cout << 3.14159 << '\n';
double d = strToNum<double>("7.012345");
cout << d << endl;
You can see the difference.
BTW, it you care about the -insignificant- trailing zeros
you have to modify the output statement:
cout.precision(20); // say set precision to 20 significant digits
cout << d << showpoint << endl;
For a complete discussion about Format states and Manipulators
see:
Bjarne Stroustrup. The C++ Programming Language. special edition
sections 21.4
also, I guess your book has something about the topic.

Regards,
-- Saeed Amrollahi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top