The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Need help getting information from a file and putting it in an array...
Discuss Need help getting information from a file and putting it in an array... in the C Programming forum on Dev Shed. Need help getting information from a file and putting it in an array... C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 12th, 2013, 12:03 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 16
Time spent in forums: 2 h 41 m 15 sec
Reputation Power: 0
|
|
|
Need help getting information from a file and putting it in an array...
Here is the assignment:
You and one other programmer have been hired by MegaBankCorp to write a program in C++ to
process currency conversions. The bank president himself has taken the time to write a
description of the program requirements on the back of a napkin:
Program must convert to and from US dollars. Ask user whether converting to or
from, then what other currency and what amount. Display all available currencies
and current rates. Calculate conversion with 5% bank fee deducted from final
amount.
In addition, your manager has pointed out that the bank handles many conversions per day, and
the rates can change at any time, so the program should handle only one conversion at a time and
currency rates must be loaded from a file named “rates.txt”.
Here is an example file:
Australian Dollars
0.96
British Pound Sterling
0.63
Canadian Dollars
1.00
Euro
0.74
Russian Rubles
30.01
STOP
Each currency is stored in two lines: the first line specifies the currency name, the second the
current exchange rates. The final line reads “STOP,” indicating that all records have been
processed.
Here is what I have so far. Right now the program is set to read from a file, however, it does not read until it sees STOP. It reads until the assigned length is met. I need it to be able to loop through and assign the info to an array until it sees stop, and I am having trouble figuring out exactly how to do that.
Code:
#include <iostream>
#include <fstream>
#include <cctype>
#include <sstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream inFile;
ofstream outFile;
int numRecords;
string inputFilename = "rates.txt";
string userChoice;
string menuChoice;
stringstream breakString1, breakString2, breakString3, breakString4, breakString5;
double num1, num2, num3, num4, num5;
double conversionAmount;
double conversionTotal;
double bankCharge;
double bankFee = .05;
string doAnother;
// Open the files using a C string (Will not take a C++ string)
inFile.open(inputFilename.c_str());
// Error message if file fails to open
if (inFile.is_open() == false) {
cerr << "ERROR: Cannot open input file. Exiting." << endl;
return 1;
}
cout << "--------------------------------------------------------------------------" << endl << endl;
// Ask if they are converting to or from US Dollars.
cout << "Do you wish to convert to or from US Dollars? (to/from)" << endl
<< "Your choice: " ;
getline(cin, userChoice);
cout << endl;
if (userChoice == "to"){
cout << "Select a currency to convert from(A,B,C,D,E): " << endl << endl;
// Input number of records -- first line of the file
inFile >> numRecords;
inFile.ignore(256, '\n');
// Declare Array
string currencyRate[10];
// Store information into the array
for (int i = 0; i < numRecords; i++) {
getline(inFile, currencyRate[i]);
}
// Print currency/rate menu from the array
for (int i = 0; i < numRecords; i++) {
cout << currencyRate[i] << endl;
}
cout << endl;
/* Access elements of the array
cout << currencyRate[1] << endl
<< currencyRate[3] << endl
<< currencyRate[5] << endl
<< currencyRate[7] << endl
<< currencyRate[9] << endl;
*/
// Request selection from currency menu
cout << "Your selection: ";
cin >> menuChoice;
// Request conversion amount
cout << "How much are you converting? " << "$";
cin >> conversionAmount;
// Convert strings to doubles
breakString1 << currencyRate[1];
breakString1 >> num1;
breakString2 << currencyRate[3];
breakString2 >> num2;
breakString3 << currencyRate[5];
breakString3 >> num3;
breakString4 << currencyRate[7];
breakString4 >> num4;
breakString5 << currencyRate[9];
breakString5 >> num5;
// Print conversion calculations
if (menuChoice == "A") {
conversionTotal = conversionAmount * num1;
}
else if(menuChoice == "B") {
conversionTotal = conversionAmount * num2;
}
else if(menuChoice == "C") {
conversionTotal = conversionAmount * num3;
}
else if(menuChoice == "D") {
conversionTotal = conversionAmount * num4;
}
else {
conversionTotal = conversionAmount * num5;
}
// Print conversion total
cout << "Your converted currency total is " << "$" << fixed << setprecision(2) << conversionTotal << endl;
// Calculate total with 5% bank fee included
bankCharge = conversionTotal * bankFee;
cout << fixed << setprecision(2) << "The bank fee for this transaction is " << "$" << bankCharge << endl;
cout << "Your total return is $" << conversionTotal - bankCharge << endl;
}
if (userChoice == "from"){
cout << "Select a currency to convert to(A,B,C,D,E): " << endl << endl;
// Input number of records -- first line of the file
inFile >> numRecords;
inFile.ignore(256, '\n');
// Declare Array
string currencyRate[10];
// Store information into the array
for (int i = 0; i < numRecords; i++) {
getline(inFile, currencyRate[i]);
}
// Print currency/rate menu from the array
for (int i = 0; i < numRecords; i++) {
cout << currencyRate[i] << endl;
}
cout << endl;
/* Access elements of the array
cout << currencyRate[1] << endl
<< currencyRate[3] << endl
<< currencyRate[5] << endl
<< currencyRate[7] << endl
<< currencyRate[9] << endl;
*/
// Request selection from currency menu
cout << "Your selection: ";
cin >> menuChoice;
// Request conversion amount
cout << "How much are you converting? " << "$";
cin >> conversionAmount;
// Convert strings to doubles
breakString1 << currencyRate[1];
breakString1 >> num1;
breakString2 << currencyRate[3];
breakString2 >> num2;
breakString3 << currencyRate[5];
breakString3 >> num3;
breakString4 << currencyRate[7];
breakString4 >> num4;
breakString5 << currencyRate[9];
breakString5 >> num5;
// Print conversion calculations
if (menuChoice == "A") {
conversionTotal = conversionAmount / num1;
}
else if(menuChoice == "B") {
conversionTotal = conversionAmount / num2;
}
else if(menuChoice == "C") {
conversionTotal = conversionAmount / num3;
}
else if(menuChoice == "D") {
conversionTotal = conversionAmount / num4;
}
else {
conversionTotal = conversionAmount / num5;
}
// Print conversion total
cout << "Your converted currency total is " << "$" << fixed << setprecision(2) << conversionTotal << endl;
// Calculate total with 5% bank fee included
bankCharge = conversionTotal * bankFee;
cout << fixed << setprecision(2) << "The bank fee for this transaction is " << "$" << bankCharge << endl;
cout << "Your total return is $" << conversionTotal - bankCharge << endl;
}
// Close the file
inFile.close();
}
|

February 12th, 2013, 12:16 PM
|
 |
Contributed User
|
|
|
|
|
> Each currency is stored in two lines: the first line specifies the currency name, the second the
> current exchange rates. The final line reads “STOP,” indicating that all records have been
OK, so what do you mean here then?
> // Input number of records -- first line of the file
> inFile >> numRecords;
> inFile.ignore(256, '\n');
Oh, and learn about functions.
Cramming everything into a 200+ line main (and it isn't even finished yet) doesn't make for readable (or testable) code.
|

February 12th, 2013, 12:23 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 16
Time spent in forums: 2 h 41 m 15 sec
Reputation Power: 0
|
|
|
Oh, sorry I forgot to explain that. The way the program is set up right now is based off of a program we did in class in which the .txt file started with an integer indicating the number of items stored. That line is there, so that when the program starts reading the information into the array, it ignores the number on the first line. That is the things that I need to change.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|