При вводе в консоль большого числа например 999999999999999, при конвертации во float программа принимает его без ошибок, при этом при выводе на консоль выводит -2147483648.
Try не помогает. Конвертировать пробовал и через потоки все одинаково.
<code>#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <regex>
using namespace std;
bool is_valid(string str) {
regex r ("^(-?|[0-9]?)([0-9]*)(,?)([0-9]*)");
return regex_match(str, r);
}
int main()
{
setlocale(LC_ALL, "Ru");
vector<int> prop;
for (int i = 1; i < 11; i++)
{
float flo = 0;
string str;
do
{
cout << "Введите признак № " << i << " --> ";
getline(cin, str);
} while (!is_valid(str));
try
{
flo = stof(str);
}
catch (invalid_argument& e)
{
cout << "ERROR Введено некорректное значение!" << endl;
i--;
continue;
}
catch (out_of_range& e)
{
cout << "ERROR Введено значение вне диапазона float!" << endl;
i--;
continue;
}
prop.push_back(flo);
}
for (int i = 0; i < prop.size(); i++)
{
cout << prop[i] << endl;
}
return EXIT_SUCCESS;
}
</code>
<code>#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <regex>
using namespace std;
bool is_valid(string str) {
regex r ("^(-?|[0-9]?)([0-9]*)(,?)([0-9]*)");
return regex_match(str, r);
}
int main()
{
setlocale(LC_ALL, "Ru");
vector<int> prop;
for (int i = 1; i < 11; i++)
{
float flo = 0;
string str;
do
{
cout << "Введите признак № " << i << " --> ";
getline(cin, str);
} while (!is_valid(str));
try
{
flo = stof(str);
}
catch (invalid_argument& e)
{
cout << "ERROR Введено некорректное значение!" << endl;
i--;
continue;
}
catch (out_of_range& e)
{
cout << "ERROR Введено значение вне диапазона float!" << endl;
i--;
continue;
}
prop.push_back(flo);
}
for (int i = 0; i < prop.size(); i++)
{
cout << prop[i] << endl;
}
return EXIT_SUCCESS;
}
</code>
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <regex>
using namespace std;
bool is_valid(string str) {
regex r ("^(-?|[0-9]?)([0-9]*)(,?)([0-9]*)");
return regex_match(str, r);
}
int main()
{
setlocale(LC_ALL, "Ru");
vector<int> prop;
for (int i = 1; i < 11; i++)
{
float flo = 0;
string str;
do
{
cout << "Введите признак № " << i << " --> ";
getline(cin, str);
} while (!is_valid(str));
try
{
flo = stof(str);
}
catch (invalid_argument& e)
{
cout << "ERROR Введено некорректное значение!" << endl;
i--;
continue;
}
catch (out_of_range& e)
{
cout << "ERROR Введено значение вне диапазона float!" << endl;
i--;
continue;
}
prop.push_back(flo);
}
for (int i = 0; i < prop.size(); i++)
{
cout << prop[i] << endl;
}
return EXIT_SUCCESS;
}
enter image description here
Видимо упущено что то важное!!!
New contributor
Ivan Golubev i is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.