Infinite display loop

I am writing a program that reads from a file a list of numbers and sorts them in ascending order.

Here is my text file

802 3110
801 7025
890 3120
870 3303
840 7028
863 7057
839 1214
836 7026
816 3113
832 7038

The way to read this, each line represents a class number at my school. So on the left side is the class number and the right side is the section number. The way this is going to get sorted is by the right side. For example, when the user choices a sort option it will display the results as

1214 839
3110 802
and so on

So far I have written a selectionSort function and a display function. The display function gets called inside the selectionSort. So when I test my program and it gives me an infinite loop because I am assuming its my display function in the selectionSort. I don't know why it gives me an infinite loop. Can somebody point out the error, I looked over my code dozens of times, especially the display function because I know that is where the error is, but I to me that function code seems fine.

Here is my 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
//test.cpp
#include <fstream>
#include "Selection.h"

int Menu();

int main()
{
	std::ifstream file("classes.txt");
	std::vector<int> values;
	int nums;
	int option;
	option = Menu();
	
	while (!file.eof())
	{
		file >> nums;
		values.emplace_back(nums);
	}
	
	file.close();

	do
	{
		switch (option)
		{
		case 1: SelectionSort(values);
		break;

		case 2:
	    break;

		case 3:
		break;

		case 4:
		break;

		case 5:
		break;

		case 6:
		break;

		case 7:
		break;

		default:std::cout << "Invalid input\n";
			break;
		}

	} while (option != 7);

	return 0;
}

int Menu()
{
	int choice;

	std::cout << "1.)Selection\n";
	std::cout << "2.)Bubble\n";
	std::cout << "3.)Insertion\n";
	std::cout << "4.)Heap\n";
	std::cout << "5.)Quick\n";
	std::cout << "6.)Merge\n";
	std::cout << "7.)Exit\n";
	std::cout << "Enter option:";
	std::cin >> choice;

	return choice;
}


1
2
3
4
5
6
7
//selectionSort.h
#pragma once
#include <vector>
#include <iostream>

void SelectionSort(std::vector<int>&);
void display(const std::vector<int>&);


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
//selection.cpp
#include "Selection.h"
#include "Swap.h"

void SelectionSort(std::vector<int>& values)
{
	int minValue;
	int size = values.size();

	for (int current = 0; current < size - 1; current++)
	{
		minValue = current;

		for (int i = 1; i < size; i++)
		{
			if (values.at(minValue) < values.at(i))
				minValue = i;
		}

		if (minValue != current)
			Swap(values.at(current), values.at(minValue));
	}

	display(values);

}

void display(const std::vector<int>& values)
{
	for (int i = 0; i < values.size(); i++)
		std::cout << values[i] << std::endl;
}
Last edited on
So I figured it out. I removed my do while loop and it worked, but I don't see why it would be the do while loop that cause the infinite loop.

Also, I can't seem to display it the way I want to.
Topic archived. No new replies allowed.