Help with code please!

Discussion in 'C++' started by shafishstix, Jun 25, 2011.

  1. shafishstix

    shafishstix New Member

    Joined:
    Jun 25, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi everyone this is my first post and it would be great if someone could help me out. Im currently learning c++ from a book and im stuck on an exercise, the code i create doesnt result in a fully functioning program.

    There are two problems, first i create a struct and dynamically create an array of "so many" of that struct. However when i access the objects in the array i begin referring to its elements from 1 not 0, does that seem correct to you?

    The other problem is the cin function call is skipped during the for loop and the possible solution is perplexing me. Here is the code -

    Code:
    #include <iostream> 
    /* This program should request the make and model year of a car, then
    print out the results */
    
    const int ArSize = 10;
    
    struct car { 
           char car_make;
           int model_year;      
    };
    
    int main()
    {
        using namespace std;
        int car_num;
        cout << "How many cars do you wish to catalogue? ";
        cin >> car_num;
        car * car_array = new car[car_num];
        
        for (int i = 1; i <= car_num; i++)
        {
               cout << "Car #" << i << ":" << endl;
               cout << "Please enter the make: ";
               cin.get(car_array[i].car_make);
               cout << "\nPlease enter the year made: "; 
               cin >> car_array[i].model_year;
               cout << endl << endl;
        }
        
        for (int i = 1; i <= car_num; i++)
        {
            cout << "\nThe make of car " << i << " is: " << car_array[i].car_make; 
            cout << "The year of car " << i << " is: " << car_array[i].model_year;
            cout << endl;
        }
        return 0; 
    }
     
    Last edited by a moderator: Jun 25, 2011
  2. alssadi

    alssadi Banned

    Joined:
    Dec 11, 2010
    Messages:
    41
    Likes Received:
    3
    Trophy Points:
    0
    Occupation:
    Creative director & web developer
    Location:
    Dubai
    Home Page:
    http://uaeinfographics.blogspot.com/
    can you tell us what is the functionality of this program , its very important to know that

    regards alssadi
     
  3. shafishstix

    shafishstix New Member

    Joined:
    Jun 25, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Yeah sure sorry, basically its an exercise from a book, the instructions were to -

    First create a structure declaration which holds information about a cars make and the year of its model. (The structure member which describes the cars make can either be char or a string and the year an integer)

    The program should request how many cars to catalog, then create a dynamic array of that many car structures.

    Then the program requests input into each structure element of the array for the members which represent the car make and its model year.

    The program should then out put the results inputted.

    So the above program should run something like this -

    How many cars do you wish to catalogue? 1 // 1 is the input in this instance
    Car# 1:
    Please enter the make: citroen // citroen = input
    Please enter the year made: 1984 // 1984 = input
    The make of car 1 is: citroen
    The year of car 1 is: 1984

    Im a newby so hopefully i've explained it properly. Im using DevC++ which uses GCC as its compiler.

    Thanks.
     
  4. shafishstix

    shafishstix New Member

    Joined:
    Jun 25, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Ok so ive checked over the code and made some improvements. It mostly works but requires a few while loops which i copied from the book solution to the exercise (i have no idea how it works). Also the program still crashes on exit which has me stumped (i am running a 64 bit system and i have no idea if it makes a difference to the compiler. Here is the code -

    Code:
    #include <iostream> 
    #include <string>
    /* This program should request the make and model year of a car, then
    print out the results */
    
    const int ArSize = 10;
    
    struct car { 
           char car_make[ArSize];
           int model_year;      
    };
    
    int main()
    {
        using namespace std;
        int car_num;
        cout << "How many cars do you wish to catalogue? ";
        cin >> car_num;
        car * car_array = new car[(car_num - 1)];
        while(cin.get() != '\n') 
            ;
        /* I copied this while statement from exercise code but i dont know
        why it is needed to get cinput functionality to work properly*/
        
        for (int i = 0; i < car_num; i++) 
        {
               
               cout << "Car #" << i+1 << ":" << endl;
               cout << "Please enter the make: ";
               cin.getline(car_array[i].car_make, ArSize); //Starting at indice 1
               cout << "\nPlease enter the year made: "; 
               cin >> car_array[i].model_year;
               cout << endl << endl;
               while(cin.get() != '\n') 
               ;
        }
        
        for (int i = 0; i < car_num; i++)
        {
            cout << "\nThe make of car " << i+1 << " is: " << car_array[i].car_make; 
            cout << "\nThe year of car " << i+1 << " is: " << car_array[i].model_year;
            cout << endl;
        }
        cout << "About to crash";
        return 0; 
    }
    
     
  5. priyatendulkar

    priyatendulkar New Member

    Joined:
    Jun 20, 2011
    Messages:
    20
    Likes Received:
    1
    Trophy Points:
    0
    Hi,

    Your code crashes becoz of line ..

    car * car_array = new car[(car_num - 1)];
    Say for example..
    car_num =4.
    In your case memory will be allocated only for 3 objects.Hence the program crashes.


    Modify it as

    car * car_array = new car[(car_num )];
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice