提示:
将函数theSmallest()和fill_array()转换为模板函数。
使用3种不同的数据类型测试模板函数:int,float和char。

问题:
当我将其加载到编译器中时,没有错误。它适用于INT和Float;但是,当我为字符选项运行index_of_smallest函数时……不喜欢它。当我实际上用“ cout”检查字符调用期间min的值是什么时,它给了我“ -10002e89 ..”。该值永远不会改变,因此在for循环中index_of_min始终为0,因此编译器认为最小字符始终为0。

码:

#include <iostream>
#include <cstdlib>
using namespace std;
const int DECLARED_SIZE = 20;

template <class BaseType>
int index_of_smallest(BaseType a[], int size, int& number_used)
{
    BaseType min = a[0];
    int index_of_min = 0;
    cout << index_of_min<< endl;
    for (int index = 1 ; index < number_used; index++)
    {
        if (a[index] < min)
        {
            min = a[index];
            index_of_min = index;
        }
    }
    cout << index_of_min<< endl;
    return  index_of_min;
}

template <class BaseType>
void fill_array(BaseType a[], int size, int& number_used)
{
    int index = 0;
    BaseType ans;

    cout << "Enter up to " << size << " of the Data Type selected.\n"
         << "Mark the end of a list of numbers with a negative number.\n"
         << "Mark the end of a list of characters with the negative symbol \"-\". \n"
         << "Separate each input with a space. \n" << endl;
    cin >> ans;

    while (ans >= 0 && ans != '-')
    {
        a[index] = ans;
        index++;
        cin >> ans;
    }
    number_used = index;
}

void menu()
{
    int number_used, choice, smallest, result;
    int start_index = 0;
    char menuLoop = 'Y';

    while (toupper(menuLoop) == 'Y')
    {
        cout << "Please select a type of Input: \n"
             << "1. Integers (1, 2, 3, 4, etc..)\n"
             << "2. Decimals (1.0, 2.0, 3.0. 4.0, etc..)\n"
             << "3. Characters ( a, b, c, d, etc...)\n\n\n";
        cin >> choice;


        switch (choice)
        {
            case 1:
                int arrI[DECLARED_SIZE];
                system ("CLS");
                fill_array(arrI, DECLARED_SIZE, number_used);
                smallest = index_of_smallest(arrI, start_index, number_used);
                cout << "The smallest integer is: " << arrI[smallest] << " ";
                break;
            case 2:
                float arrF[DECLARED_SIZE];
                system ("CLS");
                fill_array(arrF, DECLARED_SIZE, number_used);
                smallest = index_of_smallest(arrF, start_index, number_used);
                cout << "The smallest float is: "<< arrF[smallest] << " ";
                break;
            case 3:
                char arrC[DECLARED_SIZE];
                system ("CLS");
                fill_array(arrC, DECLARED_SIZE, number_used);
                smallest = index_of_smallest(arrF, start_index, result);
                cout << "The smallest character is: " << arrC[smallest] <<" \n";
                break;
            default:
                cout << "That is an invalid choice! Exiting Program";
        }

        cout << "\n\nWould you like to return to the main menu? (Y/N): ";
        cin >> menuLoop;

        while(toupper(menuLoop) != 'Y' && toupper(menuLoop) != 'N')
        {
            cout << "Invalid Choice: Please type (Y/N): ";
            cin >> menuLoop;
        }

        system("CLS");
    }
}

int main()
{
    using namespace std;

    menu();
    system("PAUSE");
}

最佳答案

您的错误在这里:

smallest = index_of_smallest(arrF, start_index, result);


应该是:

smallest = index_of_smallest(arrC, start_index, number_used);


您使用了错误的数组,并且传递了result(从未初始化)而不是number_used

关于c++ - 使用模板函数进行int,float和char查找最小值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27118981/

10-11 22:47
查看更多