[英]C++ Loop back beginning if the result is false
I am very new to C++ and still studying.我对 C++ 很陌生,还在学习。 The below scenario just came to my mind and I was trying to figure it out how to do this.
下面的场景突然出现在我的脑海中,我试图弄清楚如何做到这一点。
Scenario is as below:-场景如下:-
x
x
int
or float
int
还是float
int
, then pop up a message "Entered Number is not a Decimal Number" and go back to the beginning and inform the user to re-enter a numberint
,则弹出消息“输入的数字不是十进制数”,并 go 回到开头并通知用户重新输入数字float
then I round it to the nearest int
and pop up a message cout<<"Nearst Rounded Number is: "<<round(x)<<endl;
float
,那么我将它四舍五入到最接近的int
并弹出一条消息cout<<"Nearst Rounded Number is: "<<round(x)<<endl;
I assume this can be done with a loop, but I cannot figure it out.我认为这可以通过循环来完成,但我无法弄清楚。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float x,y;
cout<<"Enter Number Here : ";
cin>>x;
{
if ( (x- int(x) == 0))
cout<<"Entered Number is not a Decimal Number"<<endl<<endl;
else
cout<<endl;
cout<<"Nearst Rounded Number is : "<<round(x)<<endl;
}
}
while(true) {
//(your existing code goes here)
cout << "Do you want to run the program again either types yes or no" << endl;
cin >> answer;
if (answer != 'yes' && answer != 'Yes')
break;
}
It should work.它应该工作。 Else you can keep one
bool
variable in if else
part and validate them below and break the while loop until your condition satisfy.否则,您可以在
if else
部分中保留一个bool
变量并在下面验证它们并中断 while 循环,直到您的条件满足。
I believe this is what you wanted:我相信这就是你想要的:
int main() {
string input_str;
float input_float = 0;
while (true) {
cout << "Enter number here: ";
cin >> input_str;
input_float = stof(input_str); //stof converts string to float
if (input_float != 0) {
if (input_float == int(input_float)) cout << "Entered number is not a decimal, please try again." << endl;
else break;
}
//TODO: Catch any exceptions thrown by stof (Ex: User inputs a letter)
}
cout << "Nearest Rounded number is: " << round(input_float)<<endl;
return 0;
}
Bye!再见!
Edit: Changed the code a bit and removed a bug.编辑:稍微更改了代码并删除了一个错误。
I have changed your code a bit to take input continuously.我已经稍微更改了您的代码以连续输入。 while(cin>>x) means the program is taking input constiniously until EOF.
while(cin>>x) 表示程序一直在接受输入,直到 EOF。 Then when you find a decimal number, it breaks.
然后,当您找到一个十进制数时,它会中断。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float x,y;
cout<<"Enter Number Here : ";
while(cin>>x)
{
if ( (x- int(x) == 0))
cout<<"Entered Number is not a Decimal Number"<<endl<<"Enter Number Here : ";
else
{
cout<<"Nearst Rounded Number is : "<<round(x)<<endl;
break;
}
}
}
By the way I will advise you to spend a bit more time to find out the solution on your own before posting here.顺便说一句,我建议您在此处发布之前花更多时间自行找出解决方案。
first of all Why is “using namespace std;”首先为什么是“使用命名空间标准;” considered bad practice?
被认为是不好的做法? second - use a loop with a boolean flag to indicate when you want the exit the loop
第二 - 使用带有 boolean 标志的循环来指示您何时想要退出循环
#include <iostream>
#include <cmath>
int main()
{
float x,y;
bool flag = true;
while(flag)
{
std::cout<<"Enter Number Here : ";
std::cin>>x;
{
if ( (x- int(x) == 0))
std::cout<<"Entered Number is not a Decimal Number"<<std::endl;
else{
std::cout<<std::endl;
std::cout<<"Nearst Rounded Number is : "<<round(x)<<std::endl;
flag = false;
}
}
}
}
Your code needs improvements like indentations and the use of loop conditions otherwise your program will not rerun again.您的代码需要改进,例如缩进和循环条件的使用,否则您的程序将不会再次重新运行。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float x,y;
bool correctinputFlag=false;
do()
{
cout<<"Enter Number Here : ";
cin>>x;
if ( (x- int(x) == 0))
{
cout<<"Entered Number is not a Decimal Number"<<endl<<endl;
}
else
{
cout<<endl;
cout<<"Nearst Rounded Number is : "<<round(x)<<endl;
correctinputFlag=true;
}
}while(correctinputFlag==false);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.