Hi,
I'm having some trouble with code, trying to make my function to be able to accept decimal numbers.
But it somehow cannot accept the decimal numbers. It has to be able to accept the decimal number.
Here's a sample input to test my code:
Simpson,Homer,6,2,1,7,5.3
When the decimal number isn't accepted, it prints out "bad"
Here's my code:
Code:#include <stdio.h> #include<iostream> #include<iomanip> #include<sstream> #include<fstream> #include<string> #include<cctype> using std:: cout; using std:: cin; using std:: endl; using namespace std; bool validation(string& s); bool input(string& s); int main() { string text; input(text); return 0; } bool validation(string& s) { int flag = 0; for(string:: size_type i=0; i<s.length(); i++) { if(!isalpha(s[i]) && s[i] != ',' && !isdigit(s[i])) { cout << "bad" << endl; return false; } if(isalpha(s[i]) && s[i] == ',') { if(isdigit(s[i])) flag = 1; else flag = 1; } else flag = 0; } if(flag == true) { cout << "ok" << endl; return true; } return 0; } bool input(string& s) { string line; string inputText; istringstream iss; cout << "Please enter text\n" << endl; while(getline(cin, line)) { iss.clear(); iss.str(line); if(iss >> inputText) { validation(inputText); cout << "\n\n" << "text" << endl; } } return true; }