Jan 31, 2011 at 9:43pm UTC
Hello. I'm a newbie learning C++ and i've run into a problem with reading info from a .txt file.
This is what I have so far:
1 2 3 4 5 6
Inny >> getline(cin,railroad_name)
>> getline(cin,plant_destination)
>> num_ore_cars
>> cin.ignore('\n' )
>> cin.get(dollar_sign)
>> cost_per_ton_coal;
The text file would have something like:
blah, and blah railroad
plant name #10
15
$55.21
What i'm trying to read in is the first line into a string named railroad_name, the second line into a string named plant_destination, ignore the white space then read the number on the 3rd line into num_ore_cars, then ignore the dollar sign or (eat it) then finally retrieve the price following the dollar sign into cost_per_ton_coal.
Also, this is obviously not the entire program it's the section related to reading the information in from an already open text file. Note: I am not allowed to use any loops to do this.
Please help me! I've looked at 20 different examples around the web about ignoring white space etc, I know my formatting is off somewhere.
Last edited on Jan 31, 2011 at 10:22pm UTC
Jan 31, 2011 at 10:01pm UTC
[co de] "Your code goes here"
[/co de]
That code is for reading from cin.
Jan 31, 2011 at 10:25pm UTC
What do I need to change in order to make it read from the file instead? I'm not too familiar with getline. I understand the "while" examples i've seen on various web sites but i'm not allowed to use while or eof etc. My textbook has examples of reading from keyboard but that's not what i'm trying to do in this instance.
Jan 31, 2011 at 10:30pm UTC
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
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const double ORE_PER_CAR = 74.5;
const double SURCHARGE = 0.035;
int main()
{
string railroad_name;
string plant_destination;
int num_ore_cars;
char dollar_sign;
double cost_per_ton_coal;
double total_weight_coal;
double total_value_shipment;
double total_with_surcharge;
double total_surcharge;
ifstream Inny;
ofstream Outty;
Inny.open ("MP2_IN.txt" );
Outty.open ("MP2_OUT.out" );
Inny >> getline(railroad_name)
>> getline(plant_destination)
>> num_ore_cars
>> cin.ignore('\n' )
>> cin.get(dollar_sign)
>> cost_per_ton_coal;
cout << railroad_name;
Outty << fixed << showpoint << setprecision(2)
<< "123456789012345678901234567890123456789012345678901234567890\n"
<< "KERBAA&M COAL TRAIN REPORT - <Name>\n\n"
<< "Railroad Name: " << railroad_name << "\n"
<< "Destination: " << plant_destination << "\n\n"
<< "Number of ore cars:" << num_ore_cars << "\n"
<< "Total weight of coal:" << total_weight_coal << "\n"
<< "Current Cost per short ton:" << cost_per_ton_coal << "\n"
<< "Total Value of Shipment:" << total_value_shipment << "\n"
<< "Current Surcharge:" << SURCHARGE * 100 << "\n\n"
<< "Surcharge(est):" << total_surcharge << "\n"
<< "Total with Surcharge:" << total_with_surcharge << endl;
Inny.close();
Outty.close();
return 0;
}
This is where i'm at right now. The out stuff isn't finished but i'm not worried about that part.
Last edited on Feb 1, 2011 at 12:29am UTC
Jan 31, 2011 at 10:33pm UTC
getline(cin, plant_destination);
That says "read from cin and put it in plant_destination". You need to say "read from myFile and put it in plant_destination".
Also Inny >> getline(cin,railroad_name)
that syntactically wrong (you can't read to an stream). Put the getline, get, ignore, ..., in a separate line.
Jan 31, 2011 at 10:37pm UTC
Fstream reference :
http://cplusplus.com/reference/iostream/fstream/
When you want to read in from a file, use variables to store the incoming data, and you can get the information in exactly the same manner as using cin. i.e.
http://www.wikihow.com/Use-%22i.e.%22-Versus-%22e.g.%22
1 2 3 4 5 6
fstream fin;
fin.open("file.txt" ,ios::in);
string theString = "" ;
char theChar = NULL;
fin >> theString; // theString now holds the first string, it is delineated by spaces
theChar = fin.get();//theChar now holds one char (can be anything including spaces)
edit: guess I should read the whole thing before posting :O
Last edited on Jan 31, 2011 at 10:40pm UTC
Jan 31, 2011 at 11:36pm UTC
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
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const double ORE_PER_CAR = 74.5;
const double SURCHARGE = 0.035;
int main()
{
string railroad_name;
string plant_destination;
int num_ore_cars;
char dollar_sign;
double cost_per_ton_coal;
double total_weight_coal;
double total_value_shipment;
double total_with_surcharge;
double total_surcharge;
ifstream Inny;
ofstream Outty;
Inny.open ("MP2_IN.txt" );
Outty.open ("MP2_OUT.out" );
getline(Inny, railroad_name);
getline(Inny, plant_destination);
Inny >> num_ore_cars;
Inny >> dollar_sign;
Inny >> cost_per_ton_coal;
total_weight_coal = ORE_PER_CAR * num_ore_cars;
total_value_shipment = total_weight_coal * cost_per_ton_coal;
total_surcharge = total_value_shipment * SURCHARGE;
total_with_surcharge = total_surcharge + total_value_shipment;
Outty << fixed << showpoint << setprecision(2)
<< "123456789012345678901234567890"
<< "123456789012345678901234567890\n"
<< "KERBAA&M COAL TRAIN REPORT - <Name>\n\n"
<< "Railroad Name: " << railroad_name << "\n"
<< "Destination: " << plant_destination << "\n\n"
<< setfill('.' ) << left << setw(31)
<< "Number of ore cars:"
<< setfill(' ' ) << right << setw(12)
<< num_ore_cars << "\n"
<< setfill('.' ) << left << setw(31)
<< "Total weight of coal:"
<< setfill(' ' ) << right << setw(12)
<< total_weight_coal << " short tons\n"
<< setfill('.' ) << left << setw(31)
<< "Current Cost per short ton:" << " $"
<< setfill(' ' ) << right << setw(10)
<< cost_per_ton_coal << "\n"
<< setfill('.' ) << left << setw(31)
<< "Total Value of Shipment:" << " $"
<< setfill(' ' ) << right << setw(10)
<< total_value_shipment << "\n"
<< setfill('.' ) << left << setw(31)
<< "Current Surcharge:"
<< setfill(' ' ) << right << setw(12)
<< SURCHARGE * 100 << " %\n\n"
<< setfill('.' ) << left << setw(31)
<< "Surcharge(est):" << " $"
<< setfill(' ' ) << right << setw(10)
<< total_surcharge << "\n"
<< setfill('.' ) << left << setw(31)
<< "Total with Surcharge:" << " $"
<< setfill(' ' ) << right << setw(10)
<< total_with_surcharge << endl;
Inny.close();
Outty.close();
return 0;
}
Thanks guys. Here is the finished product.
Last edited on Feb 1, 2011 at 12:28am UTC