Dynamic memory allocation

This is a discussion on Dynamic memory allocation within the C++ Programming forums, part of the General Programming Boards category; Ok, I combined all the functions into one, and it's working pretty well... Code: #include <iostream> #include <fstream> using namespace ...

  1. #16
    Registered User
    Join Date
    Apr 2004
    Posts
    16
    Ok, I combined all the functions into one, and it's working pretty well...
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    struct car{
            string make;
            string model;
            int year;
            int price;
            int mileage;
    };
    void search_all(car a[], int s, string make, string model, int yearfrom,
    int yearto, int pricefrom, int priceto, int mileagefrom, int mileageto){
    	for (int i = 0; i < s; i++){
    		if (a[i].make == make){
                    	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;
                    	return;
    		}
                    else if (a[i].model == model){
                    	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;
                    	return;
    		}
              
    		else if ((a[i].year >= yearfrom) && (a[i].year <= yearto)){
    			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;
            		return;
    		}
    		else if ((a[i].price >= pricefrom) && (a[i].price <= priceto)){
    		        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;
    		        return;
    		}		
    		else if ((a[i].mileage >= mileagefrom) && (a[i].mileage <= mileageto)){
    		        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;
    		        return;
    		}
            }
    }
    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;
            search_all (search, size, make, model, yearfrom, yearto,
            pricefrom, priceto, mileagefrom, mileageto);
            delete[] search;
            break;
            }
            i.close();
            return 0;
    }
    The only problem is, it's only outputting one set of data, even if the criteria I put in matches different records in the data file. I know it has something to do with my if/else statements, but I can't figure out what. How can I change them so that it will output a set of data for each match that it finds? Oh yeah, and there's that little "ANY" problem. I still can't figure that one out.

  2. #17
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    To fix the problem of only printing one match, you need to remove the code that returns from the function after a single match is displayed. What you want to do is continue with the loop so that the next car can be checked.

    Change "return;" to "continue;"

    As for the "ANY", that should have been a big enough hint:
    Quote Originally Posted by jlou
    If the make typed in by the user matches the make of this car OR the make typed in by the user is "ANY"
    And don't forget #include<string>.

  3. #18
    Registered User
    Join Date
    Apr 2004
    Posts
    16
    Ok, that didn't actually help much. It did the same thing, except this time, it printed out a long line of zeros afterwards.

  4. #19
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Your main function needs some work. You are not saving all the data in your data1 file. Instead, you read the data in to search[x] but you never change x in your while loop.

    You need to rethink your for and while loops in your main function. The first step to doing that is to fix the indentation like this:
    Code:
    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;
                    search_all (search, size, make, model, yearfrom, yearto,
                    pricefrom, priceto, mileagefrom, mileageto);
                    delete[] search;
                    break;
            }
            i.close();
            return 0;
    }
    Notice how in each time through the for loop, you load all the data in the file, and then prompt the user to search, and then run the search. And then you immediately break out of the for loop so it only runs once.

    Don't you want to only load the data in the file first inside the for loop, and then after the for loop let the user search what you loaded? Also, when I said earlier that you must delete search inside the loop, I was assuming that you meant to declare it inside the loop. It might be more appropriate for you to declare it before the for loop and then delete it at the end of the program outside the for loop after the searching is done.

  5. #20
    Registered User
    Join Date
    Apr 2004
    Posts
    16
    Ok, I changed the main function so the for loop closes before the user can search. However, I'm still having the exact same problem. I think the problem is my search function, but I'm completely lost as to what it might be.
    Code:
    struct car{
            string make;
            string model;
            int year;
            int price;
            int mileage;
    };
    void search_all(car a[], int s, string make, string model, int yearfrom,
    int yearto, int pricefrom, int priceto, int mileagefrom, int mileageto){
            for (int i = 0; i < s; i++){
                    if (a[i].make == make){
                            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;
                    continue;
                    }
                    else if (a[i].model == model){
                            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;
                    continue;
                    }
                    else if ((a[i].year >= yearfrom) && (a[i].year <=
                    yearto)){
                            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;
                    continue;
                    }
                    else if ((a[i].price >= pricefrom) && (a[i].price <=
                    priceto)){
                            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;
                    continue;
                    }
                    else if ((a[i].mileage >= mileagefrom) && (a[i].mileage <=
                    mileageto)){
                            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;
                    continue;
                    }
            }
    }
    int main(){
            ifstream i;
            i.open("data1");
            int size = 50;
            car *search = new car[size];
            for (int x = 0; x < size; x++){
                    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;
                    }
            }
            i.close();
            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;
            search_all (search, size, make, model, yearfrom, yearto,
            pricefrom, priceto, mileagefrom, mileageto);
            delete[]search;
            return 0;
    }
    By the way, did I mention how much I appreciate your help?

  6. #21
    Registered User
    Join Date
    Apr 2004
    Posts
    16
    I don't know if this will help any, but what is being returned is the last member of the data file, no matter what I put in for search criteria.

  7. #22
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by jlou
    You are not saving all the data in your data1 file. Instead, you read the data in to search[x] but you never change x in your while loop.
    That is what is causing the last member of the data file to be returned every time, because only the last member of the data file is saved.

    Your while loop reads in a single car each time through. So shouldn't you really only be running that code once per for loop?

    ....


    Ok, gotta go now. Don't look if you don't want to know the answer:



    Change while to if in your main function. I made only that change and was able to get some good output.


    Once you fix that, your code that resizes the array doesn't do anything. There are some other little errors that might be around also, but you'll be a lot closer. I'm done for the night, so good luck.

Page 2 of 2 FirstFirst 12
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