Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,556 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,613 people online right now. Registration is fast and FREE... Join Now!




using atof() and <fstream>

 
Reply to this topicStart new topic

using atof() and <fstream>

makaveli93
29 Oct, 2007 - 10:53 AM
Post #1

New D.I.C Head
*

Joined: 30 May, 2007
Posts: 13


My Contributions
I have made a similar post recently but unfortunately I was unable to recieve any help. I've made better sense of it by my self and now there is only one line of code I can't get working
CODE

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
int main()
{

string line;

int count=0;
ifstream fin("payroll2.txt");


float c;
int hour2;
if(!fin)

            {

                        cout << "While opening a file an error is encountered" << endl;

            }

            else

            {

                        cout << "File is successfully opened\n\n" << endl;

            }

            while(!fin.eof())

            {

                        //fin>> a;
                      
                        if (count==0)
                        {
                        getline(fin,line);
                        cout << line << endl;
                        cout<<"How many hours worked?: ";
                        cin>>hour2;
                        cout<<endl;
                        }
                        
                        getline(fin,line);
                        cout<<line;
                        cout<<endl;
                    
                    

                     ************************  
                        if (count==2)
                        c = atof(line);
                     *************************  
                        

count++;
                        }

system("PAUSE");
            return(0);
}

I don't understand what I'm doing wrong. I'm trying to convert the string that reads as "42.55" to a float value. I've tried many different string functions like strcpy() but none of them worked. Any help would be greatly appreciated. Thanks!

ps: the '*' are there to show which lines are not working, I do not use them in my actual code lol.
Oh and the text file that it reads looks like:

name
Sin number: 902491019
42.55

This post has been edited by makaveli93: 29 Oct, 2007 - 10:55 AM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Using Atof() And <fstream>
29 Oct, 2007 - 11:15 AM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
atof() requires that you pass a const char*, not a string. That is, you need to pass a c-style string rather than a c++-style std::string. Fortunately, std::string has a method that returns a const char* version of the string. If you change the line you are having problems with to:
CODE

c = atof(line.c_str());

it should work - at least in terms of converting the string into a float. I haven't really looked over the flow or logic of the program in a lot of detail, but it appears that you will only be able to enter the hours worked for the first employee taken from the file - because of the if (count==0) used at the start of the block where the number of hours is entered.

C++ also has something known as string streams, which can do the same sort of conversions that atof, atoi, etc. can do, but they are much more powerful. You may want to take a look at this page for some more info on them. They allow you to treat strings as you would streams; this allows you to, for instance, use the >> operator to extract formatted data from the string, much as you would using cin or a file stream.

Hope that helps,

-jjh
User is offlineProfile CardPM
+Quote Post

makaveli93
RE: Using Atof() And <fstream>
29 Oct, 2007 - 04:44 PM
Post #3

New D.I.C Head
*

Joined: 30 May, 2007
Posts: 13


My Contributions
QUOTE(jjhaag @ 29 Oct, 2007 - 12:15 PM) *

atof() requires that you pass a const char*, not a string. That is, you need to pass a c-style string rather than a c++-style std::string. Fortunately, std::string has a method that returns a const char* version of the string. If you change the line you are having problems with to:
CODE

c = atof(line.c_str());

it should work - at least in terms of converting the string into a float. I haven't really looked over the flow or logic of the program in a lot of detail, but it appears that you will only be able to enter the hours worked for the first employee taken from the file - because of the if (count==0) used at the start of the block where the number of hours is entered.

C++ also has something known as string streams, which can do the same sort of conversions that atof, atoi, etc. can do, but they are much more powerful. You may want to take a look at this page for some more info on them. They allow you to treat strings as you would streams; this allows you to, for instance, use the >> operator to extract formatted data from the string, much as you would using cin or a file stream.

Hope that helps,

-jjh


Thanks for the help. It converts the string but when I output c it gives me a strange value of -5.395e-043. I'm guessing the way i approached this program was wrong. basically what I wanted to do is read the file, output the first line and then prompt the user for the amount of hours worked. input that. print the second line, print the third line, take the value of the third line and convert it to a float, and then multiply that number by the amount of hours work and output the number. I'm really not sure what I'm doing wrong.
User is offlineProfile CardPM
+Quote Post

makaveli93
RE: Using Atof() And <fstream>
29 Oct, 2007 - 05:22 PM
Post #4

New D.I.C Head
*

Joined: 30 May, 2007
Posts: 13


My Contributions
oh, I fixed it, but I don't understand why it works like this
CODE

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
int main()
{

string line;

int count=0;
ifstream fin("payroll2.txt");


float c;
int hour2;
if(!fin)

            {

                        cout << "While opening a file an error is encountered" << endl;

            }

            else

            {

                        cout << "File is successfully opened\n\n" << endl;

            }

            while(!fin.eof())

            {

                        //fin>> a;
                      
                        if (count==0)
                        {
                        getline(fin,line);
                        cout << line << endl;
                        cout<<"How many hours worked?: ";
                        cin>>hour2;
                        cout<<endl;
                        }
                        
                        getline(fin,line);
                        cout<<line;
                        cout<<endl;
                    
                    

                        if (count==1)
                      
                        c =  atof(line.c_str());
                    
                    

count++;
                        }
cout<<endl;
cout<<c;
cout<<endl;
system("PAUSE");
            return(0);
}


When count equals 1 wouldn't it be on line 2 and not 3? I'm confused. Thanks
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Using Atof() And <fstream>
29 Oct, 2007 - 05:32 PM
Post #5

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Remember, you've got two getline statements within the loop. The first round through, when count==0, both of these execute. You get the name first, and then the sin # - so you've read two lines with one pass through the loop. Then the next round through the loop, when count==1, only the second getline executes, which is where you get the...what is that, the pay rate?...whatever the value of 42.55 represents in your sample file.

And sorry, my comments about the flow of your code (previous post) should just be ignored - I didn't see the file contents that you inserted in the post-script (those should probably go in code tags next time, just so it stands out).

Hope that helps,

-jjh
User is offlineProfile CardPM
+Quote Post

makaveli93
RE: Using Atof() And <fstream>
29 Oct, 2007 - 05:44 PM
Post #6

New D.I.C Head
*

Joined: 30 May, 2007
Posts: 13


My Contributions
ohhhh so get line reads the line its on and then increments it. I get it. thanks a lot smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:59PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month