Unable to find error in Sorting & Searching codes

Hi, this is my first time coding a searching and sorting program so I have a hard time finding what is the error I am having now. I hope someone can help me correct the errors that might be in my codes. The problem is whenever I wanted to run the sorting program, I will stuck halfway.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
if(select==1)
		{	SEARCH:
			char search;
			cout<<"\nDo you want to do searching?"<<endl;
			cout<<"\nEnter Your Choice(y/n) : ";
			if(search=='y'||search=='Y'){

			}
			else if(search=='n'||search=='N'){
				cout << "\n-------------------------CUSTOMER INFORMATION-------------------------\n";
				cout << "   Name\t\temail\t\t\tIC\t\tPassword" << endl;
				for (int i=0; i<=count-2; i++ ){
					cout << i <<". ";
					for(int j=0; j<=count-2; j++ ){
						cout << list[i][j]<< "\t";
					}
					cout<<endl;
				}

			}
			else {
				cout<<"\n Please enter the correct selection!\n";
				goto SEARCH;
				}

		cusDetails();
		file.close();
        }
		else if(select==2)
		{
        cout << "How do you want the list to be sorted?"<<endl;
	    cout<<"\n<1> Name";
	    cout<<"\n<2> email";
	    cout<<"\n<3> IC";;
	    cout<<"\nEnter Your Choice : ";
	    cin>> ch;

	    quicksort(listCustomer,0,count,ch-1,a);

		cout << "Sorted data: "<<endl;
		for (int i=0; i<=count-2; i++ ){
			for(int j=0; j<=count-2; j++ ){
				cout<<listCustomer[i][j]<< "\t";
				}
			cout<<endl;
			}
		file.close();
		cusDetails();

		}


Thanks in advance!
Don't use goto. Forget that you ever knew anything about goto. Use an appropriate loop instead.

What is the type of list and listCustomer? What is count and why is the loop termination <= count - 2?? For both dimensions of the array?
Nice to see someone learning on their own. I can tell you are working this out on your own because of the unique style you have in this code.

I don't see your code for quicksort(), and the standard library version was called qsort() I thought. So it's hard to tell if there's a problem in your sort code or not.

Your for loops in your sort branch of your if seem like they should always terminate.

When I get stuck in programming, I try to simplify and add print statements to find the problem. For example, test your sorting routine, if you wrote it, or it's in question, with just one element. Then two elements. Then three. Put print statements in key places, so you can tell what it's doing. quicksort is a bit tricky to write, if you haven't done a lot of recursive function programming, so it might take time to get it working properly.

Some general C++ advice based on looking at your code, hope it's welcome...

* Don't ever use goto. You can use functions for your cases here I think. In general, break up all your code into short, clearly defined functions, and programming is easier. It's recommended to test each function separately.
* There's a handy #include <cctype>. With a toupper function, making it easier to do your test for 'Y'

I suspect the assignment is to create a sorted list and search it. Hence, don’t recommend using standard algorithms... assume OP is smart enough to use them if he/she were permitted and/or instructed to do so.

@excavator

You are confusing yourself by mixing tasks. I recommend you create a simple menu loop like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const int MAX_ITEM_COUNT = 100;
cont int NUM_FIELDS = 4; // name, email, IC, password
std::string list[MAX_ITEM_COUNT][NUM_FIELDS];
int count = 0; // number of items in list[]

int main()
{
  bool done = false;
  while (!done)
  {
    std::cout << "\n"
    "Menu:\n"
    "1. Add Item\n"
    "2. Find Item\n"
    "3. List Items\n"
    "0. Quit\n"
    "\n"
    "Choice? ";

    int choice;
    std::cin >> choice;

    switch (choice)
    {
      case 1: add_item(); break;
      case 2: find_item(); break;
      case 3: list_items(); break;
      case 0: done = true;
      default: 
    }
  }
}

You need to create each of the three functions. Each function should do its one and only thing, then be done.

The things that you can do are up to you, of course. This is just an example.

Remember that the count (number of items in the list, 0 to 100) is not the same as the number of fields. When searching, i starts at zero and ends at count:

 
  for (int i = 0;   i < count;   i++)

but the j iterates through the fields of each item: it starts at zero and ends at NUM_FIELDS (or whatever you call it):

1
2
3
4
5
6
7
  for (int i = 0;   i < count;   i++)
  {
    for (int j = 0;   j < NUM_FIELDS;   j++)
    {
      ...
    }
  }

Unless you are specifically tasked to sort the items because the user asked for it (by name, perhaps?), you really do not need to sort. Find where the item is to be inserted in the list, then shift every item over one and insert the new item in the right spot. This is a little trickier with a 2D array, but it works using the very same logic as inserting an integer in an array of integers — something you should have been required to do before now.

Hope this helps.
Topic archived. No new replies allowed.