Hi all,

I'm having a heck of a time with our latest assignment. I've tried everything I can possibly think if to make this work, but with no luck. We need to Load an inventory text file, sort it, search it(haven't even got to that portion of the code). I can't seem to get my insertion sort to work properly. I can get it to sort the item number, which is an int array named items. I cannot get it to sort the corresponding item names though. Also, for some reason when it loads the text file, it prints out the last line of the file twice. Anyone who is willing to look at the code, I would very much appreciate it. Thanks.

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int load (char filename[], int items[], string names[], int& n);
int add(char filename[], int items[], string names[], int& n);
void sort(int items[], string names[], int n);
const int SIZE = 500;
int main(int argc, char *argv[])
{
    
    char filename[20], answer = 'n';
    int items[SIZE];
    string names[SIZE];
    int selection, n = 0;
    
    cout << "Enter a filename to load: ";
    cin >> filename;
    
    do {
       cout << "----INVENTORY MENU----" << endl << endl;
       cout << "1.  Load Inventory"<< endl;
       cout << "2.  Add to Inventory" << endl;
       cout << "3.  Search Inventory" << endl;
    
       cin >> selection;
    
    
    
       if (selection == 1) {
          load(filename, items, names, n);
          cout <<"There are " << n << " items in " << filename << "." << endl;
          } 
       if (selection == 2) {
          add(filename, items, names, n);
          }
          
       cout << "Would you like to make another menu selection? [Y/N] ";
       cin >> answer;
          
    }while(answer == 'y' || answer == 'Y');
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

int load(char filename[], int items[], string names[], int& n) {
    ifstream in_stream;
    in_stream.open(filename);
    string data;
    n = 0;
    char next_symbol, number[15];
    
    do {
        int i = 0;
        do {
            in_stream.get(next_symbol);
            number[i] = next_symbol;
            i++;
        } while (next_symbol != ' ');
        items[n] = atoi(number);
            
        getline(in_stream, data);
        cout << data << endl;
        if ( data != "") {
          names[n] = data;
         
          }
         cout << endl << items[n];
         n++;
         
         
    } while (! in_stream.eof());
        
    in_stream.close();
}

int add(char filename[], int items[], string names[], int& n){
    ofstream out_stream(filename, ios::app);
   
    cout << "Enter an item number: ";
    cin >> items[n];
    cout << "Enter an item name: ";
    cin >> names[n];
    cout << endl << "Load section n is " << n << endl;
    out_stream << items[n] << setw(15) << names[n] << endl;
    out_stream.close();
    sort(items, names, n);
}
      
void sort(int items[], string names[], int n){

cout << "BEFORE THE SWAP" << endl;
for (int i = 0; i <n; i++) {
        cout << items[i] << setw(15) << names[i]<< endl;
        }

int key,i;

for(int j=1;j<n;j++) {

key=items[j];
i=j-1;

      while(items[i]>key && i>=0) {
          items[i+1]=items[i];
          names[i+1]=names[i];
          i--;
      }
      items[i+1]=key;
}
cout << "AFTER THE SWAP" << endl;

for (int i = 0; i <= n; i++) {
        cout << items[i] << setw(15) << names[i]<< endl;
        }
return;
}

Can you post a sample input file so I can take a look?

Are the item numbers linked to the items names. So if you put them in a multi dimensional array you can just sort it using the item number, or does it need to be able to sort by item number AND alphabetically by the item name?


Thanks.

I really don't know how to work with files this way, but i made a few changes to your code and it worked (i didn't check if the program was working correctly, just got rid of all the error messages).
Well, this were my small changes:

First, you need to include the string library if you're going to use getline . #include <string> .

I also changed the return type of your functions and the parameter list: int load(char filename[], int items[], string names[]); , in this function You don't need to pass int &n , because you're turning it into 0 at the begining of the function, every time. Plus, your return type was int, so you need to return something of type integer. So, I put at the end of the function return n; . And i the calling line, the code would look like this: n = load(filename, items, names); .

The add function is not returning anything, so i changed it to: void add(char filename[], int items[], string names[], int& n) , remember that if you declare a function as int, it must return a value of type integer.

After this small changes, the program compiled well (no errors). Hopefully it is doing the work, the way it is supposed to do it.
I think I can create a new file and just use te add function to see if it works. If I find something I'll let You know.

Can you post a sample input file so I can take a look?

Are the item numbers linked to the items names. So if you put them in a multi dimensional array you can just sort it using the item number, or does it need to be able to sort by item number AND alphabetically by the item name?


Thanks.

I suppose using a multidimensional array would simplify the sorting process, I'm just unsure of how to use them. Any suggestions? It only needs to be sorted through item number, then printed with the corresponding item name. Thanks for taking the time to help.

I really don't know how to work with files this way, but i made a few changes to your code and it worked (i didn't check if the program was working correctly, just got rid of all the error messages).
Well, this were my small changes:

First, you need to include the string library if you're going to use getline . #include <string> .

I also changed the return type of your functions and the parameter list: int load(char filename[], int items[], string names[]); , in this function You don't need to pass int &n , because you're turning it into 0 at the begining of the function, every time. Plus, your return type was int, so you need to return something of type integer. So, I put at the end of the function return n; . And i the calling line, the code would look like this: n = load(filename, items, names); .

The add function is not returning anything, so i changed it to: void add(char filename[], int items[], string names[], int& n) , remember that if you declare a function as int, it must return a value of type integer.

After this small changes, the program compiled well (no errors). Hopefully it is doing the work, the way it is supposed to do it.
I think I can create a new file and just use te add function to see if it works. If I find something I'll let You know.

I tried what you suggested, still no luck with the actual sorting. Another user suggested maybe using a multidimensional array, hopefully that would simplify the sorting process. I'll keep trying everything I can think of, only way to learn I guess. Thanks for taking the time to help!

I suppose using a multidimensional array would simplify the sorting process, I'm just unsure of how to use them. Any suggestions? It only needs to be sorted through item number, then printed with the corresponding item name. Thanks for taking the time to help.

I just realised that the 2 data types are different so using a multi dimensional array won't work. However if you make a struct with the datatypes int and string. Then use a one dimensional array and store the number in the int and the name in the string. Then when doing your sort you will do something like myArr[index].itemNumber to access the item number and sort the arrays using that value. Then when you have it sorted you just print it out running through the array using myArr[index].itemName

Are You allowed to use a class or a struct?
For example...

struct item{
	int total;	// To store the ammount of items of this product
	string name;	// The name of the product
	string code;	// If You wanna add a code to the product
	item *next;	// Pointer to the next item of the same type
};

That way you can keep your items sorted very easy
Maybe someone else can make another suggestion?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.