C++ 试图走出这个循环
如果代码看起来很草率,我很抱歉。到目前为止,我在书中只学习了一些循环和if检查。我想编写代码以跳出循环。我试过使用break,然后尝试使用这个循环C++ 试图走出这个循环,c++,C++,如果代码看起来很草率,我很抱歉。到目前为止,我在书中只学习了一些循环和if检查。我想编写代码以跳出循环。我试过使用break,然后尝试使用这个循环 #include "std_lib_facilities.h" using namespace std; int main() { double smallest = 0; double largest = 0; double x = 0; string measure = " "; double sum
#include "std_lib_facilities.h"
using namespace std;
int main()
{
double smallest = 0;
double largest = 0;
double x = 0;
string measure = " ";
double sum = 0;
vector<double> meters;
bool leave = false;
if (!leave)
{
while(cin>>x>>measure)
{
if (x < largest)
smallest = x;
else
largest = x;
if ( x == 'x')
leave = true;
cout << "You typed " << x << " Smallest number so far: " << smallest << " Largest number so far: " << largest << endl;
if(measure == "in") //inches
{
cout << "You wanted inches? : " << x << " inches" << endl;
cout << "That also happens to be : " << x * 2.54 << " cm" << endl; // inches to cm
sum += x * 0.0254;
meters.push_back(sum);
cout << "Meters so far : " << sum << endl;
}
else if(measure == "cm") //centimeter
{
cout << "You wanted centimeters? : " << x << " centimeter" << endl;
cout << "That also happens to be : " << x / 100 << " m" << endl; // inches to cm
sum += x / 100;
meters.push_back(sum);
cout << "Meters so far : " << sum << endl;
}
else if(measure == "f") //feet
{
cout << "You wanted feet? : " << x << " feet" << endl;
cout << "That also happens to be : " << x * 12 << " inches" << endl; // inches to cm
sum += x * 0.3048;
meters.push_back(sum);
cout << "Meters so far : " << sum << endl;
}
else if(measure == "m") //meters
{
cout << "You wanted meters? : " << x << " meters" << endl;
cout << "That also happens to be : " << x * 100 << " cm" << endl; // inches to cm
sum += x;
meters.push_back(sum);
cout << "Meters so far : " << sum << endl;
}
else
{
cout << "error invalid measurement. " << endl;
keep_window_open();
}
}
}
for(int i = 0; i<meters.size(); ++i)
cout << meters[i];
keep_window_open();
}
#包括“std_lib_facilities.h”
使用名称空间std;
int main()
{
双最小=0;
双最大=0;
双x=0;
字符串度量=”;
双和=0;
向量计;
bool leave=false;
如果(!离开)
{
而(cin>>x>>测量)
{
如果(x<最大值)
最小=x;
其他的
最大=x;
如果(x=='x')
离开=真;
你可以在循环之前检查离开
条件,这当然不会很好地工作。你应该在循环内部检查它
可将其放入实际回路条件中:
while(!leave && cin >> x >> measure) { ... }
似乎您希望输入是数字和字符串,或者只是一个字符。这将不起作用,因为变量x
是double
,并且无法处理正在输入的字符串或字符。实际上,您应该得到一个关于使用double
作为字符的警告(比较x='x'
)
最好是做类似的事情
std::string input;
while (std::getline(std::cin, input))
{
std::istringstream is(input);
// Try to get a number and a string
if (is >> x >> measure)
{
// Got it, do stuff here...
}
else
{
// Input was not a number and a string, try to get a character
char ch;
if (is >> ch && ch == 'x')
break; // Exit loop
else
{
std::cout << "Wrong input, have to be a number and a string, or 'x' to exit\n";
}
}
}
std::字符串输入;
while(std::getline(std::cin,输入))
{
std::istringstream是(输入);
//尝试获取一个数字和一个字符串
如果(is>>x>>测量)
{
//明白了,在这里做事。。。
}
其他的
{
//输入不是数字和字符串,请尝试获取字符
char ch;
如果(是>>ch&&ch='x')
break;//退出循环
其他的
{
std::cout您可以在循环之前检查离开
条件,这当然不会很好地工作。您应该在循环内部检查它
可将其放入实际回路条件中:
while(!leave && cin >> x >> measure) { ... }
似乎您希望输入是数字和字符串,或者只是一个字符。这将不起作用,因为变量x
是double
,并且无法处理正在输入的字符串或字符。实际上,您应该得到一个关于使用double
作为字符的警告(比较x='x'
)
最好是做类似的事情
std::string input;
while (std::getline(std::cin, input))
{
std::istringstream is(input);
// Try to get a number and a string
if (is >> x >> measure)
{
// Got it, do stuff here...
}
else
{
// Input was not a number and a string, try to get a character
char ch;
if (is >> ch && ch == 'x')
break; // Exit loop
else
{
std::cout << "Wrong input, have to be a number and a string, or 'x' to exit\n";
}
}
}
std::字符串输入;
while(std::getline(std::cin,输入))
{
std::istringstream是(输入);
//尝试获取一个数字和一个字符串
如果(is>>x>>测量)
{
//明白了,在这里做事。。。
}
其他的
{
//输入不是数字和字符串,请尝试获取字符
char ch;
如果(是>>ch&&ch='x')
break;//退出循环
其他的
{
std::为什么break
对你不起作用呢?@Joachim Pileborg break只是关闭了整个窗口,而没有打印下面的for循环。在for
循环下面没有打印。除非它在保持窗口打开
?你也能显示一下吗?内联void保持窗口打开(){cin clear();cout>ch;return;}
我没有写这篇文章,页眉是这本书的一部分。为什么break
对你不起作用?那怎么办?@Joachim Pileborg break刚刚关闭了整个窗口,没有打印下面的for循环。在for
循环下面没有打印。除非它在保持窗口打开中,你能把它也显示出来吗<代码>内联void keep_window_open(){cin.clear();cout>ch;return;}
我没有写这个,页眉是这本书的一部分。12f你输入了12个迄今为止最小的数字:0个迄今为止最大的数字:12英尺:你想要12英尺?:12英尺,也恰好是:144英寸米迄今为止:3.6576 x程序关闭。
我正在尝试正确地隔开我上面的代码。对不起,我是这里的新手>@user2442335两件事:你想要输入'x'
以退出循环并同时退出程序,是吗?当您输入'x'
时,input语句尝试将其读入变量x
,该变量不是一个字符串或多个字符,因此input语句失败。您可能希望将输入拆分为两个单独的语句,或者获取整行并解析单独输入。@user2442335用一个可能的解决方案更新了我的答案该解决方案有一些我还没有学到的东西。有更简单的解决方案吗?我觉得我想把事情复杂化了。12f您键入了12个迄今为止最小的数字:0个迄今为止最大的数字:12您想要的英尺?:12英尺也恰好是:144英寸ers到目前为止:3.6576 x程序关闭。
我正在尝试正确地隔开上面的代码。很抱歉,我是这里的新手>@user2442335有两件事:你想输入'x'
退出循环并退出程序,是吗?当你输入'x'
时,input语句试图将其读入变量x
,该变量不是字符串或字符,因此输入语句失败。您可能希望将输入拆分为两个单独的语句,或者获取整行内容并分别分析输入。@user2442335用一个可能的解决方案更新了我的答案该解决方案有我尚未了解的内容。有更简单的解决方案吗?我觉得我试图将其过于复杂化嗯。