Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

C++ read file info - please help!

Original Message
Name: Tina Brant
Date: February 16, 2005 at 07:48:43 Pacific
Subject: C++ read file info - please help!
OS: WINDOWNT
CPU/Ram: PENTIUM/128
Comment:
Hi. I am having a problem with the follow code. The program is suppose to read in a file adn determine is the call is local or long distance and then determine the length of the call. A typical file to be read into the program would look like this:

LONG 12:00 1:52
LOCAL 3:00 3:27
.
.
.

My problem is 1) I don't understand why my while loop is infinite; and 2) I do not understand how to make the program ignore (or skip) the colon (:) when I read the times into an int variable. I have been working on this for days and am not getting anywhere. If someone could please point me in the right direction I would greatly appreciate it.

Here is my code (please note many of the cout statements are merely for testing purposes):

int main()
{
int mins;
float cost;
char file[50];
string word;
int start_time;
int stop_time;
int counter=0;


cout << setprecision(2); //set up floating-pt output format

cout << "Please enter maximum number of free minutes for local";
cout << " calls per month: " << endl;

cin >> mins;

cout << "Please enter the cost per minute for local calls that";
cout << " exceed the maximum: " << endl;

cin >> cost;

cout << "Minutes = " << mins << ", Cost = " << cost << endl;

cout << "Please input name of file for processing: " << endl;

cin >> file;

ifstream infile(file);

if (infile.fail())
{
cout << "Cannot open file" << endl;
cout << "Please input name of file for processing: " << endl;
cin >> file;
ifstream infile(file);
}

while(!infile.eof() || count < 5;)
{
infile>>word>>start_time>>stop_time;
if (word == "LONG")
{
cout << "Long Distance call." << endl;
}
else
{
cout << "Local call." << endl;
}

cout << "Word = " << word << endl;

cout << "Start of call: " << start_time << ", Call end: " << stop_time <<
endl;
}


Any help would be greatly appreciated.

Thanks!



Report Offensive Message For Removal


Response Number 1
Name: Don Arnett
Date: February 16, 2005 at 11:00:39 Pacific
Subject: C++ read file info - please help!
Reply: (edit)
First, in the while loop test, I assume that you mean 'counter < 5' rather than 'count < 5', since count is not declared anywhere.

Where do you increment 'counter'?? It doesn't appear that you increment 'counter' anywhere, so it will always be 0, thus always less than 5, thus the loop never ends.


Regarding the times: you'll need to do one of the following:

- read the hour into an int, read the colon as a char, then read the minute into another int (doing this for both times)

- read the whole time as a string then extract the hour/minute or remove the colon

- fscanf() would work, but I don't think that you have that available using ifstream

FILE *if = fopen("filename","r");
int hour, minute;

fscanf(if,"%d:%d",&hour,&minute);

Be sure to come back and let us know if our suggestions helped!


Report Offensive Follow Up For Removal

Response Number 2
Name: Tina Brant
Date: February 16, 2005 at 11:18:20 Pacific
Subject: C++ read file info - please help!
Reply: (edit)
Don -- Thank you for responding. As for the while loop please just ignore the reference to the counter as I was doing some testing and just forgot to remove it (I removed all other references to it). Therefore, it should read:

while(!infile.eof())
.
.
.

For some reason it seems that eof is not recognized.

As far as my question on the time intervals, I do not know how to extract or ignore a charaction when reading text -- that is the problem. I would like try your second suggestions of reading the hours into an integer variable the colon into a string and the minutes into an integer variable. Could I read the colon into a char variable? Also, would I have to read the hour into an int array (int hour[2]) in order to skip the colon, then read the colon into a char array (char trash[1]), and then the minutes into an integer variable? Would that work?

Thanks again for responding so quickly -- I am sort of stuck here...


Report Offensive Follow Up For Removal

Response Number 3
Name: egkenny
Date: February 16, 2005 at 19:03:37 Pacific
Subject: C++ read file info - please help!
Reply: (edit)
// Data file:
// LONG 12:00 13:52
// LOCAL 15:00 15:27
// LOCAL 16:10 18:40
// LOCAL 19:00 20:30
// LOCAL 22:20 23:20

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

unsigned short ExtractMinutes(string timeString)
{
unsigned short colonPosition=0, count, length, hours, minutes;
char minuteString[6];
char hourString[6];
length = timeString.size();
colonPosition = timeString.find(":");
if (colonPosition)
{
count = colonPosition;
strcpy(hourString, timeString.substr(0, count).c_str());
hours = atoi(hourString);

count = length - colonPosition - 1;
strcpy(minuteString, timeString.substr(colonPosition+1, count).c_str());
minutes = atoi(minuteString);
}
else
{
hours = 0;
count = length;
strcpy(minuteString, timeString.c_str());
minutes = atoi(minuteString);
}
return (hours*60 + minutes);
}

int main()
{
unsigned int local_mins, free_mins, total_mins;
float cost;
char file[50];
string word;
string start_time;
string stop_time;
int count=0;

cout.flags(ios::fixed);
cout << setprecision(2); //set up floating-pt output format

cout << "Please enter maximum number of free minutes for local";
cout << " calls per month: " << endl;

cin >> free_mins;

cout << "Please enter the cost per minute for local calls that";
cout << " exceed the maximum: " << endl;

cin >> cost;

cout << "Minutes = " << free_mins << ", Cost = " << cost << endl;

cout << "Please input name of file for processing: " << endl;

cin >> file;

ifstream infile;

infile.open(file);

if (!infile.good())
{
infile.clear(); // clear fail bit
cout << "Cannot open file" << endl;
cout << "Please input name of file for processing: " << endl;
cin >> file;
infile.open(file);
}

if (infile.good())
{
total_mins = 0;
// read first line here to set initial value for eof flag
infile >> word >> start_time >> stop_time;
while(!infile.eof())
{
if (word == "LONG")
{
cout << "Long Distance call." << endl;
}
else
{
cout << "Local call." << endl;
local_mins = ExtractMinutes(stop_time) - ExtractMinutes(start_time);
cout << "minutes = "<< local_mins << endl;
total_mins += local_mins;
}

cout << "Word = " << word << endl;

cout << "Start of call: " << start_time << ", Call end: " << stop_time << endl;
infile >> word >> start_time >> stop_time;
}
infile.close();
}
else
cout << "Cannot open file" << endl;

cout << endl;
cout << "local minutes = "<< total_mins << endl;
cout << "free minutes = "<< free_mins << endl;
cout << "charged minutes = "<< total_mins - free_mins << endl;
cout << endl;
cout << "charge per minute = "<< cost << endl;
cout << "charged total ($) = "<< (total_mins - free_mins)*cost << endl;

return 0;
}


Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: C++ read file info - please help!

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




Slow boot time

Trasnferring Documents from old HD

My k8T Neo-v usb's aren't working!

Date Modified = Date Created Time

system files on removable harddrive


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC