Thread: linear sort must

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    35

    Question linear sort must

    I have put together a program that will display the contents of file if selected or search that file for a product by product number and display the number and price of that product only. I have to use the linear search method. This is what I have so far:
    Code:
    #include <iostream>
    #include <vector>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
    	vector<int> number;
    	vector<double> price;
    	int num;
    	double pri;
    	int selection;
    	int location, found, product_number;
    	ifstream inData("productinfo.txt", ios::in);
    	if(!inData)
    	{
    		cerr << "File could not be opened!" << endl;
    		exit(1);
    	}
    	cout << "This program lets the user determine the path,"
    		 << "\nselect 1 to see the table of items or select 2"
    		 << "\nto search for a product by number: ";
    	cin >> selection;
    	if(selection == 1)
    	{
    		while(!inData.eof())
    		{
    			inData >> num >> pri;
    			number.push_back(num);
    			price.push_back(pri);
    		}
    		cout << setw(10) << "Product no." << setw(10)<< "Price" << endl;
    		for(int i = 0; i < number.size(); i++)
    			cout << setw(10)<< number[i] << setw(10) << price[i] << endl;
    	}
    	else
    	{
    		cout << "Enter product number :\n";
    		cin >> product_number;
    		for(int i = 0; i < number.size(); i++)
    		if(product_number == number[i])
    			cout << number[i];
    		else
    			++number[i];
    
    
    	}
    }
    this is the input file
    Code:
    100 12.88
    101 10.99
    102 2.99
    103 4.99
    104 5.99
    105 3.99
    106 3.44
    107 5.77
    108 5.66
    109 5.67
    110 6.99
    I cannot get the linear sort method worked out can someone please help?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    if(selection == 1)
    	{
    		while(!inData.eof())
    		{
    			inData >> num >> pri;
    			number.push_back(num);
    			price.push_back(pri);
    		}
    ...
    This seems to be the only place where you read the file. Controlling the input loop with eof means asking for trouble. See the FAQ why this is not good.

    Code:
    else
    	{
    		cout << "Enter product number :\n";
    		cin >> product_number;
    		for(int i = 0; i < number.size(); i++)
    		if(product_number == number[i])
    			cout << number[i];
    		else
    			++number[i];
    
    
    	}
    My guess would be that when you get here, you won't have read the data from the file (this is done in the if block, we're in else now), hence numbers is empty and the loop is skipped.
    I have no idea why you increment the product number if it is not the product you are looking for.

    Now, since both operations require that you have the data first, read it before responding to the users selection.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    I tried that the first time before I displayed my code and it still didnt work

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    This my revised code
    Code:
     #include <iostream>
    #include <vector>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
    	vector<int> number;
    	vector<double> price;
    	int num;
    	double pri;
    	int selection;
    	int location, found, product_number;
    	ifstream inData("product.txt", ios::in);
    	if(!inData)
    	{
    		cerr << "File could not be opened!" << endl;
    		exit(1);
    	}
    	cout << "This program lets the user determine the path,"
    		 << "\nselect 1 to see the table of items or select 2"
    		 << "\nto search for a product by number: ";
    	cin >> selection;
    	if(selection == 1)
    	{
    		while(!inData.eof())
    		{
    			inData >> num >> pri;
    			number.push_back(num);
    			price.push_back(pri);
    		}
    		cout << setw(10) << "Product no." << setw(10)<< "Price" << endl;
    		for(int i = 0; i < number.size(); i++)
    			cout << setw(10)<< number[i] << setw(10) << price[i] << endl;
    	}
    	else
    	{
    		while(!inData.eof())
    		{
    			inData >> num >> pri;
    			number.push_back(num);
    			price.push_back(pri);
    		}
    		int i = 0;
    		cout << "Enter product number :\n";
    		cin >> number[i];
    		
    		if(number[i] == number[i]){
    			for(int i = 0; i < number.size(); i++)
    				cout << price[i];
    				cout << number[i];}
    		else
    			++number[i];
    
    
    	}
    }
    does not display the data that i want it displays all prices with no space inbetween then it displays the product number that i chose. can someone help?

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    int i = 0;
    cout << "Enter product number :\n";
    cin >> number[i]; //why are you changing the product number of the first item?
    		
    if(number[i] == number[i]){ //this will always be true (turn on compiler warnings)
        for(int i = 0; i < number.size(); i++)
            cout << price[i];
            cout << number[i];} //this statement would not be executed in the for loop
     else
        ++number[i]; //why???
    I suppose you need to do a linear search (not sort). For that, get the product number from user. Then loop for the vector, checking each item if it is equal to the entered value. If you get to the end of the vector without finding a match, the requested ID is not there.
    Last edited by anon; 08-05-2007 at 01:20 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    Ok I changed it a bit now it displays all the prices with the numbers inbetween i dont know why it is not doing what I want
    Code:
    else
    	{
    		while(!inData.eof())
    		{
    			inData >> num >> pri;
    			number.push_back(num);
    			price.push_back(pri);
    		}
    		int i = 0;
    		cout << "Enter product number :\n";
    		cin >> product_number;
    		
    		if(product_number == number[i])
    		{
    			for(int i = 0; i < number.size(); i++)
    				cout << price[i] << number[i];
    		}
    		else
    			cout << "Product number you entered is incorrect!";
    
    
    	}

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    As already mentioned, you should avoid testing of eof to control your read loop. This:
    Code:
    ...
    while(!inData.eof())
    {
        inData >> num >> pri;
        ...
    Should be this:
    Code:
    ...
    while(inData >> num >> pri)
    {
        ...
    Further, you should consider only having one read loop. Just read the data in after you've tested that the file is opened successfully. Later, when you get the users choice for 1 or 2, you can then just have the loops to output the data.

    Consider making a struct/class that has both number and price data members and then a vector of this object instead of individual number/price vectors:
    Code:
    struct product
    {
        int number;
        double price;
        // Constructor
        product(int num, double pri) : number(num), price(pri) {}
    };
    
    int main()
    {
        vector<product> products;
        ...
        while(inData >> num >> pri)
        {
            // Create and push back an initialized product object onto vector
            products.push_back(product(num,pri)); 
        }
    If you want you can even overload the stream extraction operator to make things fancy for your new product struct/class objects:
    Code:
    ostream& operator>>(ostream& os, product& pr)
    {
        return os >> pr.number >> pr.price;
    }
    
    int main()
    {
        vector<product> products;
        product temp_product;
        ...
        while(inData >> temp_product)
        {
            products.push_back(temp_product);
        }
    Your search code is all wrong... you must get a number from the user, then loop through all the items until you find the correct one and then display the results if you find the correct one. Currently you are testing the product number entered by the user against the first number stored in the vector and if they match, then you output everything. You want something more like:
    Code:
    cout << "Enter product number :\n";
    cin >> product_number;
    		
    for( loop through the number vector)
        if( got a match )
            display number/price
    if( our loop variable reached the end of the vector )
        display "No match found"
    Last edited by hk_mp5kpdw; 08-05-2007 at 02:24 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    ok i changed it to
    Code:
     #include <iostream>
    #include <vector>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
    	vector<int> number;
    	vector<double> price;
    	int num;
    	double pri;
    	int selection;
    	int location, found, product_number;
    	ifstream inData("product.txt", ios::in);
    	if(!inData)
    	{
    		cerr << "File could not be opened!" << endl;
    		exit(1);
    	}
    	cout << "This program lets the user determine the path,"
    		 << "\nselect 1 to see the table of items or select 2"
    		 << "\nto search for a product by number: ";
    	cin >> selection;
    	if(selection == 1)
    	{
    		while(!inData.eof())
    		{
    			inData >> num >> pri;
    			number.push_back(num);
    			price.push_back(pri);
    		}
    		cout << setw(10) << "Product no." << setw(10)<< "Price" << endl;
    		for(int i = 0; i < number.size(); i++)
    			cout << setw(10)<< number[i] << setw(10) << price[i] << endl;
    	}
    	else
    	{
    		while(!inData.eof())
    		{
    			inData >> num >> pri;
    			number.push_back(num);
    			price.push_back(pri);
    		}
    		int i = 0;
    		cout << "Enter product number :\n";
    		cin >> product_number;
    		for(int i = 0; number.size(); i++)
    		if(product_number == number[i])
    			cout << " Price: " << price[i] << " Product Number: " << number[i] << endl;
    		if(product_number != number[i])
    			cerr << "Product number not correct!";
    		}
    }
    It displays the write information, however, I get a vector subscript out of range error after executing it but it compiles fine and when I enter a value that is not correct it doesnt do anything.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    for(int i = 0; number.size(); i++)
        if(product_number == number[i])
            cout << " Price: " << price[i] << " Product Number: " << number[i] << endl;
    if(product_number != number[i])
        cerr << "Product number not correct!";
    That should be:
    Code:
    vector<int>::size_type i;
    for( i = 0; i < number.size(); i++)
        if(product_number == number[i])
            cout << " Price: " << price[i] << " Product Number: " << number[i] << endl;
    if(i == number.size())
        cerr << "Product number not correct!";
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    Thanks but i cant get the error message to display only when the wrong number is entered

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Post the code. The previous answer should have solved that problem.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Slight modification to my previous suggestion... as it stood, the "Product number not correct!" message would have always been output. We need to jump out of the loop once a match has been found so i doesn't get incremented to number.size(). This is one way to do it:
    Code:
    vector<int>::size_type i;
    for( i = 0; i < number.size(); i++)
        if(product_number == number[i])
        {
            cout << " Price: " << price[i] << " Product Number: " << number[i] << endl;
            break;
        }
    if(i == number.size())
        cerr << "Product number not correct!";
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> ok i changed it to

    You didn't change while(!inData.eof()). If you don't change that, the loop will run one extra time and your list will have the last item twice.

  14. #14
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    thanks gents for all the help it worked!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  4. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM
  5. linear sort with names
    By Agnesa in forum C++ Programming
    Replies: 2
    Last Post: 11-22-2002, 06:55 PM