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!

I was almost finished when... 1

Status
Not open for further replies.

dnfrantum

Programmer
Oct 23, 2001
175
US
My 5 digit number turned into a six digit number. Now instead of being an int it became a long and my code no longer works. Can someone review what I have and comment?
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;
      long currentVal = 0;
         
      line_cnt = 0;
      zone_chrg_input >> currentVal;
      while (!zone_chrg_input.eof())
         {
               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 < 100)
                  {
                  out << setw(2) << setfill('0') << range << &quot;  &quot;;
                  }
               else
                  {
                  out << setw(2) << setfill('0') << range << &quot; &quot;;
                  }      
                  while (line_cnt < where_i_am && !zone_chrg_input.eof())
                     {
                     out << setw(6) << setfill('0') << currentVal; //<< endl; for newline
                     ++line_cnt;
                     zone_chrg_input >> currentVal;
                     }
               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; << endl;
               out.setf(ios::right);
      }                    
zone_chrg_input.close();
out.close();
}

My result isn't human readable, so it will probably not help to send it. The code works fine if 'currentvalue' is set to int and the setw(6) is setw(5). But if the values in the stream increment past 5 digits, this doesn't work.

Thanks in advance,
Donald
 
What for endl // commented in your currenVal output? Use <iosteam> etc, not old i/o library <iostream.h>. I/O manipulators works OK with longs...
 
I made the change from <iostream.h> to <iostream> and added 'using namespace std;' I also changed the 'void main' to 'int main' and added a 'return 0;' statement at the end. It still doesn't work. Because I am not familiar with the function
Code:
zone_chrg_input.open(in_file.c_str());
, is it possible that this has something to do with my problem? I am still getting a non character return. Here is an example:

&#12336;&#13108;&#13616;&#12336;†&#12336;&#12853;&#12344;&#12336;&#12853;&#12344;&#12336;&#12853;&#12344;&#12336;&#12853;&#12344;&#12336;&#12853;&#12344;&#12336;&#12853;&#12344;&#12336;&#12853;&#12344;&#12336;&#12853;&#12344;&#12336;&#12853;&#12344;&#12336;&#12853;&#12344;&#12336;&#12853;&#12344;&#12336;&#12853;&#12344;&#12336;&#12853;&#12344;&#12336;&#12853;&#12344;&#12336;&#12853;&#12344;&#12336;&#12853;&#12344;&#12336;&#12853;

While it is not exactly this, hopefully you can see my point. really more like small boxes with an occasional cross symbol.

Thanks in advance,
Donald
 
If I only change
Code:
out << setw(5) << setfill('0') << currentVal;
to
Code:
out << setw(6) << setfill('0') << currentVal;
, I get the unreadable output. Any thoughts would be appreciated.

Thanks in advance,
Donald
 
You know, that is quite strange. I also get the weird characters with that code when I use your long example input file from a couple posts ago. However, when I use a smaller file with only the example prices from &quot;Out of Control II&quot;, it works fine.

Also, if I uncomment the
Code:
<< endl;
from that line (or if you've deleted it then add that to the end of the line so each price is separated by a newline), then it also works fine even with the
Code:
setw(6)
.

Of course, from previous posts it sounds like you want each
Code:
currentVal
to be packed together with no separation, so separating with a newline will probably not help you. I am curious as to why it is doing this also, so I will try to create a smaller example to reproduce this and maybe somebody else can figure it out from that.
 
I am pretty sure my problem is misusing the c_str() function. Does anyone have a better solution to return the input for an ifstream/ofstream?

Thanks in advance,
Donald
 
Your use of c_str() is fine. The problem is only with the text editor that you are looking at the file with. I used Notepad on Windows 2K, and it didn't work because the outputted line was too long for that editor. When I opened up the same result file in Wordpad or MSVC the correct data appeared.

So don't change anything and run your program, then try a different text editor to open the file (like Wordpad if you are using Windows).

Of course, if you don't like that, then you need to reduce the number of characters you output per line. In your code, each price gets 6 characters, and you are outputting 100 prices per line, so over 600 characters are written for each line. This works fine, but not all text editors can read a 600 character single line.
 
You da man or woman, not really sure, but you post was what exactly the issue. A lot of time wasted on something so simple.

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

Part and Inventory Search

Sponsor

Back
Top