繁体   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