如何保持从文本文件获取数据时的十进制值?



我有一个包含如下信息的文本文件(有127行,为了保持简短,我将提供几行):

Gibraltar 133.25
Israel 104.81
Seychelles 85.89
United-Arab-Emirates 63.95
Falkland-Islands 49.73

我的程序确实可以打印接种疫苗最多的国家名称和编号,但是它只是打印一个没有小数的整数。对于"最低",这尤其有问题,因为有多个国家的数字在0到1之间,它只打印列表中的第一个0,所以不是真正的最小值,下面是我的代码:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void file(string country[], double vaccinations[]);
void highest(string country[], double vaccinations[]);
void lowest(string country[], double vaccinations[]);
void average (string country[], double vaccinations[]);
void welcomeMessage();
int main() {
//declare variables and arrays, set vaccinations and countries arrays to 127 for the number of countries, amd subsequent vaccination data
string countries[127];
double vaccinations[127];
file(countries, vaccinations);

//call wecome message function
welcomeMessage();
//calls function to count highest vaccination rate and country names
highest(countries, vaccinations);
cout << endl;
//calls function to count lowest vaccination rate and country name
lowest(countries, vaccinations);
//calls function to print the average vaccination rate
//average(countries, vaccinations);
}
void welcomeMessage() {
cout << "Welcome to the vaccine data analysis program!" << endl;
cout << endl;
cout << "This program will print which country currently has admistered the most COVID-19 vaccine doses per 100 people" << endl;
cout << endl;
cout << "The program will also print which country currently has administered the least, per 100 people, as well as the average dose administration per 100 people, out of 127 countries on the list." << endl;
cout << endl;
}
void file(string country[], double vaccinations[]) {
int index = 0;
ifstream inFile;
inFile.open("vaccinations.txt");
if (!inFile.is_open()) {
cout << "Unable to open file." << endl;
} else {
while (index < 127) {
//add code to isolate country string
inFile >> country[index];
//add code to isolate vaccination percentage
inFile >> vaccinations[index];
index++;
}
}
}
void highest(string country[], double vaccinations[]) {
string highestCountry = country[0];
int highestTotal = vaccinations[0];
for (int i = 1; i < 127; i++) {
if (vaccinations[i] > highestTotal) {
highestTotal = vaccinations[i];
highestCountry = country[i];
}
}
cout << highestCountry << " has the highest number of vaccinations at " << fixed << setprecision(100) << highestTotal << " per 100 people." << endl;
}

void lowest(string country[], double vaccinations[]) {
string lowestCountry = country[0];
int lowestTotal = vaccinations[0];
for (int i = 1; i < 127; i++) {
if (vaccinations[i] < lowestTotal) {
lowestTotal = vaccinations[i];
lowestCountry = country[i];
}
}
cout << lowestCountry << " has the lowest number of vaccinations at " << fixed << setprecision(100)<< lowestTotal << " per 100 people." << endl;
}

您声明

int highestTotal = vaccinations[0];

因此,当您将浮点值复制到中时,它会减少小数。

highestTotallowestTotal声明为双精度体应该有帮助:

double highestTotal = vaccinations[0];

double lowestTotal = vaccinations[0];

编辑:正如@Casey建议的,我们可以稍微重写一下(如果你愿意,你可以添加欢迎信息和文件正确性检查);

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
struct Country {
string name;
double rate;
};
istream& operator>>(std::istream& in, Country& country) {
return in >> country.name >> country.rate;
}
int main()
{
ifstream inFile;
inFile.open("vaccinations.txt");
auto comparator = [](const Country& x, const Country& y) { return x.rate < y.rate; };
auto [min, max] = minmax_element(istream_iterator<Country>{inFile}, istream_iterator<Country>{}, comparator);
string highestCountry = max->name;
double highestTotal = max->rate;
cout << highestCountry << " has the highest number of vaccinations at " << highestTotal << " per 100 people." << endl << endl;
string lowestCountry = min->name;
double lowestTotal = min->rate;
cout << lowestCountry << " has the lowest number of vaccinations at " << lowestTotal << " per 100 people." << endl;
}

最新更新