So basically with my code I take inputfile (warehouse.txt
) add extra info to it (fruitname
, fruitquantity
) from an array then put all the info from this file (warehouse.txt
) PLUS the extra info from array to an output file (updated.txt
).
Now the issue is I want to use this output file ("updated.txt") as an input file and in my code you'll see how I've done this. The problem is that when I open the file (updataed.txt
) to try and use it an input file the strategic cout
says it can't open file so it cant aggregate my data (main aim of program to sort all files so there's only one fruit name with its corresponding quantity): (look at lines 35 and 36 and tell me please if I can/cant do that to an inputfile?)
Run the program yourselves and you'll see that it didn't open updated.txt
, just make a quick txt file warehouse.txt
e.g
{
apple 4
apple 2
pear 3
mango2
pear 3
}
My code currently looks like this:
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
typedef struct items {
string name;
int quantity;
} items_t;
void fileopenchecker(ifstream &FILE);
void printfile(items_t fruit [], int entries);
int readfromfile(ifstream &FILE, items_t fruit []);
int extrarray(items_t fruit []);
void writetooutputfile(ofstream &OFILE, items_t fruit [], int size);
int aggregatedataA(items_t overallfruit [], items_t samefruit [], items_t uniquefruit [], int num);
int aggregatedataB(items_t overallfruit [], items_t samefruit [], items_t uniquefruit [], int num);
int main()
{
const int MAX_SIZE = 150;
int Nfruit = 0;
int Nextrafruit = 0;
int total;
int A, B;
ifstream infile("warehouse.txt"), ffile("updated.txt");
ofstream outfile("updated.txt"), overallfile("FINAL.txt");
items_t extrafruit[MAX_SIZE], fruit[MAX_SIZE], overallfruit[MAX_SIZE], samefruit[MAX_SIZE], uniquefruit[MAX_SIZE];
fileopenchecker(infile);
Nextrafruit = extrarray(extrafruit);
Nfruit = readfromfile(infile, fruit);
infile.close();
writetooutputfile(outfile, fruit, Nfruit);
writetooutputfile(outfile, extrafruit, Nextrafruit);
outfile.close();
fileopenchecker(ffile);
total = readfromfile(ffile, overallfruit);
A = aggregatedataA(overallfruit, samefruit, uniquefruit, total);
B = aggregatedataB(overallfruit, samefruit, uniquefruit, total);
ffile.close();
writetooutputfile(overallfile, samefruit, A);
writetooutputfile(overallfile, uniquefruit, B);
overallfile.close();
return 0;
}
int aggregatedataA(items_t overallfruit [], items_t samefruit [], items_t uniquefruit [], int num){
int i, j, x = 0, y = 0;
for (i = 0; i < num; i++){
for (j = 0; j < num; j++){
if (overallfruit[i].name == overallfruit[j].name){
samefruit[x].name = overallfruit[i].name;
samefruit[x].quantity = overallfruit[i].quantity + overallfruit[j].quantity;
x++;
}
else{
uniquefruit[y].name = overallfruit[i].name;
uniquefruit[y].quantity = overallfruit[i].quantity;
y++;
}
}
}
return x;
}
int aggregatedataB(items_t overallfruit [], items_t samefruit [], items_t uniquefruit [], int num){
int i, j, x = 0, y = 0;
for (i = 0; i < num; i++){
for (j = 0; j < num; j++){
if (overallfruit[i].name == overallfruit[j].name){
samefruit[x].name = overallfruit[i].name;
samefruit[x].quantity = overallfruit[i].quantity + overallfruit[j].quantity;
x++;
}
else{
uniquefruit[y].name = overallfruit[i].name;
uniquefruit[y].quantity = overallfruit[i].quantity;
y++;
}
}
}
return y;
}
void fileopenchecker(ifstream &FILE){
if (!FILE.is_open()){
cout << "Your file was NOT detected!" << endl;
exit(1);
}
else{
cout << "Your file was detected" << endl;
}
}
void printfile(items_t fruit [], int entries){
int i;
cout << "Printing from file!" << endl;
for (i = 0; i < entries; i++){
cout << fruit[i].name << "," << fruit[i].quantity;
}
}
int readfromfile(ifstream &FILE, items_t fruit []){
int entries = 0;
while (!FILE.eof()){
FILE >> fruit[entries].name >> fruit[entries].quantity;
cout << fruit[entries].name << fruit[entries].quantity << endl;
entries++;
}
return entries;
}
int extrarray(items_t fruit []){
int runner = 1, exentries = 0;
while (runner == 1){
cout << "Would you like to add entries to your file? (YES-->1 NO-->0)" << endl;
cin >> runner;
if (runner == 0){
break;
}
//take the itemname and quantity and stores it in the array.
cout << "Enter the name of the fruit and its quantity" << endl;
cin >> fruit[exentries].name >> fruit[exentries].quantity;
//debugging:
cout << fruit[exentries].name << fruit[exentries].quantity << endl;
exentries++;
}
return exentries;
}
void writetooutputfile(ofstream &OFILE, items_t fruit [], int size){
int entries = 0;
while (entries < size){
cout << fruit[entries].name << fruit[entries].quantity << endl;
OFILE << fruit[entries].name << fruit[entries].quantity << endl;
entries++;
}
}