-2

I am new to C++, I wrote this code for practice and can't seem to understand why my code still loops after I input the sentinel value.

Sample output: Please enter your sales amount or -1 to exit: 25100 Your salary for the month is:$31110.00 Please enter your sales amount or -1 to exit: -1 Your salary for the month is:$3499.00 –

Here is my code:

#include <stdio.h>
#include <math.h>
#define MIN 25000
#define COMP .10
#define BASPAY 3500
//Function Prototypes
double MonthSales(void);
double CalcPay(double sales);

//Begin Main
int main(void)
{
//declare variables
    double sales = 0;
// Begin Program with sentinel value for exit
    while (sales != -1) 
    {
        sales = MonthSales();
        printf("Your salary for the month is:$%.2lf\n", CalcPay(sales));
//The line above still runs after I input -1 :S.
    }
}
//Function for sales
double MonthSales(void) 
{
    double sales;
    printf("Please enter your sales amount or -1 to exit:\n");
    scanf("%lf", &sales);
    return sales;
}

//Function for total Pay
double CalcPay(double sales) 
{
    double pay;
    double commission;
    if (sales > MIN) 
    {
        commission = sales * COMP;
        pay = commission + sales + BASPAY;
    } 
    else 
    {
        pay = sales + BASPAY;
    }
    return pay;
}
  • Because you forgot to compile with all warnings and debug info (`gcc -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/)...) then **[use the `gdb` debugger](https://sourceware.org/gdb/current/onlinedocs/gdb/)** to understand the behavior of your program. With floating points you have to read http://floating-point-gui.de/ – Basile Starynkevitch Apr 04 '18 at 05:52
  • 1
    Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Yunnosch Apr 04 '18 at 05:57

1 Answers1

0

In this loop:

while (sales != -1) 
    {
        sales = MonthSales();
        printf("Your salary for the month is:$%.2lf\n", CalcPay(sales));
    }

the control expression sales != -1 is evaluated before each iteration of the loop. It is not evaluated continuously or at points inside the iterations. The loop does not automatically stop the moment sales != -1 becomes false.

Instead, you must test the value of sales at the point where you want the control flow to change. For example, you can do this:

while (1)
{
    sales = MonthSales();
    if (sales == -1)
        break;
    printf("Your salary for the month is:$%.2lf\n", CalcPay(sales));
}
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Did you mean to add {do} after the while statement, because I couldn't get my IDE to compile. However, I took the do off and it worked like a charm. I will have to read your explanation and make sense of the order in which evaluation takes place and also how to fix the infinite loop if another character is entered. Thank you! – John Applessed Apr 04 '18 at 06:35
  • It was late last night, so I didn't realize this but does that if statement still count as a sentinel loop? – John Applessed Apr 04 '18 at 14:43
  • @JohnApplessed: “Sentinel loop” is not a formally defined term. If your course assignment says to use a sentinel, then this code satisfies that instruction. A sentinel is merely a chosen value that marks the end of data or some other significant thing (such as a change from one set of data to another). – Eric Postpischil Apr 04 '18 at 15:11
  • Oh I see, I also realized that, although a bit crude I could also add my function before the while loop and inside the while loop so that the variable was initialized and checked at the end of while again. – John Applessed Apr 04 '18 at 19:03