Need Help for C++ please: Production line Ask the user to read a file. The file
ID: 3575113 • Letter: N
Question
Need Help for C++ please:
Production line
Ask the user to read a file. The file will be in the same format as items.txt. There will always be a list of items with a name and price followed by some amount of recipes. If a recipe for an item is not present, the only way to make the item is to buy it directly. Make a program that reads all the items and recipes, then says how much profit can be made by making each item.
If an item has no recipe, you would buy that item then resell it for the same price and make a profit of 0. If an item does have a recipe, you would buy the materials to make this item and subtract this cost from the price of the final product.
There will only be zero or one recipe per item. The items will always be listed first. The names of items will always be a single word (using a _ to join names that are normally multiple words). You may assume there will be less than 50 items and each recipe will use less than 50 other items to create a final product.
Example 1 (user input is bolded):
What file to load?
items1.txt
Making Wood, profit=0
Making Metal, profit=0
Making Cat, profit=0
Making Spear, profit=40.2
Example 2 (user input is bolded):
What file to load?
items2.txt
Making Lint, profit=0
Making Rubber_Chicken, profit=0
Making Wood, profit=0
Making Glasses, profit=0
Making Substitute_Teacher, profit=65.8
Making Cat_Toy, profit=49.95
Making Scare_Crow, profit=100.58
Making Dog_Toy, profit=20
////////////////////////////////////////////////////////////////////////
BTW items1.txt is
Item: Wood 2.5
Item: Metal 5.5
Item: Cat 900
Item: Spear 50.7
Recipe: Spear = Wood + Wood + Metal ;
and Items2.txt is
Item: Lint 0.01
Item: Rubber_Chicken 20
Item: Wood 15.2
Item: Glasses 4.2
Item: Substitute_Teacher 90
Item: Cat_Toy 50
Item: Scare_Crow 120
Item: Dog_Toy 90
Recipe: Substitute_Teacher = Rubber_Chicken + Glasses ;
Recipe: Cat_Toy = Lint + Lint + Lint + Lint + Lint ;
Recipe: Scare_Crow = Wood + Lint + Lint + Glasses ;
Recipe: Dog_Toy = Cat_Toy + Rubber_Chicken ;
So far, I have done code like this but getting errors while running txt files.
#include
#include
#include
#include
#include
#include
using namespace std;
string findnextstring(string str, int beginindex);
int dividestring(string str2, string a[], int max_size);
int main()
{
ifstream in_stream;
string fileName;
cout << "Enter the file name : ";
cin >> fileName;
in_stream.open(fileName.c_str());
if (in_stream.fail())
{
cout << "File could not be opened." << endl;
exit(1);
}
string items[50];
double items_value[50];
double profit[50];
string myrecap[50];
string rname = myrecap[0];
int itemcount = 0;
string myfilelines;
while(getline(in_stream, myfilelines))
{
if(myfilelines.substr(0,5) == "Item:")
{
int begin = myfilelines.find_first_of(' ') + 1;
int nextfreespace = myfilelines.find(" ", begin);
items_value[itemcount] = atof(myfilelines.substr(nextfreespace).c_str());
//cout << items_value[itemcount];
items[itemcount] = myfilelines.substr (begin, myfilelines.find_first_of(' ', begin) - begin);
profit[itemcount]=0;
itemcount++;
cout << "Making " << items[itemcount-1] << ", profit = 0" << endl;
//populate profit[50]
}
else if (myfilelines.substr(0,7) == "Recipe:")
{
int max_size = myfilelines.length();
int count = dividestring(myfilelines,myrecap,max_size);
cout << "count : " << count << endl;
cout << "maxsize: " << max_size << endl;
cout << "item value: " << items_value[itemcount]< for(int i=2; i< count; i++)
{
for(int j= 0; j {
if((myrecap[i] == items[j]) && (myrecap[i] != "+") && (myrecap[i] != ";"))
{
profit[itemcount] += items_value[j];
}
}
}
for(int k = 0; k < count; k++)
{
if((myrecap[1] == items[k]))
{
profit[k] = items_value[k];
cout << "Making " << items[k] << ", " << "profit = "< }
}
}
}
return 0;
}
string findnextstring(string str, int beginindex)
{
int z=0;
z=str.find(' ',beginindex);
z=z-beginindex;
str=str.substr(beginindex,z);
return str;
}
int dividestring(string str2, string a[], int max_size)
{
int y;
int num = 0;
for (y=0; y {
a[y] = findnextstring(str2,num);
num = num + a[y].length()+1;
if(num >= str2.length())
{
y++;
break;
}
}
return y;
}
///////////////////////////////////////////////////////////////////////////////////////
Instead of printing like
What file to load?
items1.txt
Making Wood, profit=0
Making Metal, profit=0
Making Cat, profit=0
Making Spear, profit=40.2
Example 2 (user input is bolded):
What file to load?
items2.txt
Making Lint, profit=0
Making Rubber_Chicken, profit=0
Making Wood, profit=0
Making Glasses, profit=0
Making Substitute_Teacher, profit=65.8
Making Cat_Toy, profit=49.95
Making Scare_Crow, profit=100.58
Making Dog_Toy, profit=20
I am getting
What file to load?
items1.txt
Making Wood, profit = 0
Making Metal, profit = 0
Making Cat, profit = 0
Making Spear, profit = 0 <- I am getting unnecessary Spear item.
count : 9
maxsize: 37
item value: 0
Making Spear, profit = 40.2
What file to load?
items2.txt
Making Lint, profit = 0
Making Rubber_Chicken, profit = 0
Making Wood, profit = 0
Making Glasses, profit = 0
Making Substitute_Teacher, profit = 0
Making Cat_Toy, profit = 0
Making Scare_Crow, profit = 0
Making Dog_Toy, profit = 0
count : 7
maxsize: 55
item value: 0
Making Substitute_Teacher, profit = 65.8count : 13
maxsize: 52
item value: 0
Making Cat_Toy, profit = 25.75count : 11
maxsize: 51
item value: 0
Making Scare_Crow, profit = 76.33count : 7
maxsize: 44
item value: 0
In Items2.txt, I am not getting Actual profit of Dog_Toy, but getting unnecessary "profit = 0" for "Items" even though some of them were "Recipes"
Can anyone fix my problem?
Explanation / Answer
Solution:
Code:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
string findnextstring(string str, int beginindex);
int dividestring(string str2, string a[], int max_size);
int main()
{
ifstream in_stream;
string fileName;
cout << "Enter the file name : ";
getline(cin,fileName);
in_stream.open(fileName);
if (in_stream.fail())
{
cout << "File could not be opened." << endl;
exit(1);
}
string items[50];
double items_value[50];
double profit[50];
string myrecap[50];
string rname = myrecap[0];
int itemcount = 0;
string myfilelines;
while(getline(in_stream, myfilelines))
{
if(myfilelines.substr(0,5) == "Item:")
{
int begin = myfilelines.find_first_of(' ') + 1;
int nextfreespace = myfilelines.find(" ", begin);
items_value[itemcount] = atof(myfilelines.substr(nextfreespace).c_str());
//cout << items_value[itemcount];
items[itemcount] = myfilelines.substr (begin, myfilelines.find_first_of(' ', begin) - begin);
profit[itemcount]=0;
itemcount++;
//cout << "Making " << items[itemcount-1] << ", profit = " <<profit[itemcount]<< endl;
//populate profit[50]
}
else if (myfilelines.substr(0,7) == "Recipe:")
{
int k=0;
int max_size = myfilelines.length();
int count = dividestring(myfilelines,myrecap,max_size);
int ind;
float cost;
for(int j= 0; j<count;j++)
{
if((myrecap[1] == items[j]) )
{
cost= items_value[j];
ind=j;
}
}
for(int i=2; i< count; i++)
{
for(int j= 0; j<count;j++)
{
profit[ind] = cost-profit[ind];
if((myrecap[i] == items[j]) && (myrecap[i] != "+") && (myrecap[i] != ";"))
{
profit[ind] += items_value[j];
}
}
}
}
}
for(int j=0;j<itemcount;j++)
{
cout << "Making " << items[j] << ", " << "profit = "<<profit[j]<<endl;
}
system("pause");
return 0;
}
string findnextstring(string str, int beginindex)
{
int z=0;
z=str.find(' ',beginindex);
z=z-beginindex;
str=str.substr(beginindex,z);
return str;
}
int dividestring(string str2, string a[], int max_size)
{
int y=0;
int num = 0;
while(true) {
a[y] = findnextstring(str2,num);
num = num + a[y].length()+1;
if(num >= str2.length())
{
y++;
break;
}
y++;
}
return y;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.