Using variables declared inside an if else construct later in the program results in an undeclared identifier error

StackOverflow https://stackoverflow.com/questions/20532962

Pregunta

This program is pretty self explanatory, so I won't really get into the purpose of what its for.

My main problem right now is on lines 82, 89, 95, and 101, I'm getting "Undeclared Identifier" errors for "arr" and "input" when i compile.

Is this because I declared them inside of an if else if construct, and if so, is there any way to get around this. Thanks for any help in advance!!!!

Here is the code

#include <iostream>
#include <string>
using namespace std;

template<class T> void selectionSort(T arr[], T num)
{
    int pos_min;
    T temp;

    for (int i = 0; i < num - 1; i++)
    {
        pos_min = i;

        for (int j = i + 1; j < num; j++)
        {
            for (arr[j] < arr[pos_min])
            {
                pos_min = j;
            }
        }


        if (pos_min != i)
        {
            temp = arr[i];
            arr[i] = arr[pos_min];
            arr[pos_min] = temp;
        }
    }
}

int main()
{

    char check = 'C';

    while (toupper(check) != 'Q')
    {
        char dataType;
        int num = 0;

        cout << "What kind of data do you want to sort?" << endl;
        cout << " For integer enter i, for string enter s, for character enter c. ";
        cin >> dataType;

        //User input dataType
        if (toupper(dataType) == 'I')
        {
            int arr[100];
            int input;
            cout << " You've chosen Integer dataType" << endl;
        }
        else if (toupper(dataType) == 'S')
        {
            string arr[100];
            string input;
            cout << " You've chosen String dataType" << endl;
        }
        else if(toupper(dataType) == 'C')
        {
            char arr[100];
            char input;
            cout << " You've chosen Character dataType" << endl;
        }
        else
        {
            cout << "Not a recognizable dataType. Shuting down..." << endl;
            return -1;
        }

        //User input # of num
        cout << "How many num will be sorted? ";
        cin >> num;

        for (int i = 0; i < num; i++)
        {
            cout << "Enter an input of the dataType you selected: ";
            cin >> input;
            arr[i] = input;
        }

        //Display user input
        cout << "The data as you entered it: ";
        for (int i = 0; i < num; i++)
        {
            cout << arr[i];
            cout << " ";
        }
        cout << endl;

        //Sort user input by calling template functon selectionSort
        selectionSort(arr, num);

        //Display sorted user input
        cout << "After sorting your data by calling selectionSort: ";
        for (int i = 0; i < num; i++)
        {
            cout << arr[i];
            cout << " ";
        }

        cout << endl;
        //Query user to quit or continue
    cout << " Would you like to continue? Enter 'Q'. Enter anything else to continue.";
    cin >> check;

    }



    return 0;
}
¿Fue útil?

Solución

It is because you declared them inside an if/else block. Once the block completes, these variable go out of scope and are no longer accessible.
One way around this would be to always read in the input as character data, then convert it into the specified type after the fact. See atoi for how to convert from char to int.

Otros consejos

A variable can never have unknown type. Even inside a template, the type of every variable is fixed for any particular instantiation.

Which suggests a solution. All the code that works on a variable with multiple types can be placed into a template function.

You may find the template syntax for passing an arbitrary length array of arbitrary element type useful:

template<typename T, size_t N>
void func1( T (&arr)[N] )
{
    //...
}

But you really don't even need to pass the array. Just pass a type, and use that type when creating the array inside the function.

template<typename T>
void process_it()
{
    T arr[100];
    T input;

    // now work on them
}

Either way, you'll need to call this function from inside all the if/else branches, where the exact type is known.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top