我已经cin通过 using验证了输入std::numeric_limits,因此我需要包含<limits>.
这会影响我的程序的功能吗?
我在不同的论坛上阅读了很多评论,认为使用<limits>不是一个好习惯,但没有人明确说明为什么这不是一个好习惯。如果这确实是一种糟糕的做法,那么验证cin已知错误(例如无效输入和长输入)的输入的好做法是什么?
#include <iostream>
#include <limits>
using namespace std;
void initMenu ();
void initDecision(int);
double areaCircle (double);
double areaRectangle (double, double);
const double PI = 4.14;
bool isValid (string);
int main()
{
int choice;
char cont;
do
{
system ("cls");
initMenu ();
while (!(cin >> choice))
{
cin.clear();
system ("cls");
initMenu();
cout << "---------------------------------------------------------------------------" << '\n';
cout << "Invalid Input cleared and turns to …Run Code Online (Sandbox Code Playgroud) 问题是当我第一次运行我的程序并输入 5 时,程序将返回星期五,然后按y重新启动操作而不关闭控制台并输入9程序将返回默认错误消息,并且还将返回星期五,这是一个我的代码中存在严重错误,我无法确定是什么导致它出错。编码:
#include <iostream>
#include <limits>
using namespace std;
enum Dayofweek {monday = 1 , tuesday = 2, wednesday = 3, thursday = 4, friday = 5, saturday = 6, sunday = 7};
string Day (Dayofweek);
int main()
{
int i;
char resTart;
Dayofweek d = monday;
do
{
cin.clear();
system ("cls");
cout << "Enter the day of a week:[1] [2] [3] [4] [5] [6] [7]: ";
while (!(cin >> i)) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用max函数返回最大值,但它不适用于 3 个值。
代码块错误:
错误:“__comp”不能用作函数
编码:
#include <iostream>
using namespace std;
int main()
{
cout << max(5, 10, 20);
}
Run Code Online (Sandbox Code Playgroud) 我有这个代码来接受用户输入并验证它以增加用户钱包中的用户 BTC。我需要对这个 CODE 的工作原理进行一些解释。编码:
#include <iostream>
using namespace std;
int main()
{
int btc_input, result_btc, btc_validation, btc_validation_1_input, btc_validation_2_input;
result_btc = 0;
btc_validation = 0;
btc_validation_1_input = 0;
btc_validation_2_input = 0;
for (; btc_validation < 3; btc_validation++)
{
cout << "Enter the Value " << (btc_validation + 1) << " + " ;
cin >> btc_input;
cout << "Enter the Value " << (btc_validation + 1) << " + " ;
cin >> btc_input;
btc_validation_1_input == btc_validation + btc_input;
cout << …Run Code Online (Sandbox Code Playgroud) 预期输出是:
值123456789包含9位数字。
但给定的输出是:
值1.23457e+08包含332位数字
来源:
#include <iostream>
using namespace std;
int main ()
{
double number_1 = 123456789;
double numberOfdigits = 1;
double tmp = number_1;
while (tmp /= 10)
numberOfdigits ++;
cout << "The Value " << number_1 << " contains " << numberOfdigits << " digits " << '\n';
}
Run Code Online (Sandbox Code Playgroud)