program that reads from a text file & give results

Posted on 2000-02-15
Last Modified: 2010-04-02
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";
Question by:Samer Kharsa
  • 4
  • 2

Expert Comment

ID: 2525014
what problem you have?

Accepted Solution

aperdon earned 100 total points
ID: 2525032
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
LVL 22

Expert Comment

ID: 2525302
You can also read the floating point value directly, like

textfile >> date >> logtime >> ISP >>staytime >> phonenumber >> fee;
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline


Expert Comment

ID: 2525310
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
LVL 22

Expert Comment

ID: 2525383
agreed, I thought both points were worth mentioning.  The first is worth knowing for future reference, the later probably worth doing for this case.

Expert Comment

ID: 2525394
yeppes, i totaly agree

Author Comment

by:Samer Kharsa
ID: 2531946
Thanks guys

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now