c++ - 数组和字符串 C++ 的各种数据验证文件 I/O 实践
问题描述
#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
#include <array>
#include <sstream>
using namespace std;
void NumberSummaryList(string);
void Validation(string);
int main()
{
string file = "";
cout << "Hello, what is the name of the number file that you would like to process, please don't forget to include .txt " << "-For Example- data.txt" << endl;
cin >> file;
try
{
ifstream DataIn;
DataIn.open(file);
if (DataIn.is_open() == false)
{
throw 20;
}
}
catch (int e)
{
cout << "An error has occurred with opening the file." << endl;
}
NumberSummaryList(file);
Validation(file);
}
void NumberSummaryList(string file)
{
ifstream DataIn;
DataIn.open(file);
const int NumbersArray = 100;
int numbers[NumbersArray];
int count;
int x;
int min = 0;
int max = 0;
int temp = 0;
float sum = 0;
float avg = 0;
count = 0;
while (!DataIn.eof())
{
DataIn >> numbers[count];
count++;
}
x = count;
DataIn.close();
for (int count = 0; count < x; count++)
{
cout << numbers[count] << "\n";
}
min = numbers[0];
max = numbers[0];
for (int count = 0; count < x; count++)
{
temp = numbers[count];
if (temp < min)
min = temp;
if (temp > max)
max = temp;
}
for (int count = 0; count < x; count++)
{
sum += numbers[count];
}
avg = sum / x;
ofstream OutData;
OutData.open("listsummary.txt");
cout << "The total of numbers read in the data file is " << x << endl;
cout << "The average of all numbers in the data file is " << avg << endl;
cout << "The max value of the data file is " << max << endl;
cout << "The min value of the data file is " << min << endl;
OutData << "The total of numbers read in the data file is " << x << endl;
OutData << "The average of all numbers in the data file is " << avg << endl;
OutData << "The max value of the data file is " << max << endl;
OutData << "The min value of the data file is " << min << endl;
OutData.close();
}
void Validation(string file)
{
ifstream DataInStrings;
DataInStrings.open(file);
int count = 0;
string stringarray[100];
int intarray[100];
int y = 0;
for (int count = 0; count < 100; count++)
{
getline(DataInStrings, stringarray[count]);
if (stringarray[count].length() > 5)
{
cout << "Error Found! The element in position " << count + 1 << " " << stringarray[count] << " is too long. Error Written." << endl;
}
else
{
}
}
for (int count = 0; count < 100; count++)
{
string CopyofArray;
CopyofArray = stringarray[count];
int intcopy = stoi(CopyofArray);
if (intcopy < 0)
{
cout << "Error Found! The element in position " << count + 1 << " " << stringarray[count] << " is negative. Error Written." << endl;
}
else
{
}
}
for (int count = 0; count < 100; count++)
{
cout << stringarray[count] << "\n";
}
string ArrayCopy;
ArrayCopy = stringarray[count];
//if (isalpha(ArrayCopy))
//{
//}
//else
//{
// cout << "Alphabetical characters detected in the file, error written." << endl;
//}
DataInStrings.close();
}
大家好。这是一个我坚持并努力解决的课程项目。目标是验证我加载的文件中的一些数据,这些数据应该是数字 1-100。如果文件包含任何字母数字、符号(如?),我的老师要求我写出错误。或 /,如果元素的长度大于 5,并且数字为负数。
该程序还应该显示文件 1-100 的内容,查找读取的最大、最小、平均值和总数。
我已成功读取文件,找到读取的最大、最小、平均值和总数。如果数字太长或为负数,我也已成功对其进行编程以写出错误。
但是,每当文件包含字母数字或符号时,我的麻烦就来了。如果我尝试测试在数据文件中放置字母数字或符号,则该文件根本不会加载并且程序将无法正常运行。
我尝试重写程序以首先使用 getline 并读取字符串,然后在需要时进行转换。但它现在有一个问题,即在找到字母数字后程序不会计算的地方。
我在这里以正确的方式寻求建议或某种方向。我的老师让我们先编写程序的一部分,然后添加数据验证部分,这让我很生气,因为它在不同的地方把整个事情搞混了。
我期待任何...请,谢谢大家!
解决方案
推荐阅读
- javascript - 组件异常:重新渲染过多
- fastapi - 如何在 python dot env 文件中定义列表?
- javascript - 使用循环显示卡引导中的每个 json 数据
- python - 使用 pandas 在同一行中获得 3 年平均值
- database - 跨环境管理多个数据库 ID
- c# - 中断模式下 C# 应用程序中的 XAMLParseException
- sql - 查找 min(records) 和 max(records)
- parallel-processing - 雪花复制到并行 Parquet 文件加载
- c++ - QuickFix C++ 和 SSL:使用 sTunnel 连接到 LMAX 的问题
- flutter - 错误:运行 ios 构建时找不到“Flutter/Flutter.h”文件