2017-01-04 1 views
0

내가 뭘 잘못하고 있니? 나는 이해할 수 없다. 그리고 누가 가장 많은 양의 Sea Trip에 티켓의 합계가 가장 큰지 찾아내는 방법을 알고 있다면 어떻게 표시 할 수 있습니까? 소스 코드에서 어디에 있어야하는지 알 수 있습니다. 나는 노력하고 노력했다. 그리고 나는 이제 더 이상 할 수 없다. 지난 7-8 시간 동안 랩탑 앞에서 움직이지 않았다. 도움이 필요하다.C++ 파일 - 쓰기, 읽기, 배열;

#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); 


} 
+4

이 코드가 작동하지 않는 방식을 설명하고이를 재현 할 수있는 최소한의 코드 만 게시하십시오. 다음을 참조하십시오 : [mcve] – NathanOliver

+3

은 "지난 7-8 시간 동안 랩톱에서 이동하지 않았습니다." 얼마나 자주 컴퓨터를 떠나 산책하러 가는지 또는 컴퓨터가 아닌 다른 활동을하는 것이 놀랄 것입니다. – molbdnilo

+1

'struct'에서'operator >>'를 오버로드하는 것이 좋습니다. 이렇게하면 코드가 단순 해집니다. 이미 StackOverflow에서이를 수행하는 방법에 대한 많은 예제가 있습니다. –

답변

0

감안할 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() 항목을 찾을 수없는 경우.

동일한 알고리즘을 사용하려는 경우 반복자 대신 색인 변수를 사용하십시오.