2d dynamic array swapping

Hello I'm having a problem with swapping. It's supposed to swap the entire array to sort by users choice, but its only swapping part of the array.

My issue is in the function void pet_Alph_Sorter, any hints to solve this problem would be very helpful, thank you.

example:
https://gyazo.com/001515f442f8ebd935bfdc676a5936b9 this is the console output photo

name:c
species:c
breed:c
gender:c

name:b
species:b
breed:b
gender:b

name:a
species:a
breed:a
gender:a

output
name:c
species:c
breed:a
gender:c

name:b
species:b
breed:c
gender:b

name:a
species:a
breed:b
gender:a

here's my current code
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cassert>

using namespace std;

const int IDENTIFIERS = 4;// identifiers breed gender name species
//const int MAX_ADOPTION_CELLS = 100;

int getArraySize();
void pet_Alph_Sorter(string** cellsPointer, int userSize);
void petSortMenu(string** cellsPointer, int userSize, int choice);
int userSortOption();
void fillPetArray(string** cellsPointer, int userSize);

typedef string* AdoptionCells;

int _tmain(int argc, _TCHAR* argv[])
{
	int userSize = getArraySize(); //gets users size
	//cout << "users size: " << userSize << "\n";

	

	AdoptionCells *cellsPointer = new AdoptionCells [userSize];

	
	fillPetArray(cellsPointer, userSize);
	int choice = 0;
	petSortMenu(cellsPointer, userSize, choice);

	//testing purpose will move to apart of sort function
	cout << "displaying info in 2d Dry: \n";
	for (int i = 0; i < userSize; i++){
			cout << "name: " << cellsPointer[i][0] << "\n";
			cout << "species: " << cellsPointer[i][1] << "\n";
			cout << "breed: " << cellsPointer[i][2] << "\n";
			cout << "gender: " << cellsPointer[i][3] << "\n";
		
		cout << endl;
	}
	system("pause");
	return 0;
}

//gets info from user and stores it in array
void fillPetArray(string** cellsPointer, int userSize){
	string name, species, breed, gender;
	cin.ignore();

	for (int id = 0; id < userSize; id++){
		cellsPointer[id] = new string[IDENTIFIERS];// creates the 2d array 

		cout << "Pet #" << id + 1 << "\n"; //i+1 to make it look like pet1 instead of pet0
		cout << "name: ";
		getline(cin, name);
		cellsPointer[id][0] = name;
		cout << "species: ";
		getline(cin, species);
		cellsPointer[id][1] = species;
		cout << "breed: ";
		getline(cin, breed);
		cellsPointer[id][2] = breed;
	//	do{ //uncomment this out later once im done finishing the sorter
			cout << "gender(M/F): ";
			getline(cin, gender);
			cellsPointer[id][3] = gender;
		//} while ((gender != "M") && (gender != "m") && (gender != "F") && (gender != "f"));
	}
}

//thought this pet sorter would work but since its not a const it flips where it shouldnt be?
//issue here
void pet_Alph_Sorter(string** cellsPointer, int userSize, int choice){
	int currentCell = 0;
	

	int ident = 0;
	string temp;

	for (currentCell = 0; currentCell < userSize; currentCell++){
		for (ident = 0; ident < userSize; ident++){


			temp = cellsPointer[currentCell][choice];
			cellsPointer[currentCell][choice] = cellsPointer[ident][choice];
			cellsPointer[ident][choice] = temp;
			
		}
	}
}


//sort menu with display
void petSortMenu(string** cellsPointer, int userSize,int choice){

	cout << "Sort by: \n" <<
	 "0 - Name \n" <<
	 "1 - Species \n" <<
	 "2 - Breed \n" <<
	 "3 - Gender \n" << endl;

	// int choice;
	do
	{
		 choice = userSortOption();
		enum pets { NAME, SPECIES, BREED, GENDER };
		switch (choice)
		{
		case NAME:
			pet_Alph_Sorter(cellsPointer, userSize, choice);
			break;
		case SPECIES:
			pet_Alph_Sorter(cellsPointer, userSize, choice);
			break;
		case BREED:
			pet_Alph_Sorter(cellsPointer, userSize, choice);
			break;
		case GENDER:
			pet_Alph_Sorter(cellsPointer, userSize, choice);
			break;
		}
		//change to a do you wanna sort again y/n , need new function to ask
	} while ((choice < 0) && (choice > 4));

}


//user defined size of array
int getArraySize(){
	int userSize;
	cout << "Enter how many pets you want to put into the system. \n";
	cin >> userSize;
	return userSize;
}

//gets sort option
int userSortOption(){
	int usersSortChoice;
	cin >> usersSortChoice;
	return usersSortChoice;
}




Which bit of this is meant to be swapping something, and what is it meant to be swapping?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
		switch (choice)
		{
		case NAME:
			pet_Alph_Sorter(cellsPointer, userSize, choice);
			break;
		case SPECIES:
			pet_Alph_Sorter(cellsPointer, userSize, choice);
			break;
		case BREED:
			pet_Alph_Sorter(cellsPointer, userSize, choice);
			break;
		case GENDER:
			pet_Alph_Sorter(cellsPointer, userSize, choice);
			break;
		}

Why is this here? You can replace this entire switch block with: pet_Alph_Sorter(cellsPointer, userSize, choice);
Last edited on
The user gets to pick how to swap it, its supposed to be sorted alphabetically
the swap choices are by name,species,breed, and gender.
oh yeah, i see what you're saying.
Topic archived. No new replies allowed.