Hello, I'm in a basic logic class and every week my professor has us copy a C++
program line by line. This week we're doing using a loop to populate an array with external data... I'm 99% positive I copied every thing right, but when I run
the program, it wont get passed populating the array, it just keeps outputting "4634784 was put into the array" I can't find the mistake!!
inputFile.dat is a wordpad file containing:
101
102
103
104
105
106
/*
Project 9
Title: making an array, a loop, and a pinch of data retrieval
Purpose: Write a program that imports data
Programmer: Me
*/
int main()
{
int item, quantity;
double price = 0.0;
const int SIZE = 6;
int valid_item[SIZE];
double valid_item_price[]={0.89, 5.99, 7.50, 15.99, 19.50, 59.00};
int sub;
bool foundIt = false;
const string MSG_YES = "Item found";
const string MSG_NO = "Item not found";
ifstream data_in;
// Load the data into the array
// Initialize subscript to 0
sub = 0;
// Open input and output file
data_in.open("inputFile.dat");
// Loop through the file
while(!(data_in.eof()))
{
// Input the data value into the array
data_in >> valid_item[sub];
// Show the data was loaded
cout << valid_item[sub] << " was stored in the array " << endl;
sub +=1 ; // Increment loop index
}
data_in.close();
cout << endl;
// Get user input
cout << "Enter item number between 1 and 999: ";
cin >> item;
// Initialize subscript to 0
sub = 0;
cout << endl;
// Search the array for a match -- exit if item is found
while((sub<SIZE)&&(!(foundIt)))
{
// Show the search
cout << valid_item[sub] << " was compared" <<endl;
// test to see if this item is the desired item
if(item == valid_item[sub])
{
foundIt = true;
price = valid_item_price[sub];
}
sub +=1;
}
// Test value of foundIt
if(foundIt)
{
cout << endl;
cout << MSG_YES << endl;
cout << "Enter quantity: ";
cin >> quantity;
cout << setprecision(2)
<< setiosflags(ios::fixed | ios::showpoint);
cout << endl << quantity << " at $" << price << " each" << endl <<endl;
cout << "Total: $" << quantity * price << endl << endl;
}
else
cout << endl << MSG_NO << endl;