이 프로그램은 매우 자명하므로이 프로그램의 목적을 실제로 이해하지 못합니다.나중에 프로그램에서 if else 구문 내에서 선언 된 변수를 사용하면 선언되지 않은 식별자 오류가 발생합니다.
내 주된 문제는 현재 82, 89, 95 및 101 행에 있는데 컴파일 할 때 "arr"및 "input"에 대해 "선언되지 않은 식별자"오류가 발생합니다.
if if else if 구조체 내부에 선언했기 때문에 이것이 가능합니까? 그렇다면이 문제를 해결할 수있는 방법이 있습니다. 사전에 도움을 주셔서 감사합니다 !!!!
여기 당신이 경우/다른 블록 내부를 선언하기 때문입니다 코드
#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;
}
실제로 문제가있는 부분으로 미리보기를 제한해야합니다. – pyon
런타임에 사용자 선택에 따라 유형이 달라지는 변수 (즉,'array','input' 등)를 원한다고 생각합니다. 이것은 정적으로 (그리고 비 종속적으로) 유형화 된 언어 인 C++에서는 가능하지 않습니다. – pyon