Weird Strings

I apologize for posting on the forum twice.

I am trying to organize the names to print out the order of their respective numbers from largest to smallest. For example, Jae (5), Chris (6), Amy (2), Happy (3), Joy (100). I would want the program to print out, Joy, Chris, Jae, Happy, and Amy. However, the computer prints out something really weird with the names. The program changes the names of the strings. For example, instead of printing out Jae, it may print out Jee, or Ame. Any suggestions to fix this problem?

Thank you for reading!

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
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std; 

int *growArray (int *p_talk, int *size)
{
	*size *= 2;
	int *p_new_talk = new int [*size];
	for (int i = 0; i < *size; ++i)
	{
		p_new_talk[i] = p_talk[i];
	}
	delete [] p_talk;
	return p_new_talk;
}

int main () 
{ 
	int next_element = 0;
	int size = 10; 
	int *p_talk = new int[size];
	string *p_name = new string[size]; 
	int intUserInput; 
	string stringUserInput; 
	cin >> stringUserInput; 
	cin >> intUserInput;

	while (intUserInput > 0)
	{
		if (size == next_element)
		{
			p_talk = growArray(p_talk, &size);
		}
		p_talk[next_element] = intUserInput;
		p_name[next_element] = stringUserInput; 
		next_element++;
		cin >> stringUserInput; 
		cin >> intUserInput; 
	}

	int biggest_index = 0; 
	int j = 0;
	int k = 0; 

	for (int k = 0; k < next_element + 1; k++)
	{
		for (int i = j; i < next_element + 1; i++)
		{	
			if (p_talk[biggest_index] < p_talk[i])
			{
				biggest_index = i;
			}

			if (i == next_element)
			{
				int intTemp = 0; 
				string stringTemp; 

				intTemp = p_talk[biggest_index];
				stringTemp = p_name[biggest_index]; 

				p_talk[biggest_index] = p_talk[j];
				stringTemp[biggest_index] = stringTemp[j]; 

				p_talk[j] = intTemp;
				p_name[j] = stringTemp; 
				biggest_index++; 
			}
		}
		j++; 
	}

	for (int i = 0; i < next_element + 1; i++)
	{
		cout << p_talk[i] << endl;
	}

	for (int i = 0; i < next_element + 1; i++)
	{
		cout << p_name[i] << endl;
	}

    return 0; 
}

Just use a bubble sort but make sure to sort the name alongside the number. I'm curious as to why you haven't used a structure to hold the data rather than two separate arrays.

Anyway something 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
	// next_element holds size of data held so
	// ive used that for the bubble sort.
	int temp;
	string temp2;

	for (int i = 0; i < next_element; i++)
		for (int j = 0; j < next_element - i - 1; j++)
			if (p_talk[j] > p_talk[j + 1])
			{
				// int
				temp = p_talk[j];
				p_talk[j] = p_talk[j + 1];
				p_talk[j + 1] = temp;
				// also sort the name
				temp2 = p_name[j];
				p_name[j] = p_name[j + 1];
				p_name[j + 1] = temp2;
			}


	for (int i = 0; i < next_element ; i++)
	{
		cout << p_talk[i] << endl;
		cout << p_name[i] << endl << endl;
	}

Michael
9
Paul
3
John
2
Sam
1
Kim
0

1
Sam

2
John

3
Paul

9
Michael
Softrix,

Thank you so much for your answer. I was thinking about using a structure. However, I didn't know how I would use it in this case. Would you please show me? That would be awesome. Also, any thoughts on why my strings were going crazy?

Best regards,
Jae Kim
Last edited on

Lines 47, 49, 75, 80 - Look at your FOR loops, you will go past the last element (+1).
Last edited on

Sorry, I later realised you asked about structure example. A structure and a vector would be ideal for this scenario... but this really depends on if this code is part of a assignment or you are just learning yourself, i.e. you couldnt use vectors yet if you havent covered them in your lessons :)

Anyway.. an example..

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

#include <iostream>
#include <string>
#include <vector>

struct Data
{
	int id;
	std::string name;
};

int main()
{

	// http://www.cplusplus.com/reference/vector/vector/
	std::vector<Data> Names;

	int id = 0;
	int element = 0;
	std::string name;

	while (true) 
	{
		std::cin >> id;
		if (id == 0)
			break;
		std::cin >> name;

		// create element
		Names.push_back(Data());	
		// populate it
		Names[element].id = id;
		Names[element].name = name;

		element++;
	}

	// if you want to sort, check out
	// http://www.cplusplus.com/reference/algorithm/sort/

	return 0;

}


But if you want to stick with you previous method using a structure, just allocate in the same way.. only thing that changes really is the way you access the data and assign the values.

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

#include <iostream>
#include <string>

struct Data
{
	int id;
	std::string name;
};

const int SIZE = 5;

int main()
{
	// allocate
	Data *Names = new Data[SIZE];

	for (int i = 0; i < SIZE; i++) {
		std::cin >> Names[i].id;
		std::cin >> Names[i].name;
	}

	// ... do other stuff.

	// free memory.
	delete[]Names;

	return 0;

}
@Softrix

I don't understand why you are saying sorry. My goodness, you've helped me out so much. I don't deserve that from you. Hahaha. Anyhow, I'll come back to this one I learned about vectors. Thank you so much!

Your welcome :)

Topic archived. No new replies allowed.