Hello! I need some assistance with a somewhat complex code of mine. Here's the scenario:
The program is supposed to catalog the deliveries made delivery system based within our solar system, transporting objects from planet to planet. Luckily, I've gotten all of the math down-pat. Here's where I'm having trouble, however:
I'm supposed to read in the relevant information about each delivery from an input file, ignoring the data that is "corrupted" -- AKA, not in the correct format.
Here's how it's supposed to look:
<Service Code><space><Shipping Planet Code><space><Delivery Planet Code><space><Weight of Package>
An example description:
F M E 4
Or a First-Class delivery from Mars to Earth, weighing 4 pounds.
Like I said earlier, I already have all of the math figured out. What I can't seem to do, however, is skip the corrupted data. Here's a sample input I wrote. I want to skip the corrupted lined, but I can't somehow:
P M J 5
E J S 4
asdfghjk (corrupted)
zdfghjkl (corrupted)
E U N 6
F M E 5
T H U J
Below is the code I'm currently messing with to skip the irrelevant characters. I chose four characters because each description is supposed to be composed of only 4 characters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
const int ARRAY_SIZE = 50;
int datalog[ARRAY_SIZE];
int count = 0;
ifstream shipments_file; // File stream object.
// Open the file for input.
shipments_file.open("shipments.txt");
while (count < ARRAY_SIZE && shipments_file >> datalog[count])
{
count++;
}
// Test for errors.
if (!shipments_file)
{
cout << "Error opening file. Program aborting.\n";
return 0;
}
cout << "Here is the information in the file:\n\n";
char ch1, ch2, ch3, ch4; // ch1 = Service Type, ch2 = Shipping Planet Code, ch3 = Destination Planet Code, ch4 = Shipment Weight
for (int count = 0; count < ARRAY_SIZE; count++)
{
// Prepare the record.
shipments_file.get(ch1);
{
if (isspace(ch1))
{
shipments_file.ignore();
}
if (isalpha(ch1))
{
if (!isupper(ch1))
{
shipments_file.ignore();
}
}
else if (!isalpha(ch1))
{
shipments_file.ignore();
}
}
shipments_file.ignore();
shipments_file.get(ch2);
{
if (isspace(ch2))
{
shipments_file.ignore();
}
if (isalpha(ch2))
{
if (!isupper(ch2))
{
shipments_file.ignore();
}
}
else if (!isalpha(ch2))
{
shipments_file.ignore();
}
}
shipments_file.ignore();
shipments_file.get(ch3);
{
if (isspace(ch3))
{
shipments_file.ignore();
}
if (isalpha(ch3))
{
if (!isupper(ch3))
{
shipments_file.ignore();
}
}
else if (!isalpha(ch3))
{
shipments_file.ignore();
}
}
shipments_file.ignore();
shipments_file.get(ch4);
{
if (isspace(ch4))
{
shipments_file.ignore();
}
if (!isdigit(ch4))
{
shipments_file.ignore();
}
}
shipments_file.ignore();
|
Thank you very much in advance for any and all assistance!
EDIT: Furthermore, I would also like to say that, for whatever reason, I am unable to put the contents of the file into an array, despite using the following while loop (featured on lines 10-13 above):
1 2 3 4
|
while (count < ARRAY_SIZE && shipments_file >> datalog[count])
{
count++;
}
|