The program doesn't work, but it's kind of right.



  • #include <iostream>
    #include <cstring>
    #include <cctype>
    using namespace std;
    struct Train
    {
        int number;
        char *Location[100];
        char *departure[10];
    };
    Train* AddStruct(Train* Obj, const int number);
    void input(Train* Obj, const int number);
    void show(const Train* Obj, const int number);
    int main()
    {
        Train *City;
        int n;
        int count = 0;
        cin >> n;
        for (int i = 0;i < n;i++)
            {
                input(City,count);
                count++;
            }
        show(City, count);
        delete[] City;
        return 0;
    }
    Train* AddStruct(Train* Obj, const int number)
    {
        if (number == 0)
        {    
            Obj = new Train[number + 1];
        }
        else
        {
            Train* tempObj = new Train[number + 1];
            for (int i = 0; i < number; i++)
            {
                tempObj[i] = Obj[i];
            }
            delete[] Obj;
            Obj = tempObj;
        }
        return Obj;
    }
    void input(Train* Obj, const int number)
    {
        cin >> Obj[number].number;
        cin >> *Obj[number].Location;
        cin >> *Obj[number].departure;
    }
    void show(const Train* Obj, const int number)
    {
        for (int i = 0; i < number; i++)
        {
            cout << Obj[i].number << '\t' << Obj[i].Location << '\t' <<   Obj[i].departure << endl;
        }
    }
    

    I can't figure out why the game breaks. I'll be happy with any help. I'm a newcomer.



  • There are several problems in the programme.

    First of all, you didn't provide memory for a set of objects like this. Train♪ Index City uninitiated and undetermined.

    Train *City;
    ^^^^^^^^^^^^
    

    Therefore, using this indicator in this function

    void input(Train* Obj, const int number)
    {
        cin >> Obj[number].number;
        cin >> *Obj[number].Location;
        cin >> *Obj[number].departure;
    }
    

    leads to an uncertain programme behaviour.

    But even if you had identified the mass and the index had been initiated by the address of the first element of the mass, the programme is not correct, as these proposals set out the above function.

        cin >> *Obj[number].Location;
        cin >> *Obj[number].departure;
    

    You're also trying to use uninitiated indexes. Obj[number].Location and Obj[number].departure

    Announcement of these structures as presented

    struct Train
    {
        int number;
        char *Location[100];
        ^^^^^^^^^^^^^^^^^^^^
        char *departure[10];
        ^^^^^^^^^^^^^^^^^^^^
    };
    

    doesn't make sense. You've declared masses of type-indicators. char * Instead of declaring masses from objects of type char♪ I think you meant the next announcement.

    struct Train
    {
        int number;
        char Location[100];
        ^^^^^^^^^^^^^^^^^^^^
        char departure[10];
        ^^^^^^^^^^^^^^^^^^^^
    };
    

    As in the function AddStruct You're removing the mass that's addressed to the first parameter,

    Train* AddStruct(Train* Obj, const int number)
    {
        if (number == 0)
        {    
            Obj = new Train[number + 1];
        }
        else
        {
            Train* tempObj = new Train[number + 1];
            for (int i = 0; i < number; i++)
            {
                tempObj[i] = Obj[i];
            }
            delete[] Obj;
            Obj = tempObj;
        }
        return Obj;
    }
    

    If it is called, it should not be forgotten to assign a returnable value to the index, which has been transferred as an argument. For example,

    City = AddStruct( City, count );
    

    It's not exactly a clear intuitive interface. The user of this function may forget to assign the result of the work to the same index that was used as an argument.

    The second point to the function is that two cases should not be separated into functions where the parameter is number 0 or 0. If the parameter number 0, the first parameter shall be equal to 0. nullptrand therefore the operator may also apply to the indicator delete []

    Based on that, I would have declared this function as follows.

    int AddStruct( Train * &Obj, int number )
    {
        Train* tempObj = new Train[number + 1];
    
    for ( int i = 0; i &lt; number; i++ )
    {
        tempObj[i] = Obj[i];
    }
    
    delete [] Obj;
    
    Obj = tempObj;
    
    return number + 1;
    

    }

    EDIT: In the light of your comments, the programme may look as follows:

    #include <iostream>

    struct Train
    {
    unsigned int number;
    char *location;
    char *departure;
    };

    const size_t LOCATION_LENGTH = 100;
    const size_t DEPARTURE_LENGTH = 10;

    size_t AddStruct( Train * &trains, size_t n );
    void Input( Train *trains, size_t i );
    void Show( const Train *trians, size_t n );
    void Free( Train * &trains, size_t n );

    int main()
    {
    Train *city = nullptr;

    std::cout &lt;&lt; "Enter number of trains: ";
    size_t n = 0;
    
    std::cin &gt;&gt; n;
    
    std::cout &lt;&lt; std::endl;
    
    for ( size_t i = 0; i &lt; n; i++ )
    {
        AddStruct( city, i );
        Input( city, i );
    }
    
    Show( city, n );
    
    Free( city, n );
    
    return 0;
    

    }

    size_t AddStruct( Train * &trains, size_t n )
    {
    Train *tmp = new Train n + 1 ;

    for ( size_t i = 0; i &lt; n; i++ ) tmp[i] = trains[i];
    
    delete [] trains;
    
    trains = tmp;
    
    return n + 1;
    

    }

    void Input( Train *trains, size_t i )
    {
    std::cout << "Enter number of train #" << i + 1 << ": ";

    std::cin &gt;&gt; trains[i].number;
    
    std::cout &lt;&lt; "Enter location for train #" &lt;&lt; i + 1 &lt;&lt; ": ";
    
    trains[i].location = new char[LOCATION_LENGTH];
    
    std::cin.ignore( 1, '\n' );
    std::cin.getline( trains[i].location, LOCATION_LENGTH );
    
    std::cout &lt;&lt; "Enter departure for train #" &lt;&lt; i + 1 &lt;&lt; ": ";
    
    trains[i].departure = new char[DEPARTURE_LENGTH];
    
    std::cin.getline( trains[i].departure, DEPARTURE_LENGTH );
    

    }

    void Show( const Train *trains, size_t n )
    {
    for ( size_t i = 0; i <n; i++ )
    {
    std::cout << "Number: " << trains[i].number << '\n';
    std::cout << "Location: " << trains[i].location << '\n';
    std::cout << "Departure: " << trains[i].departure << '\n';

        std::cout &lt;&lt; std::endl;
    }
    

    }

    void Free( Train * &trains, size_t n )
    {
    for ( size_t i = 0; i < n; i++ )
    {
    delete [] trains[i].location;
    delete [] trains[i].departure;
    }

    delete [] trains;
    
    trains = nullptr;
    

    }

    A user dialogue with the programme may, for example, be such

    Enter number of trains: 3

    Enter number of train #1: 1
    Enter location for train #1: A
    Enter departure for train #1: A1
    Enter number of train #2: 2
    Enter location for train #2: B
    Enter departure for train #2: B2
    Enter number of train #3: 3
    Enter location for train #3: C
    Enter departure for train #3: C3
    Number: 1
    Location: A
    Departure: A1

    Number: 2
    Location: B
    Departure: B2

    Number: 3
    Location: C
    Departure: C3

    You can modify it at your discretion.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2