Here is the code I've tried. Although I used the full path to the file and the file is located in the same directory as my program executable, when I run it is doesn't read the text file #include #include #include #include using namespace std; void loadArrays(string product[], int quantity[], float cost[], int& counter) { ifstream file; //ifstream function string inLine, prod, qty, cst; //variables to hold read line temporarily //open input file file.open("C:\\Users\\Honeypie\\source\\repos\\Project4\\inventory.txt", ios::in); while (getline(file, inLine)) { //read first line istringstream ss(inLine); //split the line using a delimeter //you may change this delimeter according to your input file getline(ss, prod, '|'); getline(ss, qty, '|'); getline(ss, cst, '|'); if (prod != "Product Code") { //if statement that will ignore the column headers product[counter] = prod; //insert product code to product array quantity[counter] = stoi(qty); //insert quantity to quantity array. stoi is for string to int conversion. cost[counter] = stof(cst); //insert cost to cost array. stof is for string to float conversion. counter++; //add count value. } } file.close(); } void calculateValues(int quantity[], float cost[], float value[], int& counter) { /*calculate for the total value of each product then insert it in value array.*/ for (int i = 0; i < counter; i++) { value[i] = quantity[i] * cost[i]; } } string GetProd(string product[], float value[], int& counter) { string tempProd = product[0]; //declare a temporary product with the highest value. float tempValue = value[0]; //declare a temporary highest value. for (int i = 0; i < counter; i++) { if (value[i] > tempValue) { //if the current value is greater than temporary highest value. tempValue = value[i]; //change the tempValue with the new highest value. tempProd = product[i]; //change the tempProd with the product of the new highest value. } } //return the product with the highest value. return tempProd; } float GetAverage(int quantity[], float value[], int& counter) { float totalVal = 0, aveVal = 0; int totalQty = 0; for (int i = 0; i < counter; i++) { //addition of all quantities and values from the array. totalQty = totalQty + quantity[i]; totalVal = totalVal + value[i]; } //get average value by dividing total value by the total quantity on hand. aveVal = totalVal / totalQty; return aveVal; //return the average value. } void displayTable(string product[], int quantity[], float cost[], float value[], int& counter) { /* This function displays the table with the respective values Displays the product with the highest value Displays the average value of an inventory item */ cout << "Product Code\tQty on Hand\tCost Each\tTotal Value" << endl; for (int i = 0; i < counter; i++) { cout << product[i]; cout << right << setw(22) << quantity[i]; cout << right << setw(14) << cost[i]; cout << right << setw(18) << value[i]; if (value[i] > 9000) { cout << " !!!"; } cout << endl; } cout << endl; //call function GetProd to get the product with the highest value. cout << "The product with the highest inventory value is " << GetProd(product, value, counter) << "." << endl; //call GetAverage function to get the average value. //setprecision() makes float display 2 decimal places. cout << fixed << setprecision(2) << "The average cost of an inventory item is $" << GetAverage(quantity, value, counter) << "." << endl; } int main() { const int SIZE = 20; //declaring the array size. /* Declaration of array for product, quantity, cost, and value. Declaration of variable counter. */ string product[SIZE]; int quantity[SIZE], counter = 0; float cost[SIZE], value[SIZE]; //calls the loadArrays function to read data from file then insert it to the arrays. loadArrays(product, quantity, cost, counter); //calls calculateValues function to compute for the total value of each product. calculateValues(quantity, cost, value, counter); //calls displayTable function to display all the gathered and computed data. displayTable(product, quantity, cost, value, counter); return 0; }
Here is the code I've tried. Although I used the full path to the file and the file is located in the same directory as my program executable, when I run it is doesn't read the text file
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;
void loadArrays(string product[], int quantity[], float cost[], int& counter) {
ifstream file; //ifstream function
string inLine, prod, qty, cst; //variables to hold read line temporarily
//open input file
file.open("C:\\Users\\Honeypie\\source\\repos\\Project4\\inventory.txt", ios::in);
while (getline(file, inLine)) { //read first line
istringstream ss(inLine);
//split the line using a delimeter
//you may change this delimeter according to your input file
getline(ss, prod, '|');
getline(ss, qty, '|');
getline(ss, cst, '|');
if (prod != "Product Code") { //if statement that will ignore the column headers
product[counter] = prod; //insert product code to product array
quantity[counter] = stoi(qty); //insert quantity to quantity array. stoi is for string to int conversion.
cost[counter] = stof(cst); //insert cost to cost array. stof is for string to float conversion.
counter++; //add count value.
}
}
file.close();
}
void calculateValues(int quantity[], float cost[], float value[], int& counter) {
/*calculate for the total value of each product
then insert it in value array.*/
for (int i = 0; i < counter; i++) {
value[i] = quantity[i] * cost[i];
}
}
string GetProd(string product[], float value[], int& counter) {
string tempProd = product[0]; //declare a temporary product with the highest value.
float tempValue = value[0]; //declare a temporary highest value.
for (int i = 0; i < counter; i++) {
if (value[i] > tempValue) { //if the current value is greater than temporary highest value.
tempValue = value[i]; //change the tempValue with the new highest value.
tempProd = product[i]; //change the tempProd with the product of the new highest value.
}
}
//return the product with the highest value.
return tempProd;
}
float GetAverage(int quantity[], float value[], int& counter) {
float totalVal = 0, aveVal = 0;
int totalQty = 0;
for (int i = 0; i < counter; i++) {
//addition of all quantities and values from the array.
totalQty = totalQty + quantity[i];
totalVal = totalVal + value[i];
}
//get average value by dividing total value by the total quantity on hand.
aveVal = totalVal / totalQty;
return aveVal; //return the average value.
}
void displayTable(string product[], int quantity[], float cost[], float value[], int& counter) {
/*
This function displays the table with the respective values
Displays the product with the highest value
Displays the average value of an inventory item
*/
cout << "Product Code\tQty on Hand\tCost Each\tTotal Value" << endl;
for (int i = 0; i < counter; i++) {
cout << product[i];
cout << right << setw(22) << quantity[i];
cout << right << setw(14) << cost[i];
cout << right << setw(18) << value[i];
if (value[i] > 9000) {
cout << " !!!";
}
cout << endl;
}
cout << endl;
//call function GetProd to get the product with the highest value.
cout << "The product with the highest inventory value is " << GetProd(product, value, counter) << "." << endl;
//call GetAverage function to get the average value.
//setprecision() makes float display 2 decimal places.
cout << fixed << setprecision(2) << "The average cost of an inventory item is $" << GetAverage(quantity, value, counter) << "." << endl;
}
int main()
{
const int SIZE = 20; //declaring the array size.
/*
Declaration of array for product,
quantity, cost, and value.
Declaration of variable counter.
*/
string product[SIZE];
int quantity[SIZE], counter = 0;
float cost[SIZE], value[SIZE];
//calls the loadArrays function to read data from file then insert it to the arrays.
loadArrays(product, quantity, cost, counter);
//calls calculateValues function to compute for the total value of each product.
calculateValues(quantity, cost, value, counter);
//calls displayTable function to display all the gathered and computed data.
displayTable(product, quantity, cost, value, counter);
return 0;
}
Step by step
Solved in 4 steps with 1 images