vendredi 1 janvier 2021

How to keep on looping until i get desired data type

How can I keep on looping until I get the desired data type in cin for ex I want to keep on looping if the user enter non - integer digit for ex in this code.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
template <class T>
class Division
{
    public:
        T term_1;
        T term_2;
        Division(T term1_P,T term2_P):term_1(term1_P),term_2(term2_P){}
        void DivisionRes()
        {
            T term_3=term_1/term_2;
            cout<<"Result = "<<term_3;
        }
};
int main()
{
    float* Term1=new float;
    float Term2;
    cout<<"Enter A Number You Want To Divide"<<endl<<">>";
    do
    {
        cin>>(*Term1);
        if(cin.fail())
        {
            cout<<endl<<"Invalid Input!\n>>";
        }
    }while((*Term1)==0);
}


But there's something weird I noticed when I enter a string it keeps on looping without allowing me to give a Re-input.

Problem

So How can I fix it? Thanks for reading.

PS:

Sorry for bad English

Code isn't complete yet

Aucun commentaire:

Enregistrer un commentaire