C++ 文件 - 写入、读取、数组; [关闭]

Posted

技术标签:

【中文标题】C++ 文件 - 写入、读取、数组; [关闭]【英文标题】:C++ Files - Writing, Reading, Arrays; [closed] 【发布时间】:2017-01-04 18:17:09 【问题描述】:

我做错了什么?我无法理解。如果有人知道如何找出哪个 Sea Trip 的销售额最高[门票总和最大],然后我该如何显示它 - 你会注意到它应该在源代码中的位置。我一直在尝试,但我再也做不到了。在过去的 7-8 小时内,我没有在笔记本电脑前移动过。需要帮助。

#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;

struct trip

    int day; //day of Sea Trip
    int fln; //No. of Sea Trip
    char name[14]; //Ship name
    char capname[14]; // Captain Name
    char destination[14]; // Destination
    int br1; // No. pass in 1 class
    int br2; // No. pass in 2 class
    float price1; // Ticket price 1 class
    float price2; // Ticket price 2 class
    float sum; // Sum of sold tickets
;
fstream seatrips;
int add_trip(trip trip[]) //adding sea trips

    int n;
    seatrips.open("SeaTrips.txt", ios::out);
    if (seatrips.fail())
    
        cout << "Opening file had no success";
        exit(1);
    
    cout << "Enter the number of Sea Trips (max 25): ";
    do
    
        cin >> n;
     while (n < 0 || n > 25);
    for (int i = 0; i < n; i++)
    
        cout << "Enter the day of the trip (1-31) : ";
        do
            cin >> trip.day;
         while (trip.day < 1 || trip.day > 31);
        seatrips << trip.day << endl;
        cout << "Sea Trip Number : ";
        cin >> trip.fln;
        seatrips << trip.fln << endl;
        cout << "Ship name : ";
        cin >> trip.name;
        seatrips << trip.name << endl;
        cout << "Captain name : ";
        cin >> trip.capname;
        seatrips << trip.capname << endl;
        cout << "Destination : ";
        cin >> trip.destination;
        seatrips << trip.destination << endl;
        cout << "Number of passengers in 1. class : ";
        cin >> trip.br1;
        seatrips << trip.br1 << endl;
        cout << "Number of passengers in 2.class : ";
        cin >> trip.br2;
        seatrips << trip.br2 << endl;
        cout << "Ticket price 1. class : ";
        do
            cin >> trip.price1;
            seatrips << trip.price1 << endl;
         while (trip.price1 < 1);
        cout << "Ticket price 2. class : ";
        do
            cin >> trip.price2;
            seatrips << trip.price2 << endl;
         while (trip.price2 < 1);
        trip.sum = trip.br1 * trip.price1 + trip.br2 * trip.price2;
        seatrips << trip.sum;
        cout << endl << endl;


    
    seatrips.close();
    return n;

void edit(trip trip[], int n) //editing pass. info
    int t_fln; //temp sea trip number
    seatrips.open("SeaTrips.txt", ios::out);
    seatrips.seekg(0);
    if (seatrips.fail())
    
        cout << "Unable to open SeaTrips file";
        exit(1);
    
    cout << "Enter the number of the sea trip u want to edit: ";
    cin >> t_fln;
    for (int i = 0; i < n; i++)
    
        seatrips >> trip.day;
        seatrips >> trip.fln;
        seatrips >> trip.name;
        seatrips >> trip.br1;
        seatrips >> trip.br2;
        seatrips >> trip.price1;
        seatrips >> trip.price2;
        seatrips >> trip.sum;
        if (t_fln == trip.fln)
        
            cout << "Add new number of ppl in 1. class : ";
            cin >> trip.br1;
            seatrips << trip.br1 << endl;
            cout << "Add new number of ppl in 2. class : ";
            cin >> trip.br2;
            seatrips << trip.br2 << endl;

        
    
    seatrips.close();

void look(trip trip[], int n) //search by ship name, and list of all trips
    int choice2, flag = 0;
    string t_name;
    seatrips.open("SeaTrips.txt", ios::in);
    seatrips.seekg(0);
    if (seatrips.fail())
    
        cout << "Unable to open";
        exit(1);
    
    do
        cout << endl << "Choose..." << endl;
        cout << "1. Find a Sea Trip by entered Ship Name." << endl;
        cout << "2. List of all Sea Trips" << endl;
        cout << "3. Show Sea Trip with Maximum Sold Tickets" << endl;
        cout << "4. Exit" << endl;
        cout << "Choice: ";
        cin >> choice2;
        switch (choice2)
        
        case 1:cout << "Enter info about the wanted Sea Trip..." << endl;
            cout << "Ship Name: ";
            cin >> t_name;
            for (int i = 0; i < n; i++)
            
                seatrips >> trip.day;
                seatrips >> trip.fln;
                seatrips >> trip.name;
                seatrips >> trip.capname;
                seatrips >> trip.destination;
                seatrips >> trip.br1;
                seatrips >> trip.br2;
                seatrips >> trip.price1;
                seatrips >> trip.price2;
                seatrips >> trip.sum;
                if (t_name == trip.name)
                
                    cout << "Day: " << trip.day << endl;
                    cout << "Number of Sea Trip: " << trip.fln << endl;
                    cout << "Ship name : " << trip.name << endl;
                    cout << "Name of captain: " << trip.capname << endl;
                    cout<< "Destination: " << trip.destination << endl;
                    cout << "Number of passengers in 1. class: " << trip.br1 << endl;
                    cout << "Ticket price 1. class " << trip.price1 << endl;
                    cout << "Number of passengers in 2. class: " << trip.br2 << endl;
                    cout << "Ticket price 2. class " << trip.price2 << endl;
                    cout << "Sum of sold tickets: " << trip.sum << endl;
                    cout << endl;
                    flag++;
                
             break; 
        case 2:
        for (i = 0; i < n; i++)
        
                    seatrips >> trip.day;
                    seatrips >> trip.fln;
                    seatrips >> trip.name;
                    seatrips >> trip.capname;
                    seatrips >> trip.destination;
                    seatrips >> trip.br1;
                    seatrips >> trip.br2;
                    seatrips >> trip.price1;
                    seatrips >> trip.price2;
                    seatrips >> trip.sum;

                    cout << "Day: " << trip.day << endl;
                    cout << "Number of Sea Trip: " << trip.fln << endl;
                    cout << "Ship Name: " << trip.name << " Captain Name " << trip.capname << " Destination " << trip.destination << endl;
                    cout << "Number of ppl 1. class  : " << trip.br1 << endl;
                    cout << "Ticket price 1. class: " << trip.price1 << endl;
                    cout << "Number of ppl 2. class: " << trip.br2 << endl;
                    cout << "Ticket price 2. class: " << trip.price2 << endl;
                    cout << "Sum of sold tickets: "<< trip.sum << endl;  
             break; 
        case 3:

/* max sales function
*
* Trip's sum is the largest one
* And display the Sea Trip here.
*
*/

     while (choice2 != 4);
    seatrips.close();


void main()

    setlocale(LC_ALL, "Bulgarian");
    int choice1;
    int n;
    trip trip[25];
    do
    
        cout << "Menu..." << endl;
        cout << "1. Adding and saving to file Sea Trips data [deletes any existing]" << endl;
        cout << "2. Editing content" << endl;
        cout << "3. Search and look up" << endl;
        cout << "4. End" << endl;
        cout << "Enter your choice: ";
        cin >> choice1;
        switch (choice1)
        
        case 1:n = add_trip(trip); break;
        case 2: edit(trip, n); break;
        case 3:look(trip, n);
        
     while (choice1 != 4);



【问题讨论】:

请描述这段代码是如何不起作用的,并且只发布最少量的代码来重现它。请参阅:什么是minimal reproducible example “过去 7-8 小时内没有在笔记本电脑前移动。”您会惊讶于多久离开电脑去散步或做一些其他非电脑活动会有所帮助。 我强烈建议在您的struct 中重载operator&gt;&gt;。这将简化您的代码。在 *** 上已经有很多关于如何做到这一点的示例。 另外,使用std::string 而不是字符数组。 std::string 类型允许使用运算符 、==、!= 等。对于字符数组,一个常见的错误是比较两个指针,而不是它们指向的目标。见strcmp 更喜欢使用std::vector 而不是数组。 std::vector 更容易传递给函数并维护容量和大小属性。使用数组,您需要传递数组及其容量。通过引用或常量引用传递std::vector 【参考方案1】:

给定vectortrip

std::vector<trip> database;

你可以通过它迭代寻找最大的和,并维护一个迭代器到最大和的行程:

float largest_sum = -1.0f;
std::vector<trip>::const_iterator trip_with_largest_sum = database.end();
std::vector<trip>::const_iterator iter;
for (iter = database.begin(); iter != database.end(); ++iter)

  if (iter->sum > largest_sum)
  
    largest_sum = iter->sum;
    trip_with_largest_sum = iter;
  

在循环结束时,trip_with_largest_sum 将指向总和最大的行程,如果未找到任何项目,则指向database.end()

如果你坚持使用数组,同样的算法,使用索引变量而不是迭代器。

【讨论】:

以上是关于C++ 文件 - 写入、读取、数组; [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 C++ API 在 HDF5 文件中写入/读取锯齿状数组?

如何使用 C++ 在 .txt 文件中写入、读取和重写

在 C++ 中读取 .dat 二进制文件(深度图)

C++文件读取中,怎样从文件中读取一种类型的数据

java--文本文件写入

在执行期间将大型多维数组读取和写入二进制文件并返回数组?