C++ C++;写入文件

C++ C++;写入文件,c++,C++,程序应该写入用户输入和与文件“output.txt”等效的罗马数字,格式为1984:mcmlxxiv,它是原始用户输入和转换函数的结果。在我的文本文件中,我只能在该文件成功创建的txt文档上获取“0:”。代码如下 #include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; string convert(int d

程序应该写入用户输入和与文件“output.txt”等效的罗马数字,格式为1984:mcmlxxiv,它是原始用户输入和转换函数的结果。在我的文本文件中,我只能在该文件成功创建的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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
字符串转换(整数位数、字符串低位、字符串中间、字符串高位);
void saveToFile(int&,string[],const int&);
int main()
{
const int MAX_INPUT=3999,MIN_INPUT=1,//这些常量包含高整数和低整数,
数组_SIZE=4;//和数组大小声明符。
string answers[ARRAY_SIZE]={“”、“”、“”、“”};//保存convert函数输出的字符串数组。
int acculator=0;//用于保存已转换阿拉伯数字数量的变量。
int userNum=0;//用于保存用户输入的变量。
saveToFile(userNum、answers、数组大小);
do{//Main循环-确保重复执行,直到输入负循环。
cout userNum)| |(userNumMAX_INPUT)){//输入验证-仅继续
if(userNum<0)//有效,范围内输入。
{

不能在循环之前调用
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); 
//在一行上打印出等效的罗马数字。

cout[OT]:避免将幻数设置为
89
121
,使用
'y'
'y'
。如果我了解您正在接受用户输入,请将其转换为罗马数字值,然后将其保存到文件。是..保存到文件并输出到终端..您可以轻松地将整数提取概括为:
int digital_value=num%std::pow(10.0,digit)/std::pow(10.0,digit-1)
把它放在一个函数中,你需要调用的就是:
int-one=digit(userNum,1);int-tens=digit(userNum,2);int-hund=digit(userNum,3);int-thou=digit(userNum,4)
。我需要按照规范要求,作为程序的第一行询问用户。但是,这确实解决了输出打印问题。是否有任何方法可以使第一行询问的问题得到相同的结果。--也只打印输入到文件中的最后一个数字。我需要在文件中生成一个列表,其中包含在该程序运行中输入的所有输入程序-因此,首先询问用户,然后询问是否保存所有用户名及其等效罗马输入。拆分函数:-询问用户是否要保存和存储结果(在循环之前)-进行真正的保存(可能必须更改为附加模式)。