• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

program that reads from a text file & give results

I wrote a small program in C++ that reads from a text file, but I am having a problem with it
since, its main job is to read a line at a time of information in the following form:

11-02-99 16:32      Inconet      00:08:46      330889      0.31$
11-02-99 21:13      Cyberia      00:20:07      744002      0.23$

And count the amount of monthly time online & the rate (the fourth and last column)
My program is the following:

#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
void main()
{
      char date[8], logtime[5], ISP[15], staytime[8], dollars[5];
      double phonenumber;
      float fee=0.00 , a=0.00 , b=0.00 ,c=0.00; // a,b,c are the numbers from the amount in $

      ifstream textfile("a:\\log1.txt", ios::in);            //read from the text file
      while (textfile >> date >> logtime >> ISP >>staytime >> phonenumber >> dollars){
            a= dollars[0]; b= dollars[2]; c= dollars[3]; // Omitted the point
            fee+= a + b/10 + c/100;
            cout << dollars << "\t" << a << "\t" << b << "\t" << c << "\t" << fee << endl;
      }
      //cout <<"\n\nThe total amount until now is\t" << fee <<"\n\n\n";
      textfile.close();
}
0
Samer Kharsa
Asked:
Samer Kharsa
  • 4
  • 2
1 Solution
 
aperdonCommented:
what problem you have?
0
 
aperdonCommented:
The dollars variable is a character array. It contains digits in ASCII-coding. You should substrat the ascii value for '0' to get the correct value.

a= dollars[0] - '0'; b= dollars[2] - '0'; c= dollars[3] - '0'; // Omitted the point
0
 
nietodCommented:
You can also read the floating point value directly, like

textfile >> date >> logtime >> ISP >>staytime >> phonenumber >> fee;
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
aperdonCommented:
it is better, although I wanted to show what was actually wrong with the code.
now he knows about this ascii<->digit conversion, which in case of direct reading was not the case
0
 
nietodCommented:
agreed, I thought both points were worth mentioning.  The first is worth knowing for future reference, the later probably worth doing for this case.
0
 
aperdonCommented:
yeppes, i totaly agree
0
 
Samer KharsaAuthor Commented:
Thanks guys
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now