If, Else infinite loop help, C++

I am trying to make a C++ sales calculator and for my if, else statement I have my "Purchase" as == to false at moment, i am a bit confused on how to get it so that when the user enters a non number (so like letters and such) then the loop will just reset and ask the user again to add a valid number answer and must plug in a number answer, and to repeat it again and again if the user doesn't put in a valid number answer.


#include "pch.h"
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <math.h>
using namespace std;



float purchase; // float for user entering their purchase amount
float state_list, tax_rate, sales_taxe, total_price;
float maryland;

int main() {

cout << "+----------------------+" << endl; // sales tax calculator header
cout << "| SALES TAX CALCULATOR |" << endl; // sales tax calculator header
cout << "+----------------------+" << endl; // sales tax calculator header
cout << "Instructions: Please enter an item cost" << endl; // sales tax calculator header
cout << "and select a state from the list." << endl; // sales tax calculator header

std::cout << endl << "How much is the item you would like to purchase: $"; //purchase enter text
std::cin >> purchase; // we assume that the user enters a valid score

if (purchase == false) { // figure out the purchase == no number value so loop works correctly

cout << "Please select a state from the following list:";
}

else {
cout << "The entered cost is invalid..." << endl;
cout << endl << "How much is the item you would like to purchase: $";
cin >> purchase;
cout << "Please select a state from the following list:" << endl;
}

cout << "1.) Maryland" << endl;
cout << "2.) Virginia" << endl;
cout << "3.) North Carolina" << endl;
cout << "4.) South Carolina" << endl;
cout << "5.) Deleware" << endl;
cout << "6.) District of Columbia" << endl;
cout << "7.) Pennsylvania" << endl;
cout << "In which state is the purchase being made : ";
cin >> state_list;

// add state_list loop here with each state numbered

cout << endl;
std::cout << std::setfill('-') << std::setw(46) << "-" ; // line header

cout << endl << "Original Purchase Price : " << "$" << purchase;
cout << endl << "Tax Rate";
std::cout << std::setfill(' ') << std::setw(18) << ": ";
cout << endl << "Sales Tax";
std::cout << std::setfill(' ') << std::setw(17) << ": ";
cout << endl << "Total Price";
std::cout << std::setfill(' ') << std::setw(15) << ": ";

cout << endl;

}
Topic archived. No new replies allowed.