'Wanting to add an invalid input "user input" if user uses a character that is not an integer
#include <stdio.h>
#include <stdlib.h>
void quicksort(int number[25], int first, int last) {
int i, j, pivot, temp;
if (first < last) {
pivot = first;
i = first;
j = last;
while (i < j) {
while (number[i] <= number[pivot] && i < last)
i++;
while (number[j] > number[pivot])
j--;
if (i < j) {
temp = number[i];
number[i] = number[j];
number[j] = temp;
}
}
temp = number[pivot];
number[pivot] = number[j];
number[j] = temp;
quicksort(number, first, j - 1);
quicksort(number, j + 1, last);
}
}
int main() {
int i, count, number[25];
printf("Enter some elements (Max. - 25): ");
scanf("%d", & count);
printf("Enter %d elements: ", count);
for (i = 0; i < count; i++)
scanf("%d", & number[i]);
quicksort(number, 0, count - 1);
printf("The Sorted Order is: ");
for (i = 0; i < count; i++)
printf(" %d", number[i]);
return 0;
}
This is my code so far. I am able to qsort the integers given by a user but not give an error message if uses inputs a character such as the letter p.
Solution 1:[1]
You can just check for invalid input and return. Like this:
for (int i = 0; i != '\0'; i++) {
if (!isdigit(number[i]) {
return;
}
}
Make sure to include <stdlib.h>
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | onapte |