帮助同时控制结构程序
Help!? While Control Structure Program!
我正试图在第二个循环中输入账号。如果我在"While"控制结构中输入"Enter the account number"。它将在第一个循环中打印两次。那么我该如何解决这个问题呢?
/* Make a program that will determine if a department store customer has exceeded the
credit limit on a charge account*/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int aNum,Always;
double balance,iTotal,cTotal,cLimit,NewBal;
cout << "Enter account number: ";
cin >> aNum;
while ( aNum != -1 )
{
cout << "Enter beginning balance: ";
cin >> balance;
cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 );
cout << "Enter total charges: ";
cin >> iTotal;
cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 );
cout << "Enter total credits: ";
cin >> cTotal;
cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 );
cout << "Enter credit limit: ";
cin >> cLimit;
cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision(2);
cout << endl;
NewBal = balance + iTotal - cTotal;
if ( NewBal >= cLimit ) {
cout << "Account: " << setw(9) << aNum << endl;
cout << "Credit limit: " << cTotal << endl;
cout << "Balance: " << setw(9) << balance << endl;
cout << "Credit limit exceeded." << endl;
cout << endl;
}
}
return 0;
}
我会这样构建代码:
while(true)
{
cout << "Enter account number: ";
cin >> aNum;
if(aNum==-1) break;
// ... Rest of your while loop ...
}
尝试使用下面的代码
/* Make a program that will determine if a department store customer has exceeded the
credit limit on a charge account*/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int aNum,Always;
double balance,iTotal,cTotal,cLimit,NewBal;
do
{
cout << "Enter account number: ";
cin >> aNum;
if(aNum != -1)
{
cout << "Enter beginning balance: ";
cin >> balance;
cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 );
cout << "Enter total charges: ";
cin >> iTotal;
cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 );
cout << "Enter total credits: ";
cin >> cTotal;
cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 );
cout << "Enter credit limit: ";
cin >> cLimit;
cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision(2);
cout << endl;
NewBal = balance + iTotal - cTotal;
if ( NewBal >= cLimit ) {
cout << "Account: " << setw(9) << aNum << endl;
cout << "Credit limit: " << cTotal << endl;
cout << "Balance: " << setw(9) << balance << endl;
cout << "Credit limit exceeded." << endl;
cout << endl;
}
}
}while ( aNum != -1 );
return 0;
}
希望它能帮助。。。
相关文章:
- 员工测试驱动程序数据结构
- 我的堆栈和库存清单程序的结构有什么问题?
- 从c#lib返回一个包含字符串到c 程序的结构
- 使用帮助程序模板结构时无法推断模板参数
- 哪种控制结构所需的时间复杂度更低?
- 我们可以在不使用任何控制结构的情况下从函数中返回基于决策的值
- 带有结构和指针的程序错误(结构,C++)
- rxcpp:嵌套的 while 循环或类似的程序命令式结构"classic"
- C++程序控制终端窗口
- 使用 Qt 的简单线程 gui 应用程序的结构是什么?
- C++:使用 API 的应用程序体系结构
- C++中的流控制结构,如何修改
- 帮助同时控制结构程序
- 使用winapi进行远程程序控制
- 使用 if-else 控制结构的意外输出
- 完全与视图层无关的应用程序体系结构
- 运行这个简单的C ++程序(对结构数组进行排序)的更快方法
- 变量定义在 C++ 的控制结构中做什么
- 生成注释以解释C++程序的结构
- 在控制结构块中定义变量