在提示再次运行程序时尝试进行错误校正
Trying to have error correction while prompting to run program again
新的C 。我有一个正在编程的作业计划。这几乎已经完成了,但是我们的老师希望我们在程序中添加错误。
该问题在代码中出现,要求用户再次运行。我使用一段时间循环监视变量直到变量更改为止。
这有效:
#include <iostream>
using namespace std;
int main() {
char runAgainYN;
while ( toupper(runAgainYN != 'N' ) {
// Do some stuff here!
cout << "Would you like to run again?";
cin >> runAgainYN;
}
return 0;
}
它一直在循环程序,直到Runagain等于" N",然后停止。现在,我修改了该程序以使用有关再次运行程序的问题,以限制用户仅输入Y或N。以下是更新的代码:
#include <iostream>
#include <cctype>
using namespace std;
int main() {
char runAgainYN;
bool runAgain = true;
while ( runAgain ) {
// Do some stuff here!
bool validResponse = false;
while ( !validResponse ) {
cout << "Would you like to run the program again (Y/N): ";
cin >> runAgainYN;
if ( toupper(runAgainYN) == 'Y' ) {
validResponse = true;
cin.ignore();
}
else if ( toupper(runAgainYN) == 'N' ) {
runAgain = false;
validResponse = true;
cin.ignore();
}
else {
cout << "INVALID RESPONSE" << endl;
}
}
}
return 0;
}
这是问题出现的地方。如果用户输入" n",则程序将使用代码0退出,如果用户输入了Y或N以外的任何内容,则无效响应触发并再次索取输入。但是,如果用户输入Y,则程序将使用代码-1退出。嗯?我尝试了另一种方法,结果相同:
#include <iostream>
#include <cctype>
using namespace std;
int main() {
char runAgainYN;
do {
// Do some stuff here!
bool validResponse = false;
while ( !validResponse ) {
cout << "Would you like to run the program again (Y/N): ";
cin >> runAgainYN;
if ( toupper(runAgainYN) == 'Y' ) {
validResponse = true;
cin.ignore();
}
else if ( toupper(runAgainYN) == 'N' ) {
runAgain = false;
validResponse = true;
cin.ignore();
}
else {
cout << "INVALID RESPONSE" << endl;
}
}
}
while ( runAgain );
return 0;
}
有帮助的人吗?谢谢!!!
好吧,所以这似乎是导致它的实际程序中的某些东西。这是源代码:
#include <iostream>
#include <string>
#include <string.h>
#include <iomanip>
#include <cctype>
#include <limits>
#include <algorithm>
#include <fstream>
using namespace std;
void cls();
void printLine( int length );
void showCurrency( double dv, int width = 14 );
int main() {
string itemName[999][2];
double itemPrice[999][3];
double salesTotal = 0.0;
double salesTax = 0.0;
double totalTax = 0.0;
double taxRate = 0.0;
double grandTotal = 0.0;
double test = 0.0;
int numLines = 0;
string readLine;
string temp;
ifstream fileIn;
string menuHeader = "Sales Receipt from File";
char runAgainYN;
bool runAgain = true;
do { // Loop until runAgain false
// Open the file and count the number of lines, then close for next operation:
fileIn.open("cost.txt");
while (!fileIn.eof()) {
fileIn >> temp;
numLines++;
temp = "";
}
fileIn.close();
// Open the file and move the data into the arrays, then close:
fileIn.open("cost.txt");
for ( int i = 0; i < numLines; i++) {
fileIn >> itemName[i][1] >> itemPrice[i][1];
}
fileIn.close();
cls();
numLines = numLines / 2;
cout << "/";
printLine(80);
cout << "\" << endl;
cout << "|" << setw(81) << "|" << endl;
cout << "|" << setw(41 + (menuHeader.length() / 2)) << menuHeader << setw(40 - (menuHeader.length() / 2)) << "|" << endl;
cout << "|" << setw(81) << "|" << endl;
cout << "\";
printLine(80);
cout << "/" << endl << endl;
cout << "Enter the sales tax percentage (ie for 6% enter 6): ";
// Ask for taxRate and error check:
while (!(cin >> taxRate)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "INVALID RESPONSE" << endl << "Please enter a number: ";
}
cout << endl;
salesTax = taxRate / 100; // Convert sales tax to percentage
for (int i = 0; i < numLines; i++ ) { // Set the running tax amounts
itemPrice[i][2] = itemPrice[i][1] * salesTax;
salesTotal = salesTotal + itemPrice[i][1];
totalTax = totalTax + itemPrice[i][2];
}
//totalTax = salesTotal * salesTax; // Calculate tax
grandTotal = salesTotal + totalTax; // Calculate grand total
// Output:
cls();
cout << "/" << setfill('-') << setw(63) << "-" << "\" << setfill(' ') << endl;
cout << "| SALES RECEIPT |" << endl;
cout << "|" << setfill('-') << setw(63) << "-" << "|" << setfill(' ') << endl;
cout << "| " << left << setw(32) << "Sales item" << setw(13) << right << "Price" << setw(18) << "Tax |" << endl;
cout << "|" << setfill('-') << setw(63) << "-" << "|" << setfill(' ') << endl;
for ( int i = 0; i <= numLines - 1; ++i ){
cout << "| " << left << setw(32) << itemName[i][1] << "$" << setw(12) << setprecision(2) << fixed << right << itemPrice[i][1] << setw(5) << "$" << setw(11) << itemPrice[i][2] << " |" << endl;
}
cout << "|" << setfill('-') << setw(63) << "-" << "|" << setfill(' ') << endl;
cout << "| Total Sales" << setw(36);
showCurrency(salesTotal);
cout << " |" << endl;
cout << "| Sales Tax (" << setprecision(0) << fixed << taxRate << "%)" << setw(33);
showCurrency(totalTax);
cout << " |" << endl;
cout << "|" << setfill('-') << setw(63) << "-" << "|" << setfill(' ') << endl;
cout << "| Grand Total" << setw(36);
showCurrency(grandTotal);
cout << " |" << endl;
cout << "\" << setfill('-') << setw(63) << "-" << "/" << setfill(' ') << endl;
cout << endl;
// Clear vars and array for next run:
salesTax = 0.0;
totalTax = 0.0;
salesTotal = 0.0;
grandTotal = 0.0;
memset(itemPrice, 0, sizeof(itemPrice));
memset(itemName, 0, sizeof(itemName));
// Ask if program is to be run again:
bool validResponse = false;
while ( !validResponse ) {
cout << "Would you like to enter a new tax rate (Y/N): ";
cin >> runAgainYN;
if ( toupper(runAgainYN) == 'Y' ) {
validResponse = true;
cin.ignore();
}
else if ( toupper(runAgainYN) == 'N' ) {
runAgain = false;
validResponse = true;
cin.ignore();
}
else {
cout << "INVALID RESPONSE" << endl;
}
}
}
while ( runAgain == true );
return 0;
}
void printLine( int length ) {
for ( int i = 0; i < length; i++ ) {
cout << "=";
}
}
void cls() {
// check OS and run correct clear screen (I do some of my coding in Linux :)
#if (defined (_WIN32) || defined (_WIN64))
system("CLS");
#elif (defined (LINUX) || defined (__linux__))
system("clear");
#endif
}
void showCurrency(double dv, int width){
/* Credit where credit is due:
* The following code snippet was found at https://arachnoid.com/cpptutor/student3.html
* Copyright © 2000, P. Lutus. All rights reserved.
*/
const string radix = ".";
const string thousands = ",";
const string unit = "$";
unsigned long v = (unsigned long) ((dv * 100.0) + .5);
string fmt,digit;
int i = -2;
do {
if(i == 0) {
fmt = radix + fmt;
}
if((i > 0) && (!(i % 3))) {
fmt = thousands + fmt;
}
digit = (v % 10) + '0';
fmt = digit + fmt;
v /= 10;
i++;
}
while((v) || (i < 1));
cout << unit << setw(width) << fmt.c_str();
}
这是'cost.txt'的内容:
Books 45.01
Pens 21.03
Pencils 10.90
Hats 50.00
Caps 800.00
Food 1.00
好的,看起来我明白了。文件操作在DO循环范围内。我将它们移到循环外,这一切都可以。谢谢!
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Windows 凭据提示问题,CredUnPackAuthenticationBuffer 错误 50
- 在提示再次运行程序时尝试进行错误校正
- 我重载了一个运算符*()函数来计算两个矩阵的总和,但编译器提示此错误
- 代码没有任何错误,但什么也不在命令提示中
- C 机器人,作业分配 - 由于内存泄漏而导致阀门错误.确实需要一些提示
- 运行时间错误:程序跳过提示,以获取第二名和第三个名称
- 如何在窗口提示对话框(即错误消息)时获取通知
- 错误LNK2019:未解析的外部符号新手需要提示
- 如何确保我的代码是ansi C,不涉及C++.像gcc这样的编译器是否可以设置为在满足C++中的功能时提示错误
- 以下错误 C2248 的 bug 搜寻提示:'std::basic_ios<_Elem,_Traits>::basic_ios':无法访问类中声明的私有成员