0

Major C++ noob here, as well as programming in general.

I am making a very simple tax calculator and I am wondering if it is possible to prevent a user from inputting values that are not numeric. I don't want to validate the input to see if it is numeric or not. I am strictly trying to prevent the invalid input from showing up in the first place. I'm not sure if I wasn't thorough enough when searching but I couldn't find anything about this anywhere else which makes me think it is possible.

Ex. I am the user, I hold down the 'a' key, nothing shows up

I am the user, I hold down the '}' key, nothing shows up

I press '1', '2', and '3', 123 shows up in program.

Thank you in advance for help.

#include <iostream>

using namespace std;

int main()
{
    double mealCost, taxAmount;
    double tipAmount, totalAmount;

    const double TAX_RATE = .0675;
    const double TIP_RATE = .20;

    cout << "\nRestaurant Bill Calculation\n" << endl;

    // Input
    cout << "\nEnter the cost of your meal" << endl;
    cin >> mealCost;

    // Processing
    taxAmount = TAX_RATE * mealCost;

    tipAmount = (taxAmount + mealCost) * TIP_RATE;

    totalAmount = mealCost + taxAmount + tipAmount;

    // Output

    cout << "Meal cost " << mealCost << endl;
    cout << "Tax Amount " << taxAmount << endl;
    cout << "Tip Amount " << tipAmount << endl;
    cout << "Total Amount " << totalAmount << endl;

    return 0;


}
Glace
  • 1
  • The joke answer is: the only way to prevent user input other than numeric values is putting a gun t ouser's head. Failing that, your best bet is to teach your code to deal with it gracefully. – JB. Sep 29 '15 at 20:18
  • You cannot do this though iostreams. Need the involvement of the terminal. – Ed Heal Sep 29 '15 at 20:18
  • @Jb. Remove the other keys from the keyboard would be less harsh. – Ed Heal Sep 29 '15 at 20:19
  • Duplicate of http://stackoverflow.com/questions/14901678/reading-in-a-specific-format-with-cin http://stackoverflow.com/questions/14330637/what-is-the-cin-analougus-of-scanf-formatted-input – Adi Sep 29 '15 at 20:19
  • This shouldn't have been closed as duplicate because the OP never said he had to use iostream functions, but did say that the input number shouldn't even show up on the console screen. @EdHeal was right about needing a lower level terminal input function like [getch()](https://msdn.microsoft.com/en-us/library/078sfkak.aspx) for Windows. As an abbreviated answer, you would read one character at a time from the keyboard, check to see if it is a valid digit, and if it is, print it to the screen and record the digit in your program. Of course, backspace gets hard to handle when doing this... – JPhi1618 Sep 29 '15 at 21:18
  • @EdHeal now you've done it! You've reminded me of [the dedicated keyboard](http://cdn.overclock.net/0/04/04ff18c3_Microsoft_Keyboard_by_JonEastwood.jpeg)! – JB. Sep 29 '15 at 21:52
  • The command doesn't matter, I want to know of anything that will actually do what I need. – Glace Sep 30 '15 at 02:31

0 Answers0