while (simulation = true)
{
cout<<"Input the name of the reservoir : ";
string reservoir;
cin>>reservoir;
cout<<"Input the capacity of the reservoir in MAF (Millions of Acre Feet) : ";
double capacity;
cin>>capacity;
cout<<"Input the maximum inflow in MAF : ";
int max_inflow;
cin>>max_inflow;
cout<<"Input the minimum inflow in MAF : ";
int min_inflow;
cin>>min_inflow;
cout<<"Input the required outflow : ";
double req_ouflow;
cin>>req_ouflow;
if (req_ouflow > batas)
{ cout<<"The required out flow can not be greater than 90% of the average inflow "<<endl;
cout<<"Would you like to try a different lake ? "<<endl;
cout<<"1. Yes "<<endl;
cout<<"2. No "<<endl;
cout<<"Your selection : ";
cin>>choice;
if (choice == 1)
{
system ("CLS");
simulation = true;
}
else
{
cout<<"You have chosen to quit the program"<<endl;
return 0;
}
}
for (int i=1; i<11; i++)
{
double current = (rand()%19) + 4;
current = current - 7.5;
if (current < 0)
{
current == 0;
}
int inflow = rand() % (max_inflow - min_inflow + 1) + min_inflow;
int year = 0;
while (current < capacity)
{
year++;
if ( current + inflow < req_ouflow)
{
req_ouflow = current + inflow;
}
int now = inflow + current - req_ouflow;
current += now;
}
cout<<"Simulation "<<i<<" took "<< year<<" years"<<endl;
}
cout<<"In 10 simulations of filling "<<reservoir<<", it took : "<<endl;
cout<<"Minimum number of years : "<<endl;
cout<<"Maximum number of years : "<<endl;
cout<<"Average number of years : "<<endl;
cout<<endl;
cout<<"Would you like to try a different lake? (yes/no)"<<endl;
cout<<"1. Yes "<<endl;
cout<<"2. No "<<endl;
cout<<"Your selection : ";
cin>>choice;
if (choice == 1)
{
system ("CLS");
simulation = true;
}
else
{
cout<<"You have chosen to quit the program"<<endl;
return 0;
}
}
return 0;
}
but that bold part doesnt seem to work. can anyone help me please? thanks
I followed your suggestion but it still doesn't work. the only operation that works is the system ("CLS");
but the last part of the program works, it goes back to the menu. I copied and pasted the same thing but I still don't get what the problem is.
It has something to do with your input. I entered random numbers as input, got that message, and then typed '1' and it went back to the menu. Then I tried it again with different (presumably) numbers, and it didn't work.
yes, I set the program to goback to the menu only if the user inputs "1", if the user inputs a different number, the program will quit. but why won't it go back to the menu on the bold part if I input "1"?
if (choice == 1)
{
system ("CLS");
simulation = true;
continue;
}
double current = (rand()%19) + 4;
The random generator will always yield the exact same results unless given some sort of seed to adjust the number. Google 'srand c++' for details!
1 2 3 4
if ( current + inflow < req_ouflow)
{
req_ouflow = current + inflow;
}
for clarity...req_ouflow = min(req_ouflow, current + inflow);
1 2 3 4 5 6 7 8 9 10 11
while (current < capacity)
{
year++;
if ( current + inflow < req_ouflow)
{
req_ouflow = current + inflow;
}
int now = inflow + current - req_ouflow;
current += now;
}
Without running the code, this block looks bad. Say current + inflow < req_outflow...for example, "5+15 < 22"...so now req_outflow is set to "5+15". Then 'now' is set to 5+15 - 20...which will always be 0. This value is added to current, so "current + 0", and you wind up in an infinite loop! That seems unfortunate. Infinite loops can take a while to complete.