Who knows C++?

Avatar image for subjugation
Subjugation

4993

Forum Posts

963

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

#1  Edited By Subjugation

So either I'm retarded or this do-while loop should terminate, but it loops when the condition is true and false. Bolded text is the while loop condition check.

Check it:

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <ctime>
using namespace std;
void simulation ()
{
// for loop with number of iterations = 10
}
int main ()
{
srand (time(0));
string operation;
cout << "Using the letters 'o', or 'q', please indicate if you would like to simulate once, or quit the program: " << endl;
cin >> operation;
string reservoir_name; // Creating variables for reservoir
double reservoir_capacity;
double outflow;
double inflow_min;
double inflow_max;
if (operation == "q")
{
cout << "Exiting program." << endl;
system ("pause");
return 0;
}
while (operation == "o") // Choose one or multiple simulations.
{
if (operation == "o") // For one simulation
{
string reservoir_name; // Creating variables for reservoir function
double reservoir_capacity;
double outflow = 0;
double inflow_min = 0;
double inflow_max = 0;
double inflow_range = inflow_min + inflow_max;
double inflow_threshold = .9 * inflow_range/2; // Math for acceptable flow threshold.
do
{
cout << "What is the name of the reservoir?" << endl;
cin.ignore ();
getline (cin,reservoir_name); // Grab whole string for reservoir name.
cout << "What is the capacity of the reservoir in MAF (Millions of Acre Feet)?" << endl;
cin >> reservoir_capacity;
cout << "What is the minimum inflow?" << endl;
cin >> inflow_min;
cout << "What is the maximum inflow?" << endl;
cin >> inflow_max;
cout << "What is the required outflow?" << endl;
cin >> outflow;
inflow_range = inflow_min + inflow_max;
inflow_threshold = .9 * inflow_range/2;
cin.ignore ();
if (outflow > inflow_threshold) // Check for unacceptable outflow levels.
{
cout << "Warning! The outflow is over 90% of the average inflow. Simulation aborted. Please enter a value that is less than 90% of average inflow." << endl;
}
}while (outflow > inflow_threshold );
}
}
simulation ();
system ("pause");
return 0;
}
Avatar image for tycobb
TyCobb

2036

Forum Posts

90

Wiki Points

0

Followers

Reviews: 1

User Lists: 0

#2  Edited By TyCobb

stackoverflow.com is your friend.

It's also a do...while loop. it will always run the first time and then evaluate if it needs to run again. If you think it is really false, then print out the values. It could be anything, but the loop structure itself looks fine. Learning to debug is the best thing you can do =P

Avatar image for positrark
positrark

327

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#3  Edited By positrark

Isn't the problem that the "operation" variable isn't changed from "o", so that even though you exit the do-while loop you're still stuck in the outer while loop which just sends you back into the do-while loop.

Avatar image for countmacula
CountMacula

262

Forum Posts

6849

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#4  Edited By CountMacula

It's been a long day and I haven't coded in quite some time, but you're redeclaring variables inside the if(operation="o") section, that can cause some interesting scope errors iirc. Also have you tried printing out the individual values of the outflow, inflow_max,min and threshold? Are the values as you would expect?

Avatar image for countmacula
CountMacula

262

Forum Posts

6849

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#5  Edited By CountMacula

@Positrark said:

Isn't the problem that the "operation" variable isn't changed from "o", so that even though you exit the do-while loop you're still stuck in the outer while loop which just sends you back into the do-while loop.

This. That first while loop is redundant anyway. Looks like a copy/paste error

Avatar image for subjugation
Subjugation

4993

Forum Posts

963

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

#6  Edited By Subjugation

@CountMacula: Using breakpoints along the way, the values are reporting as they should so the condition to break the loop should be satisfied. I think @Positrark: may be on to something though, so I'll check that out. Thanks for the input duders.@TyCobb: I'm changing it from a do-while, removing the "do" and just using a while loop at the bolded location that will check to see if if the condition is true since those variables will be assigned values by that point and then placing the code that takes inputs to assign values to the variables to kind of emulate a menu loop to re-prompt.

Avatar image for positrark
positrark

327

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#7  Edited By positrark

@Subjugation: I'm almost certain I'm right. What you're reporting makes perfect sense when you examine how the program is currently written.

Avatar image for chaser324
chaser324

9415

Forum Posts

14945

Wiki Points

0

Followers

Reviews: 1

User Lists: 15

#8  Edited By chaser324  Moderator

@TyCobb said:

stackoverflow.com is your friend.

This...people over there probably aren't going to be thrilled about helping you with your homework, but it's a better suited place for those questions than GiantBomb.