2017-04-19 3 views
0

C++の新機能です。私はプログラミングしている宿題プログラムを持っています。しかし、私たちの先生は、私たちがプログラムに間違いをつけ加えることを望んでいます。プログラムの再実行を促している間にエラー修正しようとしています

ユーザーに再度実行するように要求するコードに問題が発生します。私は変数が変更されるまでそれを監視するためにwhileループを使用します。

これは動作します:

#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' に等しくなるまで、プログラムをループし続け、その後、停止します。問題が発生した場所

#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; 
} 

ここにある:さて、私は更新されたコードがされるだけここでYまたはNを入力するユーザーを制限するために、プログラムを再度実行についての質問のためにいくつかのエラー訂正を利用するためのプログラムを修正しました。ユーザが「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; 
} 

ヘルプはありますか?ありがとう!!!

OK、実際のプログラムの中に何かあるようです。ここでは、ソースコードがあります:

#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 
+0

デバッガでステップバイステップでデバッグしようとしましたか?あなたのプログラムがクラッシュするような、あなたの「ここで何かする」の中に何かがないのは確かですか? – Ceros

+0

私は再現できません。あなたのコード( 'while(runAgain){...}')は正常に動作します。 – Arash

+0

デスクトップで再生できません。あなたのビルド/実行環境は何ですか? –

答えて

0

OKに見えます。ファイル操作はdoループ内にありました。私はループの外にそれらを移動し、それはすべて動作します。ありがとう!

関連する問題