I try to obtain the minimum of two integers
, real numbers
, strings
, and integer arrays
by implementing functions that obtain similar treatment results as function overloading.
So I write code like this
#include <iostream>
#include <cstring>
using namespace std;
int GetMin(int a, int b);
double GetMin(double a, double b);
char* GetMin(char* a, char* b);
int GetMin(int* arr, int size);
int main()
{
int num1, num2;
double num3, num4;
char s1[30], s2[30];
int arr[10];
cout << "Enter two integers : ";
cin >> num1 >> num2;
cout << "The minimum value is " << GetMin(num1, num2) <<"\n";
cout << "Enter two real numbers : ";
cin >> num3 >> num4;
cout << "The minimum value is " << GetMin(num3, num4) <<"\n";
cout << "Enter two strings : ";
cin >> s1 >> s2;
cout << "The minimum value is " << GetMin(s1,s2) <<"\n";
cout << "Elements of an Array : ";
cin >> arr[10];
cout << "The minimum value is " << GetMin(arr[10], 10);
}
int GetMin(int a, int b)
{
if (a > b) return b;
else return a;
}
double GetMin(double a, double b)
{
if (a > b) return b;
else return a;
}
char* GetMin(char* a, char* b)
{
if (strcmp(a, b) < 0) return a;
}
int GetMin(int* arr, int size)
{
int min = arr[0];
for (int i = 1; i<size; i )
if (arr[i] < min)
min = arr[i];
return min;
}
Here's the desired result.
Enter two integers : 10 20
The minimum value is 10
Enter two real numbers : 56.84 120.26
The minimum value is 56.84
Enter two strings : orange apple
The minimum value is apple
Elements of an Array : 41 67 34 25 0 54 98 21 58 62
The minimum value is 0
I got this error message.
Run-Time Check Failure #2 - Stack around the variable 'arr' was corrupted.
CodePudding user response:
You have undefined behaviour, caused by this attempt to read into an index beyond the array:
cin >> arr[10];
I think you are trying to read the whole array in, but that is not possible like that. And if it were (e.g. by overloading >>
) it would be using arr
instead of arr[10]
.
You can solve this by overloading >>
(though I suspect that to be unnecessarily complex...). Or by reading in with a loop.
CodePudding user response:
It is wrong to use cin
to assign values to the entire array. The more common method is to use the for
or while
structure. I recommend you to read this
Warning C6201 Index '10' is out of valid index range '0' to '9' for possibly stack allocated buffer 'arr'.
The index of the array arr[10] should be arr[0]~arr[9]. About C6385 you should read this document.
char* GetMin(char* a, char* b)
{
if (strcmp(a, b) < 0) return a;
}
It is best not to use the parameters of the function as the return value. The common method is to define a variable to store the parameters to be returned.
CodePudding user response:
You can't input to an array like
cin >> arr[10];
Instead that inputs a single value into the eleventh element of your ten-element array (leading to undefined behavior).
You need to use a loop to input each element one by one:
for (unsigned i = 0; i < std::size(arr); i)
{
std::cout << "Enter input for element " << i 1 << ": ";
std::cin >> arr[i];
}
On an unrelated note, I recommend you learn more about the standard containers like std::array
and std::string
. They will make your life as a C programmer much simpler.