从文件中读取下一行
Reading next line from file
,因此该程序应该从文件中读取,该文件上有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 entryn";
}
//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
也许尝试使用这样的循环:
// Create an empty string
std::string line;
// Start a loop that will get a line from the file and input it in our string
// this loop will keep going until the getline fails, i.e. end of file.
while (std::getline(fileName, line))
{
CODE
}
您可以放一个段循环,该循环将运行,直到程序看到文件末尾
while(!EOF)
{your code here}
并且总是不要忘记关闭您打开的文件
相关文章:
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 如何使代码打印文本文件中的第一行?
- 如何格式化我的文本文件以使其不会一遍又一遍地重复同一行?
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- 将字符串存储到文件下一行的变量中
- 为什么这个.c文件只有一行"../xx/xx.c"?
- 比较两个没有一行共同点的大文件
- 如何在c ++中以一行(水平)打印两个文件的数据?如给定的.这两种形式来自两个不同的文本文件
- 到达分隔符时跳到文本文件的下一行
- 从文本文件中读取一行并将其存储在 C++ 中的 2D 矢量中
- 从文本文件中随机选取一行
- 在获取从文件到矢量的每一行之后,数字将被更改或销毁
- 从文件中的一行读取特定数据-C++
- 从文本文件中的每一行读取数字,并计算数字重复的次数
- 为什么这一行不输出我的文本文件?
- 从文件读取并在一行中写入 cout
- 如何有效地读取C 中的最后一行文件
- 将一行文件读入对象中的两个变量