【发布时间】:2021-03-15 18:44:46
【问题描述】:
我正在学习编程,并且我编写了一个代码来平均我想要的尽可能多的数字。该代码在 Visual Studio 中完美运行,但是当我尝试使用 exe 文件运行它时,它会在我输入“end”后死掉。我使用“结束”来打破循环(第 23 行)。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string n = "";
double a = 0;
double counter = 0;
string list = "0123456789.";
int b = 0;
char betu = '\0';
int num = 0;
double value = 0;
while (true) {
betu = '\0';
n = "";
a = 0;
b = 0;
cout << "Send in a number:" << endl;
cin >> n;
b = n.length();
if (n == "end") {
break;
}
for (int i = 0; i < b; i++) {
if (list.find(n[i]) == string::npos) {
cout << "The number is not correct.";
exit(EXIT_FAILURE);
}
}
a = stod(n);
counter += a;
num++;
}
if (num == 0) {
cout << "not enough numbers to count with." << endl;
}
else {
value = counter / num;
cout << "The avarage of your results is: " << endl;
cout << value << endl;
}
return 0;
}
我希望“结束”退出循环并在循环后继续程序。
【问题讨论】:
-
更清楚:它正在在循环之后运行代码。
-
从命令行运行应用程序。
-
@Yksisarvinen 我尝试了在那篇文章中写的方法,例如 system("pause");或 getchar();在主要功能的最后,但他们都没有解决我的问题。我尝试使用 ctrl + f5 运行它(无需调试即可运行)并且它可以工作,但是当我打开 exe 时它仍然无法工作。所以它似乎只适用于 Visual Studio。
-
@VargaÁgoston 你把系统放在哪里(“暂停”)?
标签: c++ while-loop break