Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading a sortType that doesn't exists stops the program #2

Closed
1 of 3 tasks
alesbe opened this issue Apr 20, 2022 · 5 comments
Closed
1 of 3 tasks

Loading a sortType that doesn't exists stops the program #2

alesbe opened this issue Apr 20, 2022 · 5 comments
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed

Comments

@alesbe
Copy link
Owner

alesbe commented Apr 20, 2022

Describe your problem:

When you try to select a sorting algorithm that does not exists and load it, the program just stops and can't do anything. Restarting the program solves the issue.

Steps to reproduce:

  • Select a non-defined algorithm with the arrow keys and start pressing space

Possible fix (optional):

Make sure that sortType it's associated to a sorting algorithm in sf::Keyboard::Up and sf::Keyboard::Down events, in main.cpp

OS

  • Windows
  • Mac OS
  • Linux
@alesbe alesbe added bug Something isn't working help wanted Extra attention is needed good first issue Good for newcomers labels Apr 20, 2022
@ariajanke
Copy link
Contributor

There's great temptation to over-engineer a solution to this. I can handle this too I believe, if you would like me to.

@alesbe
Copy link
Owner Author

alesbe commented May 13, 2022

I was thinking about reusing Utils::getSortType() to solve this, right now the function returns a string with the name of the algorithm, used to display it when changing the algorithms with the arrows.

My first thought was to reuse it by checking if the length of the return (string) is higher than 2, because the number of algorithms will probably be less than 99, and use that as a validation when starting the sort process or changing the algorithm.

Also adding a different function or variable containing the number of algorithms will be another step into adding new algorithms.

I don't want to over complicate this, but maybe there are better approaches to solve this issue!

@ariajanke
Copy link
Contributor

I had similar thoughts of using a sentinel value, and then hiding that with a simple function to check. c:

Something similar to:

const std::string kNoSort = "<NO ALGORITHM>";

std::string Utils::getSortType(int sortType) {
	switch (sortType)
	{
	case 0:
		return "Bubble sort";
	// and further cases...
	default:
        	return kNoSort;
	}
}

bool Utils::hasNextSortType(int sortType) {
	return getSortType(sortType + 1) != kNoSort;
}


While in main.cpp

// Change sort type (increase)
case sf::Keyboard::Up:
	if (sortType >= 0 && Utils::hasNextSortType(sortType)) {
		// omitted...
	}
	break;

What are your thoughts?

@alesbe
Copy link
Owner Author

alesbe commented May 13, 2022

Awesome! Go ahead if you want

alesbe added a commit that referenced this issue May 13, 2022
Added quicksort for #3, and a fix for #2
@alesbe
Copy link
Owner Author

alesbe commented May 14, 2022

PR #5 fixed this issue, now you can't select a non-existent algorithm, closing this issue.

Thanks!

@alesbe alesbe closed this as completed May 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants