'Why is my function outputting a hex number instead of an int?
I want to do a bubble sort for my array but I got an infinite loop, so I checked if my CheckSorting function is working correctly and instead I found it's outputting my variable inside called "errors" into a hex number even though my function is an int type and the variable is type int. How do I make my function return an integer instead? Sorry if this isn't clear, I am very new to coding.
#include <iostream>
using namespace std;
int GetInteger();
int IntegerCheck(int min, int max);
int CountOccurences(int theArray[], const int arraySize, int value);
void InputUniqueValues(int theArray[], const int arraySize, int min, int max);
void SortArray(int theArray[], const int arraySize, int min, int max);
int CheckSorting(int theArray[], const int arraySize); // change back to bool
int main()
{
const int SIZE = 10;
const int MIN = 1;
const int MAX = 50;
int values[SIZE] {};
cout << "Please enter 10 unique numbers, and I will sort them for you.\n";
InputUniqueValues(values, SIZE, MIN, MAX);
CheckSorting(values, SIZE);
cout << CheckSorting << " errors in this order\n";
cout << "Your numbers are sorted.\n";
for (int i = 0; i < SIZE; i++)
{
if (i < SIZE - 1)
{
cout << values[i] << ", ";
}
else if (i == SIZE - 1)
{
cout << values[i] << "\n\n";
}
}
return 0;
}
int GetInteger()
{
int value = 0;
while (!(cin >> value))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
return value;
}
int IntegerCheck(const int min, const int max)
{
int input = 0;
do
{
input = GetInteger();
if (input < min || input > max)
{
cout << "*error!* YOU MUST ENTER A NUMBER BETWEEN " << min << " and " << max << endl;
}
}
while (input < min || input > max);
return input;
}
int CountOccurences(int theArray[], const int arraySize, int value)
{
int occurences = 0;
for (int i = 0; i < arraySize; i++)
{
if (theArray[i] == value)
{
occurences++;
}
}
return occurences;
}
void InputUniqueValues(int theArray[], const int arraySize, int min, int max)
{
int uniqueValuesEntered = 0;
int input = 0;
int occurences = 0;
//Keep looping until 10 unique values have been entered
while (uniqueValuesEntered < arraySize)
{
//Read in the number
input = IntegerCheck(min, max);
occurences = CountOccurences(theArray, uniqueValuesEntered, input);
//Is the number unique? If occurences == 0 then it is unique
if (occurences == 0)
{
theArray[uniqueValuesEntered] = input;
uniqueValuesEntered++;
}
else
{
cout << " * Error: " << input << " has already been entered, please enter a unique number. *\n";
}
}
}
void SortArray(int theArray[], const int arraySize, int min, int max)
{
InputUniqueValues(theArray, arraySize, min, max);
do
{
CheckSorting(theArray, arraySize);
for (int i = 0; i < arraySize; i++)
{
cout << CheckSorting << endl;
if (CheckSorting == 0)
{
break;
}
else
{
if (theArray[i] > theArray[i++])
{
int tempvalue = theArray[i];
theArray[i] = theArray[i++];
theArray[i++] = tempvalue;
}
}
}
for (int i = 0; i < arraySize; i++)
{
if (i < arraySize - 1)
{
cout << theArray[i] << ", ";
}
else if (i == arraySize - 1)
{
cout << theArray[i] << "\n\n";
}
}
}
while(CheckSorting > 0);
}
int CheckSorting(int theArray[], const int arraySize)
{
int errors = 0;
for (int i = 0; i < arraySize; i++)
{
for (int j = i; j < arraySize; j++)
if (theArray[i] <= theArray[j])
{
continue;
}
else if (theArray[i] > theArray[j])
{
cout << theArray[i] << " is > " << theArray[j] << endl;
errors++;
}
}
return errors;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
