Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

program that reads from a text file & give results

Posted on 2000-02-15
7
Medium Priority
?
228 Views
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";
      textfile.close();
}
0
Comment
Question by:Samer Kharsa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 1

Expert Comment

by:aperdon
ID: 2525014
what problem you have?
0
 
LVL 1

Accepted Solution

by:
aperdon earned 400 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
0
 
LVL 22

Expert Comment

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

textfile >> date >> logtime >> ISP >>staytime >> phonenumber >> fee;
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Expert Comment

by:aperdon
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
0
 
LVL 22

Expert Comment

by:nietod
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.
0
 
LVL 1

Expert Comment

by:aperdon
ID: 2525394
yeppes, i totaly agree
0
 

Author Comment

by:Samer Kharsa
ID: 2531946
Thanks guys
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

721 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