Advertisement
Mike_be

Maybe working lib

Nov 8th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. string input = "";
  10.  
  11. int read_uint(const string start_mes) { //Checks variable for correct input (number)
  12.     const string string_err = "You wrote letters in number";
  13.     const string dot_err = "You wrote too many dots";
  14.     const string neg_err = "You number is negative";
  15.     const string repeat = ", type again: ";
  16.     cout << start_mes << ": ";
  17.     do { getline(cin, input); } while (input.length() == 0);
  18.     int dot_count = 0;
  19.     bool only_digits = true, negative = false;
  20.     string dummy;
  21.     while (!only_digits || dot_count == 0) {
  22.         for (int i = input.length() - 1; i >= 0; i--) {
  23.             if (!isdigit(input[i])) {
  24.                 if (input[i] == '.' || input[i] == ',') dot_count += 1;
  25.                 else if (input[i] == '-') negative = true;
  26.                 else only_digits = false;
  27.             }
  28.         }
  29.         if (only_digits && dot_count == 0 && !negative) {
  30.             return stoi(input.c_str(), NULL, 0);
  31.         }
  32.         else {
  33.             if (!only_digits) { cout << string_err << repeat; }
  34.             else if (dot_count != 0) { cout << dot_err << repeat; }
  35.             else if (negative) { cout << neg_err << repeat; }
  36.             getline(cin, input);
  37.             only_digits = true;
  38.             dot_count = 0;
  39.             negative = false;
  40.         }
  41.     }
  42.     return 0;
  43. }
  44.  
  45. long long int read_int(const string start_mes) { //Checks variable for correct input (number)
  46.     const string string_err = "You wrote letters in number";
  47.     const string dot_err = "You wrote too many dots";
  48.     const string neg_err = "You number is negative";
  49.     const string repeat = ", type again: ";
  50.     cout << start_mes << ": ";
  51.     getline(cin, input);
  52.     int dot_count = 0;
  53.     bool only_digits = true, negative = false;
  54.     string dummy;
  55.     while (!only_digits || dot_count == 0) {
  56.         for (int i = input.length() - 1; i >= 0; i--) {
  57.             if (!isdigit(input[i])) {
  58.                 if (input[i] == '.' || input[i] == ',') dot_count += 1;
  59.                 else if (input[0] == '-') negative = true;
  60.                 else only_digits = false;
  61.             }
  62.         }
  63.         if (only_digits && dot_count == 0) {
  64.             return strtoll(input.c_str(), NULL, 0);
  65.         }
  66.         else {
  67.             if (!only_digits) { cout << string_err << repeat; }
  68.             else if (dot_count != 0) { cout << dot_err << repeat; }
  69.             getline(cin, input);
  70.             only_digits = true;
  71.             dot_count = 0;
  72.             negative = false;
  73.         }
  74.     }
  75.     return 0;
  76. }
  77.  
  78. char readChar(string request) {
  79.     do {
  80.         string input = "";
  81.         cout << request << ": ";
  82.         getline(cin, input);
  83.         if (input.length() == 1) return input[0];
  84.     } while (input.length() != 1);
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement