簡體   English   中英

C++ 寫入文件

[英]C++ Writing to file

程序應該以這種形式寫入用戶輸入和等效於文件“output.txt”的羅馬數字1984: MCMLXXXIV這是原始用戶輸入和轉換函數的結果。 在我的文本文件中,我在文件成功創建的 txt 文檔上只得到“0:”。 代碼如下。

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;


string convert(int digit, string low, string mid, string high);         
void saveToFile(int &, string[], const int &);

int main()
{
    const int MAX_INPUT = 3999, MIN_INPUT = 1,                       // These constants hold high and low integer numbers,
        ARRAY_SIZE = 4;                                             // and the array size declarator.
    string answers[ARRAY_SIZE] = { "", "", "", "" };                //An  array of string to hold the output from the convert function.
    int accumulator = 0;                                            // Variable to hold number of arabic numbers converted.
    int userNum = 0;                                        // Variable to hold user input.


    saveToFile(userNum, answers, ARRAY_SIZE);
    do {                                                                    //Main loop - ensures repeated execution until negative entered. 

        cout << "Enter a negative number to end the program.\n";
        cout << "Enter an arabic number between 1 and 3999: ";
        accumulator++;

        while (!(cin >> userNum) || (userNum < MIN_INPUT || userNum > MAX_INPUT)){              //input validation - only proceed with
            if (userNum < 0)                                                                    //valid, in-range input.
            {
                cout << "Program Ending due to user request.";
                cout << endl << "Arabic numbers converted:    " << accumulator - 1 << endl;   //Counter
                cout << "Thank you for using the program. Have a nice day!" << endl;
                system("PAUSE");
                exit(EXIT_SUCCESS);                                                      //Termintaion with message and exit function
            }
            else {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');                        //handling of non-integer input.
                cout << "Invalid Value. Number must be between 1 and 3999:      "; 
            }
        }

        // Digit Extraction - turns userNum into four seperate values
        int thous = userNum / 1000;                                     //thousands place value
        int hund = userNum % 1000 / 100;                            //hundreds place value
        int tens = userNum % 100 / 10;                              //tens place value
        int ones = userNum % 10 / 1;                                //ones place value



     // filling answers ARRAY OF STRINGS with results from convert function. 
        answers[0] = convert(thous, "M", "M", "M");
        answers[1] = convert(hund, "C", "D", "M");
        answers[2] = convert(tens, "X", "L", "C");
        answers[3] = convert(ones, "I", "V", "X");


        // Printing out equivelent roman numeral on one line.
        cout << "\nRoman numeral for " << userNum << " is: ";
        cout << answers[0] << answers[1] << answers[2];
        cout << answers[3] << endl;




    } while (userNum > 0);                                                                  //Loop to allow multiple numbers per run.

    system("PAUSE");
    return 0;
  }

// Convert function - returns a string for roman numerals broken up by digits,     Accepts as arguments
//  the extracted digits and three string values known as low, med, and high.

 string convert(int digit, string low, string mid, string high)
{

    if (digit == 1)
    {
        return low;
    }
    if (digit == 2)
    {
        return low + low;
    }
    if (digit == 3)
    {
        return low + low + low;
    }
    if (digit == 4)
    {
        return low + mid;
    }
    if (digit == 5)
    {
        return mid;
    }
    if (digit == 6)
    {
        return mid + low;
    }
    if (digit == 7)
    {
        return mid + low + low;
     }
    if (digit == 8)
    {
        return mid + low + low + low;
    }
     if  (digit == 9)
    {
         return low + high;
    }
    if (digit == 0)
    {
        return "";
    }
}

void saveToFile(int &userNum, string answers[], const int &ARRAY_SIZE)
{

    char writeToFile;
    cout << "Do you want to write output to a file? Y/N   ";
    cin >> writeToFile;

    if (writeToFile == 89 || writeToFile == 121)
    {
        ofstream outputFile;
        outputFile.open("output.txt");

        if (outputFile)
        {
            outputFile << userNum << ":" << answers[0] + answers[1] + answers[2] + answers[3];
            outputFile.close();
        }
        else
        {
            cout << "Error opening the file.\n";
            exit(EXIT_FAILURE);
        }
    }
     else
        return;
}

您在循環之前調用saveToFile

您應該在循環結束時調用它:

    // Printing out equivalent roman numeral on one line.
    cout << "\nRoman numeral for " << userNum << " is: ";
    cout << answers[0] << answers[1] << answers[2];
    cout << answers[3] << endl;

    saveToFile(userNum, answers, ARRAY_SIZE);

} while (userNum > 0); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM