I'm trying to write a program that, you tell it what you want, and it removes one from the quantity. It's compiling just fine and it even sends back the fail message I set up but it won't replace the number I want it to. I'm not allowed to modify the text file in any way.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
usingnamespace std;
struct snacksJustString
{
string name;
string price;
int quant;
};
void display();
snacksJustString getQuant(int);
snacksJustString mabob[5];
int main()
{
for (;;)
{
int I;
display();
cout << "Please enter the number you want. Enter 0 to quit." << endl;
cin >> I;
if (I==0)
{
return 0;
}
elseif (I>5)
{
cout << "not a valid input" << endl;
}
else
{
cout << getQuant(I).name << endl;
}
}
}
void display()
{
fstream doneDone("DrinkMachine.txt");
string thing;
int stuff=6;
int burb = 0;
while (stuff!=0)
{
getline (doneDone, thing);
cout << burb << " " << thing << endl;
burb++;
stuff = stuff-1;
}
doneDone.close();
}
snacksJustString getQuant (int I)
{
bool yaDoneDidIt = false;
for (;;)
{
snacksJustString fail;
fail.name = "fail";
snacksJustString win;
win.name = "win";
int stuff=6;
int twentyThree = 0;
int getname = 1;
int getprice = 1;
int getquant = 1;
string junkString;
fstream doneDone;
doneDone.open("DrinkMachine.txt");
cout << "line94" << endl; //crashes here
for(;twentyThree<=100;)
{
if (twentyThree<=5 || twentyThree==10 || twentyThree==17 || twentyThree==21)
{
doneDone >> junkString;
}
if (twentyThree==6 || twentyThree==9 || twentyThree==13 || twentyThree==16 ||twentyThree == 20)
{
doneDone >> mabob[getname].name;
getname++;
}
if (twentyThree==7 || twentyThree==11 || twentyThree==14 || twentyThree==18 || twentyThree==22)
{
doneDone >> mabob[getprice].price;
getprice++;
}
if (twentyThree==8 || twentyThree==12 || twentyThree==15 || twentyThree==19 || twentyThree==23)
{
if (yaDoneDidIt == true && getquant==I)
{
mabob[getquant].quant--;
doneDone << mabob[getquant].quant;
doneDone.close();
return win;
}
doneDone >> mabob[getquant].quant;
if (getquant==I)
{
if (mabob[I].quant == 0)
{
doneDone.close();
return fail;
}
else
{
yaDoneDidIt = true;
}
}
getquant++;
}
twentyThree++;
}
}
}
Here's the contents of the text file:
Drink Name Cost Number in Machine
Cola $0.75 20
Root Beer $0.75 0
Lemon-Lime $0.75 20
Grape Soda $0.80 20
Cream Soda $0.80 20
this is somewhat confusing to read but it sure looks suspect to see:
for(;twentyThree<=100;)
{
...
getprice++;
mabob[getprice].price;
and similar logic.
I would almost bet money that you increment past 5 in there somewhere. If it is crashing, I would check that.
That aside, what exactly did not update as expected? What did you input? How far into the program did it get "correctly"? Youll want to put some debugging print statements in there, trace the logic, and see where it went wrong.