Dynamic memory allocation

This is a discussion on Dynamic memory allocation within the C++ Programming forums, part of the General Programming Boards category; Hello all. I am working on a project for a college level C++ course, and have run into a big ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    16

    Dynamic memory allocation

    Hello all. I am working on a project for a college level C++ course, and have run into a big snag. Here is the text of the assignment:

    1. For this assignment, you will be creating a car search engine.

    2. Your program should begin by reading a file that has the following record format

    make model year price mileage


    3. The name of the input file will be data1. There will be one record per line. There is no limit to the number of records. You MUST use dynamic memory allocation.

    4. Your program should read in the file and store the records using a structure.

    5. It should then prompt the user for search parameters


    Please enter the search criteria:Make:Model:Year from:Year to:Price from:Price to:Mileage from:Mileage to:

    6. Your program should search all the records based on the above criteria and list the results.

    7. If the user puts in ANY for the make and model search for all makes and models. The range of years in the car information input file will be from 1984 to 2005. The user can search within this range of years. The maximum price in the car information input file will be 500,000. The maximum mileage in the car information input file will be 200,000.


    I am having troubles with parameter number 3. Specifically, I'm not sure how to go about using dynamic memory allocation with a an array of a structure type. I know how to use DMA with a normal array, but when I try inputting the data from the structure array, I keep getting a compile error. Does anyone have any idea how to go about this? I would really appreciate any help.

    Thanks.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    Post some code and/or the compiler error you are getting.

    gg

  3. #3
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    I'm willing to bet your compile error has something to do with variable-sized arrays.

    Let's suppose you have a struct called Dante.

    Code:
    struct Dante
    {
      int lengthOfWang;
    };
    To create it dynamically, you could do this:

    Code:
    Dante* array = new Dante[ size ];  // where size is the size of the array.
    Don't forget to delete the dynamically allocated array later.

    Code:
    delete[] array;

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    16
    Ok, here is my code:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    struct car{
            char make[50];
            char model[50];
            int year;
            int price;
            int mileage;
    };      
    int main(){
            ifstream i;
            i.open("data1");
            int size = 50;
            for (int x = 0; x < size; x++){
            car *search = new car[size];
            while (i >> search[x].make){
                    i >> search[x].model >> search[x].year >>
                    search[x].price >> search[x].mileage;
            }
            }
            i.close();
            delete search;
            return 0;
    }
    And here is the error message I'm getting.
    Code:
    project8.cpp: In function `int main()':
    project8.cpp:24: error: type `<unknown type>' argument given to `delete',
       expected pointer
    Any ideas on what's wrong?

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You declare search inside the for loop, but you delete it outside the loop. You must delete it inside the loop. Also, since you used [] with new to allocate memory for it, you must use delete [] instead of just delete.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    16
    Thanks. Can't believe I missed that. Just wondering, does anyone see anything that could go wrong in the future? Because I still have to write a couple search functions, and I'm worried that my code as it is now may cause them to mess up later.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    16
    Ok, here's my code as of now, along with the error messages I'm getting.
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    struct car{
            char make[50];
            char model[50];
            int year;
            int price;
            int mileage;
    };
    void search_year(car a[], int s, int val){
            for (int i = 0; i < s; i++){
                    if (a[i] == val)
                    cout << "Found";
    
            }
    }
    int main(){
            ifstream i;
            i.open("data1");
            int size = 50;
            for (int x = 0; x < size; x++){
            car *search = new car[size];
            if (x == size){
                    car *temp = search;
                    size += size;
                    search = temp;
            }
            while (i >> search[x].make){
                    i >> search[x].model >> search[x].year >>
                    search[x].price >> search[x].mileage;
            cout << "Enter year search criteria: ";
            int year;
            cin >> year;
            search_year(search, size, year);
            }
            delete[] search;
            }
            i.close();
            return 0;
    }
    
    project8.cpp: In function `void search_year(car*, int, int)':
    project8.cpp:13: error: no match for `car& == int&' operator
    I need to write functions that search for the make, model, year, price, and mileage. Does anyone know how I can do this easily? Will I need to write a seperate function for each one, or is it possible to do them all in the same function? Thank you very much.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    16
    No one have any ideas? I'm really stuck here and it's due tomorrow.

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You can figure this one out. Look at line 13. The error mentions ==. Look at the statement with the ==. Does it look right? Since == compares two things, what two things are you comparing? The error message tells you what you are actually comparing, is that what you meant to compare? The function name is search_year, maybe that will give you a hint at what you should change to make that comparison correct.

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    16
    Ok, so I need to make val type 'car' correct? I've tried doing that, and I still get the same error message.

  11. #11
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    the error says that you're trying to compare a car with an int. You want to campare a car's year with year searched.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    16
    I just figured that out. I feel a bit silly now.

  13. #13
    Registered User
    Join Date
    Apr 2004
    Posts
    16
    Ok, here is the way my code looks now
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    struct car{
            string make;
            string model;
            int year;
            int price;
            int mileage;
    };
    void search_make(car a[], int s, string val){
            for (int i = 0; i < s; i++){
                    if (a[i].make == val){
                            cout << a[i].make << endl;
                            cout << a[i].model << endl;
                            cout << a[i].year << endl;
                            cout << a[i].price << endl;
                            cout << a[i].mileage << endl;
                    }
    
            }
    }
    void search_model(car a[], int s, string val){
            for (int i = 0; i < s; i++){
                    if (a[i].model == val){
                            cout << a[i].make << endl;
                            cout << a[i].model << endl;
                            cout << a[i].year << endl;
                            cout << a[i].price << endl;
                            cout << a[i].mileage << endl;
                    }
    
            }
    }
    void search_year(car a[], int s, int val){
            for (int i = 0; i < s; i++){
                    if (a[i].year == val){
                            cout << a[i].make << endl;
                            cout << a[i].model << endl;
                            cout << a[i].year << endl;
                            cout << a[i].price << endl;
                            cout << a[i].mileage << endl;
                    }
    
            }
    }
    void search_price(car a[], int s, int val){
            for (int i = 0; i < s; i++){
                    if (a[i].price == val){
                            cout << a[i].make << endl;
                            cout << a[i].model << endl;
                            cout << a[i].year << endl;
                            cout << a[i].price << endl;
                            cout << a[i].mileage << endl;
                    }
    
            }
    }
    void search_mileage(car a[], int s, int val){
            for (int i = 0; i < s; i++){
                    if (a[i].mileage == val){
                            cout << a[i].make << endl;
                            cout << a[i].model << endl;
                            cout << a[i].year << endl;
                            cout << a[i].price << endl;
                            cout << a[i].mileage << endl;
                    }
    
            }
    }
    int main(){
            ifstream i;
            i.open("data1");
            int size = 50;
            for (int x = 0; x < size; x++){
                    car *search = new car[size];
                    if (x == size){
                            car *temp = search;
                            size += size;
                            search = temp;
                    }
                    while (i >> search[x].make){
                            i >> search[x].model >> search[x].year >>
                            search[x].price >> search[x].mileage;
                    }
            string model, make;
            int yearfrom, yearto, pricefrom, priceto, mileagefrom, mileageto;
            cout << " Please enter search criteria:" << endl;
            cout << " Make: ";
            cin >> make;
            cout << " Model: ";
            cin >> model;
            cout << " Year from: ";
            cin >> yearfrom;
            cout << " Year to: ";
            cin >> yearto;
            cout << " Price from: ";
            cin >> pricefrom;
            cout << " Price to: ";
            cin >> priceto;
            cout << " Mileage from: ";
            cin >> mileagefrom;
            cout << " Mileage to: ";
            cin >> mileageto;
            delete[] search;
            }
            i.close();
            return 0;
    }
    The problem I am having is with instruction number 7 (at the start of this thread). I'm not sure how to change my search functions so that if they put ANY for make and/or model, it will just disregard them. Also, how can I change my other search functions so they will search within a specific set of numbers? Thanks.

  14. #14
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    In plain english, it is something like this:

    If the make typed in by the user matches the make of this car or the make typed in by the user is "ANY", then output the data for the car.

    The same goes for model. Also, the same idea applies to the numbered properties. You have an if statement in each search function that checks to see if something is true. Now, you have more things to check, so just add them to the if statement.

    And by the way, since you are using string (very good) you should add the string header <string>. You might get funny results if you don't include the header, even if it seems to work without it.

  15. #15
    Registered User
    Join Date
    Apr 2004
    Posts
    16
    Actually, if "ANY" is criteria, shouldn't the program just disregard those criteria, and then just search the rest of them? Also, I don't really want to output the data for a car every time there's a match in one criteria. Is there any way that I can just output the data once for a match, and then go on? Maybe I should write one function instead of five? I'm really confused now.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21