Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

string to double

Posted on 2004-11-28
3
Medium Priority
?
504 Views
Last Modified: 2010-04-01
I am trying to read a file one line at a time  ,tokenize a string with space delimiter. Then convert the lines to objects.

the lines in the file are in this format

A 100000.12  0.08 11
S 100000.12  0.08 11

now I am trying to read this file and tokenize  them with space delimiter so that I can create objects from them.

while(getline(ins, line)){

stringstream ss(line);
if(ss >> buff)
            {
                  if(buff == "A")
                  {
                        //(double prin, double rate, int length)
                        //ss.setw(10);
                        ss>>principal;
                        ss>>rate;

What is Interesting is that though both principal and rate are declared as double. it is not reading the values like 10000.12 or 100000.12 .  it is taking out the single digit or both the digits after the decimal depending on the size of the number.  what is the problem here.

0
Comment
Question by:ashokpappu
  • 2
3 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12692907
you must declare buff as 'char' type. Something like this:

char buff;

while(getline(ins, line)){

stringstream ss(line);
if(ss >> buff)
          {
               if(buff == 'A')    //   <--------single quotes here
               {
                    //(double prin, double rate, int length)
                    //ss.setw(10);
                    ss>>principal;
0
 

Author Comment

by:ashokpappu
ID: 12692981
THe problem is not in the Buff  the problem is in the principal.  it it not reading double values  properly
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 12693065
Are you sure you have an input problem. I think it is an output problem. Have a look to this example:

int main()
{
string buff, line = "A 100000.12 0.08 11";
float principal, rate;

stringstream ss(line);

if(ss >> buff) {
   if(buff == "A")
   {
       ss >> principal >> rate;
    }    
}

cout.precision(10);            // IF YOU DONT PUT THIS LINE, THEN principal VALUE WILL BE CUTTED AT ***OUTPUT TIME****
cout << buff << endl;
cout << principal << endl;     // BUT INTERNALLY IT HAS STORED VALUE CORRECTLY
cout << rate << endl;
   
   system("pause");
   
    return 0;

}
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question