#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const int ARRAY_SIZE = 1000; // Array size
int accountNumbers[ARRAY_SIZE]; // Array with 1000 elements
double accountBalances[ARRAY_SIZE]; // Loop counter variable
int count; // Input file stream object
ifstream inputFile;
// Open the file.
inputFile.open("Final_Random_Accounts_Unsorted.txt.rtf");
// Read the numbers from the file into the array
while (count < ARRAY_SIZE && inputFile >> accountNumbers[count] >> accountBalances[count]) {
count++;
}
inputFile.close();
// Display the read data
cout << "The bank account numbers are: ";
for (count = 0; count < ARRAY_SIZE; count++) {
cout << accountNumbers[count] << " " << endl;
}
cout << "The bank balances are: ";
for (count = 0; count < ARRAY_SIZE; count++) {
cout << accountBalances[count] << " " << endl;
}
}
So this is what I started with, not to familiar with reading files honestly.
My results when I run gives me out of mostly 0's for bank accounts results and few large random numbers in there, the results for bank balances gives me extremely odd numbers, ex: 2.435325e-310
The file I am attempting to read has 1000 "bank account numbers" along with there balances.
Consider using a struct instead of 2 arrays as with sorting etc the relationship b/w a/c numbers and a/c balances might break quite easily. Also std::vector over C-style arrays:
@Chris213, I assume that your input file is a simple ASCII text file; i.e. you could open it with a simple text editor like notepad. The name of your file suggests otherwise.
#include <iostream>
#include <fstream>
int main()
{
constint ARRAY_SIZE = 1000; // Array size
int accountNumbers[ARRAY_SIZE] = {0} ; // initialize to all zeroes
double accountBalances[ARRAY_SIZE] = {0} ; // initialize to all zeroes
// Open the file. important: make sure that the file is a
// plain text file; not one in the rich text format (.rtf)
std::ifstream inputFile( "Final_Random_Accounts_Unsorted.txt" );
// verify that the file was opened
if( !inputFile.is_open() ) {
std::cerr << "failed to open input file\n" ;
return 1 ;
}
int count = 0 ; // actual count of items read; initialise to zero
// Read the numbers from the file into the array
while( count < ARRAY_SIZE && inputFile >> accountNumbers[count] >> accountBalances[count] ) {
++count;
}
// count now holds the actual number of items read; do not modify it after this point
inputFile.close();
// Display the read data
std::cout << "The bank account numbers and balances are: ";
for( int i = 0; i < count ; ++i ) { // for each pair of values up to count
// print the values
std::cout << accountNumbers[i] << ' ' << accountBalances[i] << '\n' ;
}
}