0

I am trying to make an employee discount program that reads employee number and discount from an input file. I need it to display an error message when a character is put into the variable. I can't seem to figure this out. I'm a newbie so I don't know that many identifiers yet. This compiles with an unexpected output. I'm not sure how to get the variables for all three employees as well. Any help is greatly appreciated.

/* Program name: main.cpp
*  Author: Carl Snyder
*  Date last updated: 09/11/2022
* Purpose: Top employee discount calculator
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main() 
{
ifstream inFile;
int winning_Num;
int employee_Num;
double total_Bill;
double discount_Percent;
double discount_Amount;
double bill_After_Discount;
inFile.open("EOM.txt");

inFile >> winning_Num >> discount_Percent;

cout << "Enter your employee number to see if you get a discount: ";
cin >> employee_Num;
cout << fixed << showpoint << setprecision(2);
if (employee_Num >= 10000 && employee_Num <= 99999)
    {
        if (employee_Num == winning_Num)
        {
            cout << "You have won a discount of " << discount_Percent << "%." << endl;
            cout << "Enter the total bill: " << endl;
            cin >> total_Bill;
            if (total_Bill > 0)
            {
            discount_Amount = total_Bill * discount_Percent;
            bill_After_Discount = total_Bill - discount_Amount;
            cout << "Your discount will take $" << discount_Amount << " off your bill. " 
<< endl;
            cout << "Your new total is $" << bill_After_Discount << "." << endl;
            return 0;
            }
            if (total_Bill <= 0)
            {
                cout << "The total should be greater than 0. The program will now 
exit.";
            }
            else
                cout << "You entered something that is not a number! The program will 
now exit.";
            
        }
        else
            cout << "Sorry you did not win a discount this month. Try again next 
month.";
    }
if (employee_Num <=9999 || employee_Num >=100000)
    {
        cout << "You entered a number that is out of range! Employee numbers are 5 
digits (between 10000 and 99999). Program will now exit.";
    }
else
        cout << "You did not enter a number! The program will now exit.";
return 0;
}
  • What error do you get? Post it in your question. Also see [How Can I avoid char input for an int variable?](https://stackoverflow.com/questions/11523569/how-can-i-avoid-char-input-for-an-int-variable) – Jason Sep 12 '22 at 14:36
  • `cin` and `!cin` (or `cin.fail()` and `!cin.fail()`) are usually used in conditions. – 273K Sep 12 '22 at 14:38
  • A way to verify that input worked is `if (cin >> employee_Num) handle_value() else error();` – BoP Sep 12 '22 at 14:39
  • I've already tried cin.fail() in the expression and then clearing and ignoring until it reaches a newline character. I'm trying to get it to read three employee numbers with 3 different discounts, if that makes any sense. I can't have the bill be "0". The employee number can only be five numbers as well. It compiles but it doesn't output what I want. – Carl Snyder Sep 12 '22 at 15:04

0 Answers0