Problem with if-else statement. MY PROGRAM IS DUE AT 11:59pm CST!!

I am having trouble with one requirement for my program. Here it is:

"Your program will continue asking for the sales until the user types a negative number for sales, then the program should ask the user, if he/she wants to terminate the program. The program terminates if the user answers yes or y."

My program prompts the user to input 'y' or 'n' but it does not recognize y or n when it is input. It just jumps to the "Invalid entry" statement and loops back to the beginning.

Here is my program:

#include "StdAfx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

double getsales();
double calcsales(double);
char ans, y, n;
double sales, comm;

int main(double sales, double comm)
{
do
{
sales = getsales();
comm = calcsales(sales);
if(sales >= 0)
{
cout << "\nThe sale amount entered: $" << fixed << setprecision(2) << sales
<< "\nThe commission earned: $" << fixed << setprecision(2) << comm;
continue;
}
else
cout << "\n Would you like to terminate the program?"
<< "\n Enter 'y' or 'n': ";
cin >> ans;
if(ans == y)
break;
else if(ans == n)
continue;
else
cout << "\nInvalid Entry. Please try again.";
continue;
}while(sales = 1, sales >= 0, sales ++);

return 0;

}

double getsales()
{
cout << "\n\nPlease enter the sale amount: $";
cin >> sales;

return sales;
}


double calcsales(double)
{
if(sales >= 0 && sales <= 999)
comm = sales * 0.035;

else if(sales > 999 && sales <= 1999)
comm = sales * 0.04;

else if(sales > 1999 && sales <= 2999)
comm = sales * 0.045;

else if(sales > 2999)
comm = sales * 0.05;
else
comm = 0;

return comm;
}
Last edited on
Your mistake was to compare ans with uninitialised variables y and n... try this one below by just putting 'y' and 'n' instead of y or n. or first initialise your variables y and n then compare them with ans. hope it works!!!

1
2
3
4
5
6
7
8
9
10
if(ans == 'y')
break;
else if(ans == 'n')
continue;
else
cout << "\nInvalid Entry. Please try again.";
continue;
}while(sales = 1, sales >= 0, sales ++);

return 0;
It worked great AND the deadline was extended! Thank you very very much! This community is awesome!!!
Topic archived. No new replies allowed.