我想使用dev c++从file.txt中打印出结构变量,但屏幕是空白的,没有错误



我想使用dev c++从file.txt中打印出结构变量,即客户名称和总价,但屏幕没有打印出结果。屏幕为空白,但没有出现错误。

这是文件的内容

File name :Fruit Order.txt
Jean;1;Apple;1;1;
Alex;2;Apple;Kiwi;1;1;1;2;
Adam;2;Kiwi;Watermelon;2;1;2;5;

这是编码

#include<iostream>
#include<fstream>
using namespace std;
struct CustInfo{
string custName;
int order;
double totalPrice;
};
struct OrderInfo{
string fruitName;
int quantity;
double price;
};
int main(){
int x = 0, y = 0;
double temp = 0;
CustInfo CI[3];
ifstream file1;
file1.open("Fruit Order.txt");
while(!file1.eof()){
for(x; x < 3; x++){
getline(file1, CI[x].custName, ';');
file1 >> CI[x].order;
file1.ignore(1, ';');
OrderInfo OI[CI[x].order];
for(y; y < CI[x].order; y++){
getline(file1, OI[y].fruitName, ';');
file1 >> OI[y].quantity;
file1.ignore(1, ';');
file1 >> OI[y].price;
file1.ignore(1, ';');
temp += (OI[y].quantity * OI[y].price);
}
CI[x].totalPrice = temp;
temp = 0;
}
}
for(x = 0; x < 3; x++){
cout << CI[x].custName << endl;
cout << CI[x].totalPrice << endl;
}
file1.close();
return 0;
}

在这里,我用//---->在他们面前

#include<iostream>
#include<fstream>
using namespace std;
struct CustInfo{
string custName;
int order;
double totalPrice;
};
struct OrderInfo{
string fruitName;
int quantity;
double price;
};
int main(){
int x = 0, y = 0;
double temp = 0;
CustInfo CI[3];
ifstream file1;
file1.open("Fruit Order.txt");
// ---> dummyString to read unwanted strings ... you can use it to read unwanted ; but I use it for n
string dummyString;
//-----> removed while ( user4581301  recommendation )
// while(!file1.eof()){
for(x; x < 3; x++){
getline(file1, CI[x].custName, ';');
file1 >> CI[x].order;
file1.ignore(1, ';');
// OrderInfo OI[CI[x].order];
//------> added this (dynamic allocation as order is known only at runtime)
OrderInfo* OI = new OrderInfo[CI[x].order];
for(y; y < CI[x].order; y++){
getline(file1, OI[y].fruitName, ';');
}
// split the two loops because all fruitNames come before other numbers. So, read all fruitNames first
for (y = 0;  y < CI[x].order; y++)
{
file1 >> OI[y].quantity;
file1.ignore(1, ';');
file1 >> OI[y].price;
file1.ignore(1, ';');
temp += (OI[y].quantity * OI[y].price);
}

CI[x].totalPrice = temp;
temp = 0;
// --------> added this | or make it local (inside for loop)
// -- this is because you access OI[y] and its size is 3 while y increases and is outside for(x...)
y= 0;
// ---> deleted OI because we don't need it
delete[] OI;
// ----> if you delete this, the custName string will have n character in it (new line)
getline(file1,dummyString,'n');
}
for(x = 0; x < 3; x++){
cout << CI[x].custName << endl;
cout << CI[x].totalPrice << endl;
}
file1.close();
return 0;
}

说实话。您的代码中存在许多问题。最大的问题是您的代码中有0行注释。这样一来,它就很难阅读了。

然后,输入数据的格式很笨拙,很难读取。基本上,数量和价格应该紧跟在产品名称之后,最好放在单独的一行。无论如何这就像是。

所以,然后你就有了动态数据,这意味着,你事先不知道你的数组必须有多大。在下面的例子中,我使用new和原始指针来表示拥有的内存。您通常不应该这样做,而是始终使用std::vector。但好吧,让我们看看。

数据模型,因此,如何以及使用结构对数据进行建模也是至关重要的。请看下面例子中我的做法。

解析输入行时的额外困难是格式化输入和非格式化输入之间的变化。必须特别注意正确处理分号。

然后,您可以在结构中添加成员函数来处理工作。对象应该被封装。

所有这些我都不会在下面的示例解决方案中做。我或多或少地坚持你的编程风格,但添加了很多评论。

请参阅:

#include <iostream>
#include <fstream>
#include <string>
struct Product{
std::string productName{};
unsigned int quantity{};
double price{};
double totalPriceProduct{};
};
struct Order {
std::string customerName{};
Product *products{};
unsigned int numberOfProducts;
double totalPriceOrder{};
};
struct OrderDatabase {
Order* orders{};
unsigned int numberOfOrders{};
double totalPrice;
};
int main() {
// Here we will store all our orders
OrderDatabase orderDatabase{};
// Open the source file
std::ifstream fileStream{ "r:\Fruit Order.txt" };
// Count, how many orders we do have in the file
std::string lineWithOrder{};
// Read all lines from file an throw content away. Count the lines
while (std::getline(fileStream, lineWithOrder) and not lineWithOrder.empty())
++orderDatabase.numberOfOrders;
// Reset stream. Clear eof bit and reset file pointer
fileStream.clear();
fileStream.seekg(0, std::ios::beg); // back to the start!
// Now dynamically allocate memory for our orders
orderDatabase.orders = new Order[orderDatabase.numberOfOrders];

char dummy; // for reading the semicolon and throw it away
// Read all data 
unsigned int orderIndex{};
// -----------------------------------------------------------------------------------------------------------
// Read all data
do {
// Read customer name
std::getline(fileStream, orderDatabase.orders[orderIndex].customerName, ';');
// How many products are in this order?
fileStream >> orderDatabase.orders[orderIndex].numberOfProducts >> dummy;
// Allocate memory for new products
orderDatabase.orders[orderIndex].products = new Product[orderDatabase.orders[orderIndex].numberOfProducts];
// Read product names
for (unsigned int productIndex = 0; productIndex < orderDatabase.orders[orderIndex].numberOfProducts; ++productIndex) {
std::getline(fileStream, orderDatabase.orders[orderIndex].products[productIndex].productName, ';');
}
// Read product quantity and price
for (unsigned int productIndex = 0; productIndex < orderDatabase.orders[orderIndex].numberOfProducts; ++productIndex) {
fileStream >> orderDatabase.orders[orderIndex].products[productIndex].quantity >> dummy;
fileStream >> orderDatabase.orders[orderIndex].products[productIndex].price >> dummy;
// Calculate total price for product
orderDatabase.orders[orderIndex].products[productIndex].totalPriceProduct = orderDatabase.orders[orderIndex].products[productIndex].quantity * orderDatabase.orders[orderIndex].products[productIndex].price;
// Accumulate prices in oder
orderDatabase.orders[orderIndex].totalPriceOrder += orderDatabase.orders[orderIndex].products[productIndex].totalPriceProduct;
// Accumulate overall total price
orderDatabase.totalPrice += orderDatabase.orders[orderIndex].products[productIndex].totalPriceProduct;;
}
// Read next line
++orderIndex;

} while (fileStream and orderIndex < orderDatabase.numberOfOrders);
// -------------------------------------------------------------------------------------------------------
// Show result to user
std::cout << "nnThere are " << orderDatabase.numberOfOrders << " orders in the database. Details:n";
for (orderIndex = 0; orderIndex < orderDatabase.numberOfOrders; ++orderIndex) {
// Show one order
std::cout << "nOrder number " << orderIndex + 1 << " for Customer: " << orderDatabase.orders[orderIndex].customerName << " with " << orderDatabase.orders[orderIndex].numberOfProducts << " productsn";
// Show product in this order
for (unsigned int productIndex = 0; productIndex < orderDatabase.orders[orderIndex].numberOfProducts; ++productIndex) {
std::cout << "Product: " << orderDatabase.orders[orderIndex].products[productIndex].productName << " t Quantity: " << orderDatabase.orders[orderIndex].products[productIndex].quantity << " tPrice: " <<
orderDatabase.orders[orderIndex].products[productIndex].price << "   t Total -->  " << orderDatabase.orders[orderIndex].products[productIndex].totalPriceProduct << 'n';
}
// Show total order price
std::cout << "nTotal price of this order: " << orderDatabase.orders[orderIndex].totalPriceOrder << 'n';
}
// Show overall total
std::cout << "nnTotal price overall: " << orderDatabase.totalPrice << 'n';
// Free memory
for (orderIndex = 0; orderIndex < orderDatabase.numberOfOrders; ++orderIndex) {
delete[] orderDatabase.orders[orderIndex].products;
}
delete[] orderDatabase.orders;
return 0;
}

如前所述。我不会那样做,而是使用更先进的C++技术。

但请一开始就试试这个。

相关内容

  • 没有找到相关文章