Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string input = "";
- long double read(string startMes, string stringError, string dotError) { //Checks variable for correct input (number)
- cout << startMes << ": ";
- getline(cin, input);
- dotCount = 0;
- bool onlyDigits = true;
- string dummy;
- while (!onlyDigits || (dotCount >= 0 && dotCount < 2)) {
- for (int i = input.length() - 1; i >= 0; i--) {
- if (!isdigit(input[i])) {
- if (input[i] == '.') dotCount += 1;
- else onlyDigits = false;
- }
- }
- if (onlyDigits && dotCount < 2) {
- return stod(input.c_str());
- }
- else {
- if (!onlyDigits) { cout << stringError << ", type again: "; }
- else if (dotCount > 1) { cout << dotError << ", type again: "; }
- getline(cin, input);
- onlyDigits = true;
- dotCount = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement