[英]Reading from a text file and storing the data into a table has produced a corrupted table using c++
我需要從文本文件中讀取數據並將其存儲在表中。 我還需要使用數據來計算每個員工的工資(測試文件列出了員工:id:rate:工作小時數)需要顯示計算工資,並且表格本身需要按從高到低的順序列出員工最低工資,以及顯示工資總額。 下面是文本文件:(注意兩行之間沒有空格,為了清楚起見,在這篇文章中添加了空格)
Mike:Jones:567:25.75:67
Sue:Smith:45:30.25:82
Ann:Barber:2:15.45:39
Billy:Simpson:1234:10.15:65
Barabara:Stone:75:45.33:22
Alan:Colllins:17:12.75:73
Cindy:Davis:210:13.67:45
Eilein:Ferguson:62:53.36:17
Gordon:Howard:981:9.89:31
Bob:Jones:295:14.73:43
當我嘗試這樣做時,表出來相當損壞。 output 如下:
名字 | 姓氏 | 員工證 | 每小時費率($) | 小時 | 工資($)
| |-9.25596e+61 | -9.25596e+61 |-9.25596e+61 | -9.25596e+61
| |-9.25596e+61 | -9.25596e+61 |-9.25596e+61 | -9.25596e+61
| |-9.25596e+61 | -9.25596e+61 |-9.25596e+61 | -9.25596e+61
| |-9.25596e+61 | -9.25596e+61 |-9.25596e+61 | -9.25596e+61
| |-9.25596e+61 | -9.25596e+61 |-9.25596e+61 | -9.25596e+61
| |-9.25596e+61 | -9.25596e+61 |-9.25596e+61 | -9.25596e+61
| |-9.25596e+61 | -9.25596e+61 |-9.25596e+61 | -9.25596e+61
| |-9.25596e+61 | -9.25596e+61 |-9.25596e+61 | -9.25596e+61
| |-9.25596e+61 | -9.25596e+61 |-9.25596e+61 | -9.25596e+61
| |-9.25596e+61 | -9.25596e+61 |-9.25596e+61 | -9.25596e+61
每周總工資為:
以下是我用來嘗試完成此操作的代碼。
include <cmath>
include <string>
include <stream>
include <iostream>
include <stream>
include <iomanip>
using namespace std;
void swap(double* xp, double* yp)
{
double temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(double arr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n - 1; i++)
{
min_idx = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(&arr[min_idx], &arr[I]);
}
}
int main()
{
ifstream myFile("Assignment4.txt");
if (!myFile.is_open())
{
cout << "File Failed to Open!" << endl;
return 0;
}
const int n = 100;
string firstname[n], lastname[n];
double id[n], hours[n], rate[n], wage[n], totalwage;
unsigned int i = 0;
string myString;
string line;
cout << "First Name |" << " Last Name |" << " Employee ID |" << " Hourly Rate($) |" << " Hours |" <<
" Wage($)" << endl;
while (getline(myFile, line))
{
stringstream ss(line);
getline(ss, firstname[i], ':');
getline(ss, lastname[i], ':');
getline(ss, myString, ':');
id[i] = stoi(myString);
getline(ss, myString, ':');
rate[i] = stoi(myString);
getline(ss, myString, '\n');
hours[i] = stoi(myString);
if (hours[i] <= 40)
wage[i] = rate[i] * hours[i];
else
wage[i] = rate[i] * 40 + ((hours[i] - 40) * rate[i] * 1.5);
totalwage += wage[i];
i = i + 1;
selectionSort(wage, i);
cout << setw(10) << firstname[i] << " |" << setw(10)
<< lastname[i] << " |" << setw(12)
<< id[i] << " |" << setw(15)
<< rate[i] << " |" << setw(6)
<< hours[i] << " | " << wage[i] << endl;
}
cout << "The total weekly wage is: ";
myFile.close();
}
如果有人知道它為什么以這種方式輸出數據以及如何修復它,請提供幫助。 我將 c++ 與 Visual Studio 2019 一起使用。
經驗法則:如果您使用並行 arrays,您可能需要轉換為結構向量。
struct Record
{
std::string first_name;
std::string last_name;
double id; // ??? Is the ID really floating point?
double rate;
double hours;
double wage;
friend std::istream& operator>>(std::istream& input, Record& r);
};
提取運算符的重載不是必需的,但可以簡化代碼。
std::istream& operator>>(std::istream& input, Record& r)
{
char separator;
std::getline(input, r.first_name, ':');
std::getline(input, r.last_name, ':');
input >> r.id; input >> separator;
input >> r.rate; input >> separator;
input >> r.hours; input >> separator;
input >> r.wage;
input.ignore(100000, '\n'); // Ignore any remaining characters until end of line.
return input;
}
您的輸入代碼可能如下所示:
std::vector<Record> database;
Record r;
while (myFile >> r)
{
database.push_back(r);
}
對數組進行排序需要重載operator<
:
struct Record
{
//...
bool operator<(const Record& r)
{
if (last_name == r.last_name)
{
return first_name < r.first_name;
}
return last_name < r.last_name;
}
};
排序操作將簡化為:
std::sort(database.begin(), database.end());
另一個經驗法則:如果您認為您的程序太長或太大,通常可以簡化。
這並沒有損壞它實際上系統試圖縮短數字文件它可能你可以用字符串或字符逐個鍵入每個數字。 但我不建議這樣做。
但是嘗試首先為單獨的文本文件設置名稱,也就是像圖片一樣的數據包,這樣你就可以像拼圖一樣把它們放在一起
另一件可以幫助的事情是標記您的項目,以便更容易調試。
如果您早上醒來看它而忘記了自己在做什么,您的程序會看起來很復雜。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.