在C ++中的文本文件中保存多个输出
Saving multiple output in a text file in c++
还在努力学习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 <<"\n\nChoose 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"
"\n\nChoice:\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 << "0";
}
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 << "0" << "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 << "0";
}
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 << "0" << "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 循环之外
还在努力学习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 <<"\n\nChoose 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"
"\n\nChoice:\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 << "0";
}
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 << "0" << "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 << "0";
}
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 << "0" << "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 循环之外