Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <vector>
- #include <string>
- #include <iomanip>
- using namespace std;
- string input = "";
- int read_uint(const string start_mes) { //Checks variable for correct input (number)
- const string string_err = "You wrote letters in number";
- const string dot_err = "You wrote too many dots";
- const string neg_err = "You number is negative";
- const string repeat = ", type again: ";
- cout << start_mes << ": ";
- do { getline(cin, input); } while (input.length() == 0);
- int dot_count = 0;
- bool only_digits = true, negative = false;
- string dummy;
- while (!only_digits || dot_count == 0) {
- for (int i = input.length() - 1; i >= 0; i--) {
- if (!isdigit(input[i])) {
- if (input[i] == '.' || input[i] == ',') dot_count += 1;
- else if (input[i] == '-') negative = true;
- else only_digits = false;
- }
- }
- if (only_digits && dot_count == 0 && !negative) {
- return stoi(input.c_str(), NULL, 0);
- }
- else {
- if (!only_digits) { cout << string_err << repeat; }
- else if (dot_count != 0) { cout << dot_err << repeat; }
- else if (negative) { cout << neg_err << repeat; }
- getline(cin, input);
- only_digits = true;
- dot_count = 0;
- negative = false;
- }
- }
- return 0;
- }
- long long int read_int(const string start_mes) { //Checks variable for correct input (number)
- const string string_err = "You wrote letters in number";
- const string dot_err = "You wrote too many dots";
- const string neg_err = "You number is negative";
- const string repeat = ", type again: ";
- cout << start_mes << ": ";
- getline(cin, input);
- int dot_count = 0;
- bool only_digits = true, negative = false;
- string dummy;
- while (!only_digits || dot_count == 0) {
- for (int i = input.length() - 1; i >= 0; i--) {
- if (!isdigit(input[i])) {
- if (input[i] == '.' || input[i] == ',') dot_count += 1;
- else if (input[0] == '-') negative = true;
- else only_digits = false;
- }
- }
- if (only_digits && dot_count == 0) {
- return strtoll(input.c_str(), NULL, 0);
- }
- else {
- if (!only_digits) { cout << string_err << repeat; }
- else if (dot_count != 0) { cout << dot_err << repeat; }
- getline(cin, input);
- only_digits = true;
- dot_count = 0;
- negative = false;
- }
- }
- return 0;
- }
- char readChar(string request) {
- do {
- string input = "";
- cout << request << ": ";
- getline(cin, input);
- if (input.length() == 1) return input[0];
- } while (input.length() != 1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement