stuck on with input validation in c++ either string or integer -


i trying practice input validation in c++. how can let program validate user input when user asked enter number or string? here sample of code.

public: void createproduct() { inputproduct:     system("cls");     cout << "\n\n\n\n\n\n\n\t\t\t\tplease provide accurate information";     cout << "\n\n\t\tproduct number: ";     cin >> productnumber;     if (!cin) {         cout << "\nplease provide integer";         cin.clear();         cin.end;         goto inputproduct;        //when enter string should enter if statement , exit        // asked entry getting stuck in loop.     }     system("cls");     cout << "\n\n\n\n\n\n\n\t\t\t\tproduct name: ";     cin >> productname;     system("cls");     cout << "\n\n\n\n\n\n\n\t\t\t\tprice: ";     cin >> price;     system("cls");  } 

please me understand input validation.

you can check whether input number or not checking ascii value of each character in input .

 #include <iostream> #include <cstring> #include <string>  int main(void) {     std::string str;      std::cin>>str;      bool isnumeric = true;     for(size_t i=0;i<str.length();++i)     {         if(str[i]< '0' || str[i] > '9')         {            isnumeric = false;           break;         }     }      if(!isnumeric)     {         std::cout<<"input not integer";         exit(1);     }     return 0; } 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -