Problem with evaluating a conditional statement (C++)
2 replies, posted
Hi everyone
Haven't posted here in many years, but i've encountered a problem with my C++ Program.
I've written a program that performs some simple calculations based off of user input values and prints formatted results.
The program runs fine, and does what I want it to do (for the most part), but I have encountered a roadblock when it comes
to evaluating this conditional statement.
First, the user is asked to input two values, The initial meter reading (InMeter) and the final meter reading (Finmeter).
These values are evaluated using a do-while loop to ensure they are on the range of 0 > X < 99,9999.
This is done with the following code: (Formatting on FP is kind of weird, it does not copy here like it appers in my code)
do
{
cout << "What was the initial meter reading?: " << endl; //Collects initial meter reading
cin >> InMeter;
}
while((InMeter<0)||(InMeter>99999));
do
{
cout << "What was the final meter reading?: " << endl;; //Collects final meter reading
cin >> FinMeter;
}
while((FinMeter<0)||(FinMeter>99999));
The conditional statement to evaluate the two values in question is:
if(FinMeter > InMeter)
{
EnergyUsed = (FinMeter-InMeter);
}
else if (FinMeter < InMeter)
{
EnergyUsed = (100,000 - InMeter) + (FinMeter);
}
My problem occurs when the program evaluates the condition of which input value is greater. The program seems to default to the first equation
( EnergyUsed = (FinMeter-InMeter); ) and run that regardless of whether the conditional is true or false, producing a negative value.
For example; the program would run fine given:
FinMeter = 99882
InMeter = 98830
BUT
If given:
FinMeter = 00631 OR 631
InMeter = 99856
The program will run the first equation and the returned value will be negative, resulting in incorrect values being displayed.
Here is a link to my full code: https://repl.it/repls/NuttyEasyCoin
PLEASE HELP!!! Thank you in advance.
That explains why the linter at work complains about comma expressions.
In C++14 you can use ' as a digit separator: integer literal
Optional single quotes(') may be inserted between the digits as a separator. They are ignored by the compiler.
Sorry, you need to Log In to post a reply to this thread.