Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

out of control II 1

Status
Not open for further replies.

dnfrantum

Programmer
Oct 23, 2001
175
US
Because my last post was sooo long, it was suggested that I start a new thread.
The following code isn't returning the values that I expect:

Code:
/* 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 << &quot;This program creates a P&H table to be loaded into the&quot; << endl;
      cout << &quot;Ecometry PH-DOLLARS dataset.&quot; << endl;
      cout << endl;
      cout << endl;
      cout << &quot;Please enter the Ship Method that you wish to apply&quot; << endl;
      cout << &quot;these shipping charges and <enter>:&quot; << endl;
      cin >> ship_method;
      cout << &quot;Please enter the zone that you wish to apply to these&quot; << endl;
      cout << &quot;charges and <enter>:&quot; << endl;
      cin >> zone;
      cout << &quot;Please enter the company that you wish to apply these&quot; << endl;
      cout << &quot;charges and <enter>:&quot; << endl;
      cin >> company;
      cout << &quot;Please enter the path where the input file can be found:&quot; << endl;
      cin  >> in_file;
      cout << &quot;Please enter the path where the output file should be placed:&quot; << 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 << &quot;  &quot;;
         }
         line_cnt = 0;
               while ( (c = zone_chrg_input.get() ) !=EOF )
               {
                  while (line_cnt <= where_i_am)
                  {
                     i = 0;
                          for(i = 0; i < 5 && isdigit(c) && c != '\n'; i++)
                        {
                        count[i] = c;
                        }
                     out << setw(5) << setfill('0') << count[i];
                     line_cnt = ++line_cnt;
                  }
               range = ++range;
               where_i_am = where_i_am + 100;
               out << setw(8) << setfill(' ');
               out.setf(ios::left);
               out << &quot;MGR&quot;;
               out << setw(92) << setfill(' ');
               out.setf(ios::left);
               out << &quot;20030905010101&quot;;
               out.setf(ios::right);
               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 << &quot;  &quot;;
                  }
               else
                  {
                  out << setw(2) << setfill('0') << range << &quot; &quot;;
                  }
               }      
zone_chrg_input.close();
out.close();
}

sample input data...


4000
4007
4029
4050
4072
4093
4115
4136
4157
4179
4200
4222
4243
4265
4286
4307


answers to prompts...

shipmethod 43
zone 02
company 00
filename myinfile
output myoutfile

What I am expecting...

This is UPS zone charge table. It contains all the charges per pound for zone 02.

What I need is for it to read in the file myinfile, put the incrementing header '00420201 ' and the footer 'MGR 20030901010101 and 92 trailing spaces' after every 100 lines read into the myoutfile. The header should only increment the last 4 digits, padded with spaces. I can provide an example offline if necessary.



Thanks in advance,
Donald
 
I was playing with your code a little and I realized a problem you are having. You asked about isdigit in another thread, and it looks like you are trying to output the char as an int.

You don't have to do it that way. You can just read an int from the input file directly. Assuming your file has all numbers, one per row, and they always are small enough to fit into an int, you can do this:
Code:
int currentVal;
zone_chrg_input >> currentVal;
while (!zone_chrg_input.eof())
{
    // All your header stuff ...

    while (line_cnt <= where_i_am && !zone_chrg_input.eof())
    {
        out << setw(5) << setfill('0') << currentVal; //<< endl; for newline
        ++line_cnt;
        zone_chrg_input >> currentVal;
    }

    // All your footer stuff ...
}
 
PS. I was assuming above that you are using the correct headers (e.g. <iostream> instead of <iostream.h>, etc.) like I mentioned in the previous thread.
 
To make sure I understand what you are saying...
Can I get rid of

Code:
ifstream zone_chrg_input;
      zone_chrg_input.open(in_file.c_str());
      ofstream out;
      out.open(out_file.c_str());

Thanks in advance,
Donald
 
No, because you still use those file streams. In the partial code I posted, you are using
Code:
zone_chrg_input
, and you can still use
Code:
out
in the way you were doing it.
 
That last post got me over the hump. Thanks again.



Thanks in advance,
Donald
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top