在 C++ 的文本文件中保存多个输出

Saving multiple output in a text file in c++

本文关键字:保存 输出 文件 C++ 文本      更新时间:2023-10-16

仍在尝试学习 c++。我们的教授希望我们制作一个程序,其中温度的输出将保存在文本文件中。可变结果是我要保存的输出。尽管代码有效并保存了结果,但问题是它只保存了第一个结果。我设置了一个限制,现在我只想保存至少 5 个结果。

#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
int main()
{
   double value, the_result, result;
   int choice;
   ofstream file;
   file.open("output.txt");

   do {
        cout <<"nnChoose among the following:"
            "n1 = Celsius to Fahrenheit"
            "n2 = Celsius to Kelvin"
            "n3 = Fahrenheit to Celsius"
            "n4 = Fahrenheit to Kelvin"
            "n5 = Kelvin to Celsius"
            "n6 = Kelvin to Fahrenheit"
            "n7 = Exit"
            "nnChoice:n";
        cin >> choice;

        switch (choice){
        case 1:
            cout<<"Enter Value"<< endl;
            if (!(cin >> value )){
                cout << "Invalid Input. Please Try Again!!";
                cin.clear();
                cin.ignore(123, 'n');
            }else{
            result = value*9/5+32;
            cout <<"The result is ~"<< result << "370";
            }
            break;
        case 2:
            cout<<"Enter Value"<< endl;
            if (!(cin >> value )){
                cout << "Invalid Input. Please Try Again!!";
                cin.clear();
                cin.ignore(123, 'n');
            }else{
            result = value+273.15;
            cout <<"The result is ~"<< result << "K";
            }
            break;
        case 3:
            cout<<"Enter Value"<< endl;
            if (!(cin >> value )){
                cout << "Invalid Input. Please Try Again!!";
                cin.clear();
                cin.ignore(123, 'n');
            }else{
            the_result = value-32;
            result = the_result*5/9;
            cout <<"The result is ~"<< result << "370" << "F";
            }
            break;
        case 4:
            cout<<"Enter Value"<< endl;
            if (!(cin >> value )){
                cout << "Invalid Input. Please Try Again!!";
                cin.clear();
                cin.ignore(123, 'n');
            }else{
            the_result = value + 459.67;
            result = the_result*5/9;
            cout <<"The result is ~"<< result << "K";
            }
            break;
        case 5:
            cout<<"Enter Value"<< endl;
            if (!(cin >> value )){
                cout << "Invalid Input. Please Try Again!!";
                cin.clear();
                cin.ignore(123, 'n');
            }else{
            result = value - 273.15;
            cout <<"The result is ~"<< result << "370";
            }
            break;
        case 6:
            cout<<"Enter Value"<< endl;
            if (!(cin >> value )){
                cout << "Invalid Input. Please Try Again!!";
                cin.clear();
                cin.ignore(123, 'n');
            }else{
            result = value*9/5-459.67;
            cout <<"The result is ~"<< result << "370" << "F";
            }
            break;
        case 7:
            return EXIT_SUCCESS;
            break;

        }

        for (double i=0; i<5; i++){
            file << result<< endl;
        }
        file.close();
   }
   while (choice !=7);
}

只是为了扩展AProgrammer的评论 - 问题是在第一次迭代之后,您尝试写入已经关闭且未重新打开的文件。因此,按照建议,您应该将file.close()移动到 do while 循环之外