C++ 数组选择排序问题和输出

C++ 数组选择排序问题和输出,c++,arrays,binary-search,selection-sort,linear-search,C++,Arrays,Binary Search,Selection Sort,Linear Search,你好,我不确定我的代码出了什么问题。 选择排序可以工作,但当程序再次要求用户输入姓名时,他们找不到正确的人。有人能帮我吗?我不确定出了什么问题 编辑:我现在第二次请求输入时遇到的错误是“找不到名称”。我不明白为什么 图片如下: 这是我的完整代码: #include <iomanip> #include <string> #include <iostream> #include <fstream> using namespace std; //

你好,我不确定我的代码出了什么问题。 选择排序可以工作,但当程序再次要求用户输入姓名时,他们找不到正确的人。有人能帮我吗?我不确定出了什么问题

编辑:我现在第二次请求输入时遇到的错误是“找不到名称”。我不明白为什么

图片如下:

这是我的完整代码:

#include <iomanip>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

// GLOBAL CONSTANTS
const int NUM_NAMES = 20;

// FUNCTION PROTOTYPES
void getNames(ifstream &, string[], int[], int);
int linearSearch(const string[], int, string);
int binarySearch(const string[], int, string);
void selectionSort(string[], int[], int);
void displayData(const string[], const int[], int);
void displaySearch(const string[], const int[], int);

int main()
{
// LOCAL VARIABLES
string names [NUM_NAMES];
int marks [NUM_NAMES];
ifstream inStream;              // Input file stream variable
int index = 0;
string searchWho;

// FUNCTION CALL 1
getNames (inStream, names, marks, NUM_NAMES);
cout << endl;
cout << " Students and Marks:" << endl;
cout << " ______________________________" << endl << endl;
// FUNCTION CALL 2: DisplayData
displayData(names, marks, NUM_NAMES);
// OUTPUT - Perform search - USER INPUT
cout << endl;
cout << " Please enter the first and last name of who who want to look up, seperated with a space." << endl << endl;
cout << " "; cin >> searchWho;
cout << endl << endl;
// FUNCTION CALL 3: linearSearch
index = linearSearch (names, NUM_NAMES, searchWho); 
// FUNCTION CALL 4: displaySearch
displaySearch(names, marks, index);
// FUNCTION CALL 5: selectionSort
selectionSort (names, marks, NUM_NAMES);
cout << endl;
cout << " Students and Marks:" << endl;
cout << " ______________________________" << endl << endl;
displayData(names, marks, NUM_NAMES);
// OUTPUT - Perform search - USER INPUT
cout << endl;
cout << " Please enter the first and last name of who who want to look up, seperated with a space." << endl << endl;
cout << " "; cin >> searchWho;
cout << endl << endl;
// FUNCTION CALL 4
index = binarySearch (names, NUM_NAMES, searchWho);
displaySearch(names, marks, index);

cout << " "; return 0;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// FUNCTION 1:      getNames
// DESCRIPTION:     Function opens data file students.txt
//                  Function reads data from students.txt and stores data
//                  appropriately according to customerCode and utilityCharge
//                  in parallel arrays Customer[] and Charge[]
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void getNames (ifstream &inStream, string names[], int marks[], int numElts)
{
    // Open input file
inStream.open ("students.txt");
string studentNames;            // Student names - Last name followed by first
int studentMarks = 0;           // Student mark for text/exam

// Read in data from students.txt
while (!inStream.eof())
{
    for(int count = 0; count < numElts; count ++)
    {
        inStream >> names[count];
        inStream >> marks[count];
    }

}
inStream.close();
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  FUNCTION: LinearSearch
//  DESCRIPTION: 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int linearSearch (const string names[], int numElts, string who)
{
int index = 0;      // Used as a subscript to search array
int position = -1;  // To record position of search value
bool found = false; // Flag to indicate if value was found

 while (index < numElts && !found)
{
    if (names[index] == who) // If the name is found 
    {
        found = true; // Set the flag 
        position = index; // Record the value's subscript
    }
    index++; // Go to the next element
}
return position; // Return the position, or -1
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  FUNCTION: SelectionSort
//  DESCRIPTION: 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void selectionSort(string names[], int marks[], int numElts)
{
int startScan;
int minIndex;
int startName;

for (startScan = 0; startScan < (numElts - 1); startScan++)
{
    startName = startScan;
    for (int minIndex = startScan + 1; minIndex < numElts; minIndex++)
    {
        if (names[minIndex] > names[startName])
        {
            startName = minIndex;
            string tempString = names[startScan];
            names[startScan] = names[minIndex];
            names[minIndex] = tempString;

            // Aligning arrays
            int tempInt = marks[startScan];
            marks[startScan] = marks[minIndex];
            marks[minIndex] = tempInt;
        }
    }

}
}

int binarySearch(const string names[], int numElts, string who)
{
int first = 0;              // First array element
int last = numElts - 1;     // Last array element
int middle;                 // Mid point of search
int position = -1;          // Position of search value
    bool found = false;         // Flag

while (!found && first <= last)
{
    middle = (first + last) / 2;     // Calculate mid point

    if (names[middle] == who)      // If value is found at mid
    {
        found = true;
        position = middle;
    }
    else if (names[middle] > who)  // If value is in lower half
    {
        last = middle - 1;
    }
    else
    {
        first = middle + 1;           // If value is in upper half
    }
}
return position;
}


void displayData(const string names[], const int marks[], int numElts)
{

// OUTPUT
for (int count = 0; count < numElts; count++)
{
    cout << " " << left << setw(15) << names[count] << right << setw(15) << marks[count] << endl;
}
}

void displaySearch(const string names[], const int marks[], int index)
{   
if (index == -1)
{
    cout << " Name not found. Restart the program to search again." << endl << endl;
}
else
{
    cout <<  names[index] <<  " scored " <<  marks[index] << " marks." << endl << endl;
}
}
#包括
#包括
#包括
#包括
使用名称空间std;
//全局常数
const int NUM_NAMES=20;
//功能原型
void getNames(ifstream&,string[],int[],int);
int linearSearch(常量字符串[],int,字符串);
int二进制搜索(常量字符串[],int,字符串);
void selectionSort(字符串[],整数[],整数);
void displayData(常量字符串[],常量整数[],整数);
void displaySearch(常量字符串[],常量整数[],整数);
int main()
{
//局部变量
字符串名[NUM_names];
整数标记[NUM_name];
ifstream inStream;//输入文件流变量
int指数=0;
字符串搜索谁;
//函数调用1
getNames(流内、名称、标记、NUM_名称);
cout从共享图像中,数组没有正确排序。例如

艾利森,杰夫45
柯林斯,比尔80
艾伦,吉姆82

如以下代码所示:

if (names[minIndex] > names[startName])
{
        startName = minIndex;
        //...
}
startName
表示
names
数组中最大名称的索引,不是吗?(数组是按降序排序的,对吗?) 因此,假设将索引
startName
处的最大名称与索引
startScan
处的当前扫描名称进行交换

startName = startScan;
for (int minIndex = startScan + 1; minIndex < numElts; minIndex++)
{
    if (names[minIndex] > names[startName])
    {
        startName = minIndex;
    }
}

string tempString = names[startScan];
names[startScan] = names[startName];
names[startName] = tempString;

// Aligning arrays
int tempInt = marks[startScan];
marks[startScan] = marks[startName];
marks[startName] = tempInt;

if(名称[minIndex]
您可以添加几行“students.txt”文件吗?
if (names[minIndex] > names[startName])
{
        startName = minIndex;

}
if (names[minIndex] < names[startName])
{
        startName = minIndex;
}