所以程序應該從文件中讀取,文件中有4-5行信息。我可以讀入程序的第一行,並通過各種算法進行處理,但我不確定如何將下一行循環並處理,並一次又一次地處理,直到文件結束。非常感謝您的閱讀和所有的輸入是讚賞。這是整個程序,從文件底部讀入文本。從文件中讀取下一行
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inputFile;
ofstream invoicefile;
string name, author, isbn, customerid, filename, fictionoutput, genreoutput;
char booktype, genre;
bool fictionvalue;
double initial_total, tax_price, subtotal, totalprice, price;
int fee, quantity;
const double tax(0.07);
cout << "Enter name of file.\n";
cin >> filename;
cout << "Opening file \n";
inputFile.open(filename);
if (inputFile.is_open()) {
inputFile >> customerid >> name >> author >> isbn >> price >> quantity >> booktype >> genre;
//QUANTITY FEE CODING BLOCK
if (quantity > 50) {
fee = 50;
}
else if (quantity >= 15 && quantity <= 19) {
fee = 40;
}
else if (quantity >= 10 && quantity <= 14) {
fee = 30;
}
else if (quantity >= 5 && quantity <= 10) {
fee = 20;
}
else if (quantity < 5) {
fee = 10;
}
//BOOKTYPE CODING BLOCK (FICTION or NON-F)
if (booktype == 'F') {
fictionvalue = true;
}
else if (booktype == 'N') {
fictionvalue = false;
}
else {
cout << "INVALID";
}
//BOOKTYPE INTO STRING OUTPUT
if (fictionvalue = true) {
fictionoutput = "Fiction";
}
else if (fictionvalue = false) {
fictionoutput = "Non-Fiction";
}
//GENRE TYPE INTO STRING OUTPUT
if (genre == 'R') {
genreoutput = "Romance";
}
else if (genre == 'D') {
genreoutput = "Drama";
}
else if (genre = 'M') {
genreoutput = 'M';
}
else {
cout << "Invalid entry\n";
}
//NO FEE EXCEPTION
if (booktype == 'N' && genre == 'R') {
fee = 0;
}
//CALCULATION OF PRICE + TAX CODING BLOCK
initial_total = (price*quantity);
tax_price = (initial_total * tax);
subtotal = (initial_total + tax_price);
totalprice = (subtotal + fee);
//OUTPUT TO FILE/CONSOLE CODING BLOCK
cout << "-----------------------------------------" << endl;
cout << "Order Invoice" << endl;
cout << "Customer ID: " << customerid << endl;
cout << name << " " << author << " " << fictionoutput << " " << genreoutput << " " << quantity << "@" << price << "Subtotal: " << endl; //add subtotal price
//cout << "Total book sales: " <<
cout << "Tax: " << tax_price << endl;
cout << "Subtotal: " << subtotal << endl;
cout << "Fee: " << fee << endl;
cout << "Total Price: " << totalprice << endl;
cout << "-----------------------------------------" << endl;
system("pause");
}
}
文字示例
1234 Dog_Strategy Henry_Moreno 3-598-21500-2 12.99 5 N M
6789 Companion_Kicked_Me_Out Lorraine_Johnson 3-598-21599-1 24.99 3 F R
3444 Mime_On_My Journey Kristy_Wahl 3-699-21500-8 6.75 10 N D
4455 Damaged_By_The_Joke Henry_Christopher 3-598-21500-2 12.99 4 N R
歡迎使用堆棧溢出。標題中不需要重複標籤信息。這也沒有必要喊HELP,因爲如果你不需要幫助,你就不會在這裏發帖。相反,花費精力寫出更好的標題。更好的是,在這裏搜索一下現有的帖子,因爲有很多關於在C++中逐行閱讀文件的文章。 –