【发布时间】:2022-10-17 15:28:22
【问题描述】:
该程序以列格式打印报告,该列的格式与示例输出中显示的完全相同。
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
char user_gender, user_smoker;
string user_eyecolor;
int user_minAge, user_maxAge, user_minHeight, user_maxHeight;
cout << "What is the gender of your ideal match(M, F, N) ? ";
cin >> user_gender;
cout << "What is the minimum age? ";
cin >> user_minAge;
cout << "What is the maximum age? ";
cin >> user_maxAge;
cout << "What is the minimum height (in inches)? ";
cin >> user_minHeight;
cout << "What is the maximum height (in inches)? ";
cin >> user_maxHeight;
cout << "Smoker (Y/N)? ";
cin >> user_smoker;
cout << "What is the eyecolor (Blue, Green, Grey, Brown)? ";
cin >> user_eyecolor;
cout << endl << endl;
//Variables to check against the conditions
int countGender = 0;
int partialMatch = 0;
int fullMatch = 0;
cout << endl << left << setw(1) << " Name" << fixed << right << setw(22) << "Age" << fixed << right << setw(12) << "Height" << fixed << right << setw(12) << "Smoker" << fixed << right << setw(15) << "Eye Color" << fixed << right << setw(22) << "Phone" << endl;
cout << "-----------------------------------------------------------------------------------------------------------------" << endl;
//Now read the file data.
ifstream fin("matches.txt");
if (fin.is_open())
{
while (!fin.eof())
{
string firstname, lastname, eyecolor, phoneno;
char gender, smoker;
int age, height;
fin >> firstname >> lastname >> gender >> age >> height >> smoker >> eyecolor >> phoneno;
if (gender == user_gender)
{
countGender++;
//Now check to see if the age and height are between the maximum and minum preferences.
if ((age >= user_minAge && age <= user_maxAge) && (height >= user_minHeight && height <= user_maxHeight))
{
//Then check to see if the smoking preference and eye color are also a match.
if (user_smoker == smoker && user_eyecolor == eyecolor)
{
fullMatch++;
cout << "* " << firstname << " " << lastname << setw(25) << age << setw(11) << height << setw(11) << smoker << setw(11) << eyecolor << setw(11) << phoneno << endl;
}
else if (eyecolor == user_eyecolor)
{
partialMatch++;
cout << " " << firstname << " " << lastname << setw(24) << age << setw(11) << height << setw(11) << smoker << setw(11) << eyecolor<< setw(11) << phoneno << endl;
}
}
}
}
cout << "-----------------------------------------------------------------------------" << endl;
cout << "There were " << fullMatch << " matches and " << partialMatch << " partial matches out of " << countGender << " records." << endl;
cout << "-----------------------------------------------------------------------------" << endl;
fin.close();
}
else {
cout << "File did not open";
}
return 0;
}
****该程序运行得非常好,但我没有将输出打印在上面示例输出中所示格式的列中。 **** 编写一个程序,打开文件并逐条读取记录。该程序将跳过性别偏好不匹配的任何记录。在符合性别偏好的那些记录中,检查年龄和身高是否在最大和最小偏好之间。然后检查吸烟偏好和眼睛颜色是否也匹配。如果至少 3 个剩余字段匹配,则认为记录部分匹配,并将其打印在报告中。如果所有 4 个剩余字段都匹配,则该记录是完全匹配的,并将其打印在报告中,并在其旁边带有一个星号。在程序结束时,关闭文件并报告指定性别的总记录数,部分匹配的记录数,完美匹配的记录数。
Charlie Bradbury F 42 65 N Green 555-867-5309
Bobby Singer M 70 69 Y Brown 555-867-5309
Dean Winchester M 43 72 N Brown 555-867-5309
Sam Winchester M 39 75 N Brown 555-867-5309
Bela Talbot F 39 69 Y Blue 555-867-5309
James Novak M 46 71 Y Blue 555-867-5309
【问题讨论】:
-
发布您的输入文件的几行(作为文本,上面一行带有
```none,下面一行带有```,以格式化为固定文本)查看您的输入将使这里的每个人都可以确定您的代码是否正在执行您的操作希望是。确保数据文件不是多字节字符编码(例如,带有 BOM 的 UTF-16 是 Windows 记事本的默认值 - 会导致问题)
标签: c++