#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep 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();
    		
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    > 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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep 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.

IMN logo majestic logo threadwatch logo seochat tools logo