Im making a tip calculator and want to generate an error if the user enters a negative number of a character. Although the program is working, my question is that why is generating an error for character even when I haven't put any such condition yet?
//Tip Calculator
#include<iostream>
using namespace std;
int main()
{
//Declaring Variables
float BillAmount, TipRate, Tip, TotalBill;
//Taking input for bill amount and generating an error if its negative/charcter
cout<<"Enter the Bill Amount: "; cin>>BillAmount;
if (BillAmount<=0)
{
cout<<"Enter a Valid Amount"<<endl;
exit(0);
}
//Taking input for tip rate and generating an error if its negative/character
cout<<"Enter the Tip Rate: "; cin>>TipRate;
if (TipRate<=0)
{
cout<<"Enter a Valid Amount"<<endl;
exit(0);
}
//Calculating and printing the tip to 2dp
Tip = (TipRate/100)*BillAmount;
cout<<"Tip: $";printf("%.2f",Tip);cout<<endl;
//Calculating the total amount and printing it to 2dp
TotalBill = BillAmount + Tip;
cout<<"Total Bill: $";printf("%.2f",TotalBill);cout<<endl;
}
Also, is there a way to restart the whole thing after the error?
As of now, I only wanted to generate an error over a negative value, however it is now giving the error "Enter a Valid Amount" on both negative values and upper/lowercase letters. Why is that?