C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 12th, 2013, 12:03 PM
blobman23 blobman23 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 16 blobman23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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();
		
}

Reply With Quote
  #2  
Old February 12th, 2013, 12:16 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,835 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 16 h 3 m 48 sec
Reputation Power: 1774
> 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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old February 12th, 2013, 12:23 PM
blobman23 blobman23 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 16 blobman23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Need help getting information from a file and putting it in an array...

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap