C++ 如果输入数字为负数,如何循环
输入验证: 如果数字为负数,程序应输出一个错误,并允许用户重试(如果有)。只需要一次迭代(即,您的程序不需要提示输入另外三个数字,它可以在打印结果后退出) 所以我已经完成了作业,我唯一缺少的就是上面提到的输入验证。换句话说(根据我的理解),我应该在我的代码中实现一个循环,在这个循环中,如果用户以前输入了一个负数,那么他们必须输入另外3组数字C++ 如果输入数字为负数,如何循环,c++,function,median,C++,Function,Median,输入验证: 如果数字为负数,程序应输出一个错误,并允许用户重试(如果有)。只需要一次迭代(即,您的程序不需要提示输入另外三个数字,它可以在打印结果后退出) 所以我已经完成了作业,我唯一缺少的就是上面提到的输入验证。换句话说(根据我的理解),我应该在我的代码中实现一个循环,在这个循环中,如果用户以前输入了一个负数,那么他们必须输入另外3组数字 #include <iostream> #include <iomanip> using namespace std; doubl
#include <iostream>
#include <iomanip>
using namespace std;
double getAverage (double num1, double num2, double num3){
double averageAmount;
averageAmount = (num1 + num2 + num3)/ 3;
return averageAmount;
}
double getMedian (int num1, int num2, int num3){
int x = num1-num2;
int y = num2-num3;
int z = num1-num3;
if(x*y > 0) return num2;
if(x*z > 0) return num3;
return num1;
}
double countDigits( int num1){
int digits = 0;
while (num1){
num1 = num1/10;
digits++;
}
return (digits);
}
int main() {
double num1 = 0;
double num2 = 0;
double num3 = 0;
cout << "Enter 3 integers" << endl;
cin >> num1;
cin >> num2;
cin >> num3;
cout << "The average is " << fixed << setprecision(2) << getAverage(num1, num2, num3) << endl;
cout << "The median is " << fixed << setprecision(2) << getMedian(num1, num2, num3) << endl;
cout << "There are " << countDigits(num1) << " digits in the number " << num1 << endl;
return 0;
}
#包括
#包括
使用名称空间std;
双倍平均值(双倍num1,双倍num2,双倍num3){
双倍平均金额;
平均金额=(num1+num2+num3)/3;
返回平均金额;
}
双对数中值(整数m1,整数m2,整数m3){
int x=num1-num2;
int y=num2-num3;
int z=num1-num3;
如果(x*y>0)返回num2;
如果(x*z>0)返回num3;
返回num1;
}
两位数(整数num1){
整数位数=0;
while(num1){
num1=num1/10;
数字++;
}
返回(位数);
}
int main(){
双num1=0;
双num2=0;
双num3=0;
coutnum1;
cin>>num2;
cin>>num3;
cout您可以使用for循环输入数字
当用户输入任何负数时,您可以显示错误消息并退出主功能
例如:
double num[3];
cout您可以使用for循环输入数字
当用户输入任何负数时,您可以显示错误消息并退出主功能
例如:
double num[3];
cout您可以使用do while循环编写代码:
do {
cout << "Enter 3 integers" << endl;
cin >> num1;
cin >> num2;
cin >> num3;
} while ((num1 < 0) || (num2 < 0) || (num3 < 0));
do{
coutnum1;
cin>>num2;
cin>>num3;
}而((num1<0)| |(num2<0)| |(num3<0));
否则,仅使用while循环作为:
while (true) {
cout << "Enter 3 integers" << endl;
cin >> num1;
// if any number is -ve, continue the whole loop
if (num1 < 0) continue;
cin >> num2;
if (num2 < 0) continue;
cin >> num3;
if (num3 < 0) continue;
// if every number is positive, just break out of the loop
break;
}
while(true){
coutnum1;
//如果任何数字为-ve,则继续整个循环
如果(num1<0)继续;
cin>>num2;
如果(num2<0)继续;
cin>>num3;
如果(num3<0)继续;
//如果每个数字都是正数,就从循环中跳出来
打破
}
希望这有帮助,谢谢
-Rajkumar您可以使用do-while循环编写代码:
do {
cout << "Enter 3 integers" << endl;
cin >> num1;
cin >> num2;
cin >> num3;
} while ((num1 < 0) || (num2 < 0) || (num3 < 0));
cout << "Enter 3 integers" << endl;
cin >> num1;
cin >> num2;
cin >> num3;
while(num1 < 0 || num2 < 0 || num3 < 0)
{
if (num1 < 0)
{
cout << "Your first number [" << num1 << "] is negetiv. Please enter a positive number!";
cin >> num1;
}
if (num2 < 0)
{
cout << "Your second number [" << num2 << "] is negetiv. Please enter a positive number!";
cin >> num2;
}
if (num3 < 0)
{
cout << "Your third number [" << num3 << "] is negetiv. Please enter apositive number!";
cin >> num3;
}
}
do{
coutnum1;
cin>>num2;
cin>>num3;
}而((num1<0)| |(num2<0)| |(num3<0));
否则,仅使用while循环作为:
while (true) {
cout << "Enter 3 integers" << endl;
cin >> num1;
// if any number is -ve, continue the whole loop
if (num1 < 0) continue;
cin >> num2;
if (num2 < 0) continue;
cin >> num3;
if (num3 < 0) continue;
// if every number is positive, just break out of the loop
break;
}
while(true){
coutnum1;
//如果任何数字为-ve,则继续整个循环
如果(num1<0)继续;
cin>>num2;
如果(num2<0)继续;
cin>>num3;
如果(num3<0)继续;
//如果每个数字都是正数,就从循环中跳出来
打破
}
希望这有帮助,谢谢
-拉吉库马尔coutnum1;
cout << "Enter 3 integers" << endl;
cin >> num1;
cin >> num2;
cin >> num3;
while(num1 < 0 || num2 < 0 || num3 < 0)
{
if (num1 < 0)
{
cout << "Your first number [" << num1 << "] is negetiv. Please enter a positive number!";
cin >> num1;
}
if (num2 < 0)
{
cout << "Your second number [" << num2 << "] is negetiv. Please enter a positive number!";
cin >> num2;
}
if (num3 < 0)
{
cout << "Your third number [" << num3 << "] is negetiv. Please enter apositive number!";
cin >> num3;
}
}
cin>>num2;
cin>>num3;
而(num1<0 | | num2<0 | | num3<0)
{
if(num1<0)
{
coutcout num1;
cin>>num2;
cin>>num3;
而(num1<0 | | num2<0 | | num3<0)
{
if(num1<0)
{
cout如果希望用户输入无符号整数值,请首先使用无符号整数值类型进行输入。并执行错误检查(例如,无符号整数num1、num2、num3;If(!(std::cin>>num1>>num2>>num3)){/*输入中的错误*/}
)@Someprogrammerdude的可能重复但是,将-1解析为无符号int,可能会意外地产生一个巨大的正值而不是一个错误。@moooeeep啊,我知道为什么会发生这种情况……这是因为输入被解析为by(参见)。如果值超出范围(例如负数),则返回ULLONG_MAX
(并且errno
设置为ERANGE
)。在读取和验证时需要考虑这一点。@一些程序员已经发现了一个问题:如果希望用户输入无符号整数值,请从使用无符号整数值类型作为输入开始。然后进行错误检查(例如,unsigned int num1,num2,num3;if(!(std::cin>>num1>>num2>>num3)){/*输入错误*/}
)可能重复@Someprogrammerdude,但是,将-1解析为unsigned int,可能会意外地产生一个巨大的正值,而不是错误。@moooeeep啊,我知道为什么会发生这种情况……这是因为输入是按解析的(请参阅)。如果值超出范围(例如负数),则返回ULLONG_MAX
(并且errno
设置为ERANGE
)。在读取和验证时需要考虑这一点。@Someprogrammerdude已经发现了一个关于这一点的问题: