This code went wildly out of control and I can't figure out why. It compiled o.k., but when I ran it, it just kept growing and growing the file. What I want out of this code is to input a file of variable length records comprised of digits and for it to return a file of fixed length digits (5)with the appropriate header and footer information per 100 records.
ex. number = 5 digit number
00430100number x 100mgr 20030903010101
00430101number x 100mgr
20030903010101
00430102number x 100mgr
20030903010101
Here is the code that I have so far,
/* Donald Frantum */
#include <iostream.h>
#include <ctype.h>
#include <fstream.h>
#include <string>
#include <iomanip.h>
void main()
{
int ship_method = 0;
int zone = 0;
int company = 0;
string in_file;
string out_file;
cout << "This program creates a P&H table to be loaded into the" << endl;
cout << "Ecometry PH-DOLLARS dataset." << endl;
cout << endl;
cout << endl;
cout << "Please enter the Ship Method that you wish to apply" << endl;
cout << "these shipping charges and <enter>:" << endl;
cin >> ship_method;
cout << "Please enter the zone that you wish to apply to these" << endl;
cout << "charges and <enter>:" << endl;
cin >> zone;
cout << "Please enter the company that you wish to apply these" << endl;
cout << "charges and <enter>:" << endl;
cin >> company;
cout << "Please enter the path where the input file can be found:" << endl;
cin >> in_file;
cout << "Please enter the path where the output file should be placed:" << endl;
cin >> out_file;
ifstream zone_chrg_input;
zone_chrg_input.open(in_file.c_str());
ofstream out;
out.open(out_file.c_str());
int c;
int i;
int count[5];
long line_cnt = 0;
long where_i_am = 100;
long range = 0;
int digit_cnt = 0;
if (company < 10)
{
out << setw(2) << setfill('0') << company;
}
if (ship_method < 10)
{
out << setw(2) << setfill('0') << ship_method;
}
else
{
out << ship_method;
}
if (zone < 10)
{
out << setw(2) << setfill('0') << zone;
}
if (range < 10)
{
out << setw(2) << setfill('0') << range << " ";
}
line_cnt = 0;
while ( (c = zone_chrg_input.get() ) !=EOF )
{
while (line_cnt <= where_i_am)
{
if (isdigit(c) && digit_cnt < 5 && c != '\n')
{
i = 0;
for(i = 0; i < 5; i++)
{
count = c;
}
out << setw(5) << setfill('0') << count;
}
if (c == '\n')
{
line_cnt = ++line_cnt;
}
}
range = ++range;
where_i_am = where_i_am + 100;
out << setw(8) << setfill(' ');
out.setf(ios::left);
out << "MGR";
out << setw(92) << setfill(' ');
out.setf(ios::left);
out << "20030905010101";
if (range < 100)
{
if (company < 10)
{
out << setw(2) << setfill('0') << company;
}
if (ship_method < 10)
{
out << setw(2) << setfill('0') << ship_method;
}
else
{
out << ship_method;
}
if (zone < 10)
{
out << setw(2) << setfill('0') << zone;
}
if (range < 10)
{
out << setw(2) << setfill('0') << range << " ";
}
}
if (company < 10)
{
out << setw(2) << setfill('0') << company;
}
if (ship_method < 10)
{
out << setw(2) << setfill('0') << ship_method;
}
else
{
out << ship_method;
}
if (zone < 10)
{
out << setw(2) << setfill('0') << zone;
}
if (range < 10)
{
out << setw(2) << setfill('0') << range << " ";
}
}
zone_chrg_input.close();
out.close();
}
Any help is appreciated.
Thanks in advance,
Donald
ex. number = 5 digit number
00430100number x 100mgr 20030903010101
00430101number x 100mgr
20030903010101
00430102number x 100mgr
20030903010101
Here is the code that I have so far,
/* Donald Frantum */
#include <iostream.h>
#include <ctype.h>
#include <fstream.h>
#include <string>
#include <iomanip.h>
void main()
{
int ship_method = 0;
int zone = 0;
int company = 0;
string in_file;
string out_file;
cout << "This program creates a P&H table to be loaded into the" << endl;
cout << "Ecometry PH-DOLLARS dataset." << endl;
cout << endl;
cout << endl;
cout << "Please enter the Ship Method that you wish to apply" << endl;
cout << "these shipping charges and <enter>:" << endl;
cin >> ship_method;
cout << "Please enter the zone that you wish to apply to these" << endl;
cout << "charges and <enter>:" << endl;
cin >> zone;
cout << "Please enter the company that you wish to apply these" << endl;
cout << "charges and <enter>:" << endl;
cin >> company;
cout << "Please enter the path where the input file can be found:" << endl;
cin >> in_file;
cout << "Please enter the path where the output file should be placed:" << endl;
cin >> out_file;
ifstream zone_chrg_input;
zone_chrg_input.open(in_file.c_str());
ofstream out;
out.open(out_file.c_str());
int c;
int i;
int count[5];
long line_cnt = 0;
long where_i_am = 100;
long range = 0;
int digit_cnt = 0;
if (company < 10)
{
out << setw(2) << setfill('0') << company;
}
if (ship_method < 10)
{
out << setw(2) << setfill('0') << ship_method;
}
else
{
out << ship_method;
}
if (zone < 10)
{
out << setw(2) << setfill('0') << zone;
}
if (range < 10)
{
out << setw(2) << setfill('0') << range << " ";
}
line_cnt = 0;
while ( (c = zone_chrg_input.get() ) !=EOF )
{
while (line_cnt <= where_i_am)
{
if (isdigit(c) && digit_cnt < 5 && c != '\n')
{
i = 0;
for(i = 0; i < 5; i++)
{
count = c;
}
out << setw(5) << setfill('0') << count;
}
if (c == '\n')
{
line_cnt = ++line_cnt;
}
}
range = ++range;
where_i_am = where_i_am + 100;
out << setw(8) << setfill(' ');
out.setf(ios::left);
out << "MGR";
out << setw(92) << setfill(' ');
out.setf(ios::left);
out << "20030905010101";
if (range < 100)
{
if (company < 10)
{
out << setw(2) << setfill('0') << company;
}
if (ship_method < 10)
{
out << setw(2) << setfill('0') << ship_method;
}
else
{
out << ship_method;
}
if (zone < 10)
{
out << setw(2) << setfill('0') << zone;
}
if (range < 10)
{
out << setw(2) << setfill('0') << range << " ";
}
}
if (company < 10)
{
out << setw(2) << setfill('0') << company;
}
if (ship_method < 10)
{
out << setw(2) << setfill('0') << ship_method;
}
else
{
out << ship_method;
}
if (zone < 10)
{
out << setw(2) << setfill('0') << zone;
}
if (range < 10)
{
out << setw(2) << setfill('0') << range << " ";
}
}
zone_chrg_input.close();
out.close();
}
Any help is appreciated.
Thanks in advance,
Donald