C++ 代码不';不要超越输入阶段
我已经构建了一个简单的程序,它应该以美分为单位,用户以美元和美分的形式输入和输出它,但是,当我运行它时,它会向我索要金额,我输入它,程序不会继续。我是编程新手,非常感谢在这件事上能得到的任何帮助C++ 代码不';不要超越输入阶段,c++,C++,我已经构建了一个简单的程序,它应该以美分为单位,用户以美元和美分的形式输入和输出它,但是,当我运行它时,它会向我索要金额,我输入它,程序不会继续。我是编程新手,非常感谢在这件事上能得到的任何帮助 #include <iostream> using namespace std; int main() { int numb; int dollars=0; int cents=0; cout &
#include <iostream>
using namespace std;
int main()
{
int numb;
int dollars=0;
int cents=0;
cout << "Enter the amount" << endl;
cin >> numb;
while (numb>=0);
{
dollars=int(numb/100);
cents=numb%100;
}
cout << dollars << "dollars" << cents << "cents"<<endl;
return 0;
}
#包括
使用名称空间std;
int main()
{
智力麻木;
整数美元=0;
整数分=0;
不能麻木;
而(numb>=0);
{
美元=整数(numb/100);
美分=麻木%100;
}
cout您的'while'块有一个无限循环,因为numb总是正的(任何迭代都不会改变)。
尝试删除while循环以让程序继续
#include <iostream>
using namespace std;
int main()
{
int numb;
int dollars=0;
int cents=0;
cout << "Enter the amount" << endl;
cin >> numb;
dollars=int(numb/100);
cents=numb%100;
cout << dollars << "dollars" << cents << "cents" << endl;
return 0;
}
#包括
使用名称空间std;
int main()
{
智力麻木;
整数美元=0;
整数分=0;
不能麻木;
美元=整数(numb/100);
美分=麻木%100;
cout您的'while'块有一个无限循环,因为numb总是正的(任何迭代都不会改变)。
尝试删除while循环以让程序继续
#include <iostream>
using namespace std;
int main()
{
int numb;
int dollars=0;
int cents=0;
cout << "Enter the amount" << endl;
cin >> numb;
dollars=int(numb/100);
cents=numb%100;
cout << dollars << "dollars" << cents << "cents" << endl;
return 0;
}
#包括
使用名称空间std;
int main()
{
智力麻木;
整数美元=0;
整数分=0;
不能麻木;
美元=整数(numb/100);
美分=麻木%100;
cout您的'while'块有一个无限循环,因为numb总是正的(任何迭代都不会改变)。
尝试删除while循环以让程序继续
#include <iostream>
using namespace std;
int main()
{
int numb;
int dollars=0;
int cents=0;
cout << "Enter the amount" << endl;
cin >> numb;
dollars=int(numb/100);
cents=numb%100;
cout << dollars << "dollars" << cents << "cents" << endl;
return 0;
}
#包括
使用名称空间std;
int main()
{
智力麻木;
整数美元=0;
整数分=0;
不能麻木;
美元=整数(numb/100);
美分=麻木%100;
cout您的'while'块有一个无限循环,因为numb总是正的(任何迭代都不会改变)。
尝试删除while循环以让程序继续
#include <iostream>
using namespace std;
int main()
{
int numb;
int dollars=0;
int cents=0;
cout << "Enter the amount" << endl;
cin >> numb;
dollars=int(numb/100);
cents=numb%100;
cout << dollars << "dollars" << cents << "cents" << endl;
return 0;
}
#包括
使用名称空间std;
int main()
{
智力麻木;
整数美元=0;
整数分=0;
不能麻木;
美元=整数(numb/100);
美分=麻木%100;
cout这个while循环没有任何意义。请删除它
while (numb>=0); // remove this line and the '{' '}'
{
dollars=int(numb/100);
cents=numb%100;
}
通常在使用循环时,检查它们是否会终止。您正在检查numb>=0
的每个迭代,但在循环中numb
永远不会更改其值。因此程序将永远执行此循环。此while循环没有任何意义。请删除它
while (numb>=0); // remove this line and the '{' '}'
{
dollars=int(numb/100);
cents=numb%100;
}
通常在使用循环时,检查它们是否会终止。您正在检查numb>=0
的每个迭代,但在循环中numb
永远不会更改其值。因此程序将永远执行此循环。此while循环没有任何意义。请删除它
while (numb>=0); // remove this line and the '{' '}'
{
dollars=int(numb/100);
cents=numb%100;
}
通常在使用循环时,检查它们是否会终止。您正在检查numb>=0
的每个迭代,但在循环中numb
永远不会更改其值。因此程序将永远执行此循环。此while循环没有任何意义。请删除它
while (numb>=0); // remove this line and the '{' '}'
{
dollars=int(numb/100);
cents=numb%100;
}
通常在使用循环时,检查它们是否会终止。您正在检查numb>=0
的每个迭代,但在循环内部numb
永远不会更改其值。因此程序将永远执行此循环。您创建了一个无限循环
while (numb>=0);
{
dollars=int(numb/100);
cents=numb%100;
}
numb是3,所以这总是3>=0等于真,所以你永远不会退出循环,你做了一个无限循环
while (numb>=0);
{
dollars=int(numb/100);
cents=numb%100;
}
numb是3,所以这总是3>=0等于真,所以你永远不会退出循环,你做了一个无限循环
while (numb>=0);
{
dollars=int(numb/100);
cents=numb%100;
}
numb是3,所以这总是3>=0等于真,所以你永远不会退出循环,你做了一个无限循环
while (numb>=0);
{
dollars=int(numb/100);
cents=numb%100;
}
numb是3,所以它总是3>=0等于true,因此,您永远不会退出循环,下面的代码应该按照您的预期工作。
阅读我在代码中插入的注释以了解更多信息。
此外,您可能希望在循环中插入额外的检查,以检查无效输入(即非数字字符),因为这将导致循环进入无限循环
编辑:我用额外的检查更新了代码,以处理无效的非数字用户输入
#include <iostream>
using namespace std;
int main()
{
int numb = 0;
int dollars=0;
int cents=0;
cout << "Enter the amount" << endl;
// I suspect your while loop here is to keep soliciting input
// if the input is not valid, so I've moved the "cin" into the loop.
// Don't use semi colon here after the while statement because
// in doing so, you're eliminating the body of the loop.
while (numb <= 0)
{
cin >> numb;
if(cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<int>::max(), '\n');
cout << "Non numeric input, please try again." << endl;
continue;
}
cout << "numb: " << numb << endl;
dollars=int(numb/100);
cents=numb%100;
}
// I've inserted extra spaces around dollars and cents here
// to make the output more readable.
cout << dollars << " dollars " << cents << " cents"<<endl;
return 0;
}
#包括
使用名称空间std;
int main()
{
int numb=0;
整数美元=0;
整数分=0;
cout下面的代码应该按照您的预期工作。
阅读我在代码中插入的注释以了解更多信息。
此外,您可能希望在循环中插入额外的检查,以检查无效输入(即非数字字符),因为这将导致循环进入无限循环
编辑:我用额外的检查更新了代码,以处理无效的非数字用户输入
#include <iostream>
using namespace std;
int main()
{
int numb = 0;
int dollars=0;
int cents=0;
cout << "Enter the amount" << endl;
// I suspect your while loop here is to keep soliciting input
// if the input is not valid, so I've moved the "cin" into the loop.
// Don't use semi colon here after the while statement because
// in doing so, you're eliminating the body of the loop.
while (numb <= 0)
{
cin >> numb;
if(cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<int>::max(), '\n');
cout << "Non numeric input, please try again." << endl;
continue;
}
cout << "numb: " << numb << endl;
dollars=int(numb/100);
cents=numb%100;
}
// I've inserted extra spaces around dollars and cents here
// to make the output more readable.
cout << dollars << " dollars " << cents << " cents"<<endl;
return 0;
}
#包括
使用名称空间std;
int main()
{
int numb=0;
整数美元=0;
整数分=0;
cout下面的代码应该按照您的预期工作。
阅读我在代码中插入的注释以了解更多信息。
此外,您可能希望在循环中插入额外的检查,以检查无效输入(即非数字字符),因为这将导致循环进入无限循环
编辑:我用额外的检查更新了代码,以处理无效的非数字用户输入
#include <iostream>
using namespace std;
int main()
{
int numb = 0;
int dollars=0;
int cents=0;
cout << "Enter the amount" << endl;
// I suspect your while loop here is to keep soliciting input
// if the input is not valid, so I've moved the "cin" into the loop.
// Don't use semi colon here after the while statement because
// in doing so, you're eliminating the body of the loop.
while (numb <= 0)
{
cin >> numb;
if(cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<int>::max(), '\n');
cout << "Non numeric input, please try again." << endl;
continue;
}
cout << "numb: " << numb << endl;
dollars=int(numb/100);
cents=numb%100;
}
// I've inserted extra spaces around dollars and cents here
// to make the output more readable.
cout << dollars << " dollars " << cents << " cents"<<endl;
return 0;
}
#包括
使用名称空间std;
int main()
{
int numb=0;
整数美元=0;
整数分=0;
cout下面的代码应该按照您的预期工作。
阅读我在代码中插入的注释以了解更多信息。
此外,您可能希望在循环中插入额外的检查,以检查无效输入(即非数字字符),因为这将导致循环进入无限循环
编辑:我用额外的检查更新了代码,以处理无效的非数字用户输入
#include <iostream>
using namespace std;
int main()
{
int numb = 0;
int dollars=0;
int cents=0;
cout << "Enter the amount" << endl;
// I suspect your while loop here is to keep soliciting input
// if the input is not valid, so I've moved the "cin" into the loop.
// Don't use semi colon here after the while statement because
// in doing so, you're eliminating the body of the loop.
while (numb <= 0)
{
cin >> numb;
if(cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<int>::max(), '\n');
cout << "Non numeric input, please try again." << endl;
continue;
}
cout << "numb: " << numb << endl;
dollars=int(numb/100);
cents=numb%100;
}
// I've inserted extra spaces around dollars and cents here
// to make the output more readable.
cout << dollars << " dollars " << cents << " cents"<<endl;
return 0;
}
#包括
使用名称空间std;
int main()
{
int numb=0;
整数美元=0;
整数分=0;
库特:为什么在这里使用while循环?尝试使用调试器并找到错误你自己:为什么在这里使用while循环?尝试使用调试器并找到错误你自己:为什么在这里使用while循环?尝试使用调试器并找到错误你自己:为什么在这里使用while循环?尝试使用调试器并找到错误你自己你疯了