gpt4 book ai didi

c++ - 命名一个文本文件并让它显示使用函数的平均值

转载 作者:太空宇宙 更新时间:2023-11-04 14:29:01 24 4
gpt4 key购买 nike

我正在开发一个名为 Online GDB 的 C++ 编译器。我需要编写一个程序,要求用户输入文件名,然后读取该数据以计算平均值,然后显示数据。

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
// functions
string getFileInfo();
double calculateMean (string fileName);

// main
int main ()
{
string filename = getFileInfo(); // getting file name
double mean = calculateMean( filename); // avg
cout << "The mean was" << endl;
return 0;
}
// function for getting file
string getFileInfo() {
string filename;
ifstream inputFile; // varibles
// asking user for the name
cout << "Please enter a file name to read from: ";
cin >> filename;

inputFile.open(filename);
// opening name and checking if its good.
if (inputFile)
{
inputFile.close();
}
// if the file is not good
else
{
cout << "Error: Please enter a file name to read from: ";
cin >> filename;
}
return 0;
}

// funtion for mean
double calculateMean(string fileName)
{
ifstream infile;
float num=0;
float total = 0.0;
int count = 0;
infile.open(fileName);
// While infile successfully extracted numbers from the stream
while(infile >> num) {
total += num;
++count;
}
// don't need the file anymore, close it
infile.close();

// give the average
return total/count;
}

我找到平均值的部分工作正常,但我在文件命名方面遇到了问题。

它运行并询问我的名字,当我输入时它直接进入错误并再次输入然后它显示这个:

terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
Aborted.

我以前从未见过这个,我已经尝试创建一个文件,如果我不这样做,那么什么也不会显示。

最佳答案

此代码完全有效

    #include <iostream>
#include <fstream>
#include <stdio.h>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
//int calculatorMin(string fileName);
//void displayInfo(double mean, int main);
string getFileInfo();
double calculateMean (string fileName);

// main
int main ()
{
string filename = getFileInfo(); // getting file name
double mean = calculateMean( filename); // avg
return 0;
}
// function for getting file
string getFileInfo() {
string filename;
ifstream inputFile;

cout << "Please enter a file name to read from: ";
cin >> filename;

inputFile.open(filename);

if (inputFile)
{
inputFile.close();
}

else
{
cout << "Error: Please enter a file name to read from: ";
cin >> filename;
}
//if it opens then name is good and close file then return name
//if it didn't open, ask for a new name.
return filename;
}

// funtion for mean
double calculateMean(string fileName)
{
ifstream infile;
float num=0;
float total = 0.0;
int count = 0;
infile.open(fileName);
// While infile successfully extracted numbers from the stream
while(infile >> num) {
total += num;
++count;
}
// don't need the file anymore, close it
infile.close();
cout << "The mean was " << total/count << endl;
// give the average
return total/count;

}

关于c++ - 命名一个文本文件并让它显示使用函数的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54858657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com