以下代码运行后,如何在程序再次运行之前清除用户声明的变量 empName

After the following code runs, how do I clear the user declared variable empName before the program runs again?

本文关键字:运行 清除 用户 empName 变量 声明 程序 代码      更新时间:2023-10-16

我创建的以下程序采用一个字符串类型的数组,其值由员工姓名组成,然后由函数sortArray((排序。

在此之后,它会要求用户输入员工姓名,以查看其是否在数组中。

搜索名称,输出告诉用户名称在

原始数组中的位置(如果找到(,或者如果未找到,则告诉用户找不到名称。

最后,我有一个 if, then 语句来询问用户是否要重新运行该程序,以便他们可以根据需要搜索另一个名称。

#include "stdafx.h"
#include <iostream>
#include <string> 
using namespace std;
//Function prototype
void sortArray(string[], int);
int binarySearch(string[], int, string);
const int SIZE = 20;
char input;
int main()
{
    //Program Description
    cout << "This program asks for a name, sorts the list, searches for a match in the list,nand lastly outputs whether there is a match and the position in the array it is found.nn";
    //Defined array 
    const int NUM_NAMES = 20;
    string names[NUM_NAMES] = {"Collins, Bill", "Smith, Bart", "Allen, Jim", "Griffin, Jim", "Stamey, Marty", "Rose, Geri", "Taylor, Terri", "Johnson, Jill", "Allison, Jeff", "Looney, Joe", "Wolfe, Bill", "James, Jean", "Weaver, Jim", "Pore, Bob", "Rutherford, Greg", "Javens, Renee", "Harrison, Rose", "Setzer, Cathy", "Pike, Gordon", "Holland, Beth" };
    //Variables
    string empName;
    int results;
    //Sort array first
    sortArray(names, NUM_NAMES);
    //Prompt for user input to enter an employee name 
    cout << "Please enter an employee's name (last name, first name): ";
    getline(cin, empName);
    //Search for name
    results = binarySearch(names, NUM_NAMES, empName);
    //If results contains -1 the name was not found.
    if (results == -1)
        cout << "nThat name does not exist in the array.n";
    else
    {
        //Otherwise results contains the subscript of the specified employee ID in the array.
        cout << "nThat name is found at element " << results;
        cout << " in the array.nn";
    }
    std::cout << "nAre you sure you want to quit?(y/n?): "; //Asks user if they want to rerun the program? 
    std::cin >> input; //Stores user's answer 
    cout << "n";
    if (input == 'n' || input == 'N') main(); //Run program again
    else return 0; //Exit Program
}
//*************************************************************
// Definition of function sortArray.                           *
// This function performs an ascending order selection sort on *
// array. size is the number of elements in the array.         *
//**************************************************************
void sortArray(string names[], int size)
{
    int startScan, minIndex;
    string minValue;
    for (startScan = 0; startScan < (size - 1); startScan++)
    {
        minIndex = startScan;
        minValue = names[startScan];
        for (int index = startScan + 1; index < size; index++)
        {
            if (names[index] < minValue)
            {
                minValue = names[index];
                minIndex = index;
            }
        }
        names[minIndex] = names[startScan];
        names[startScan] = minValue;
    }
}
//***************************************************************
// The binarySearch function performs a binary search on an     *
// integer array. array, which has a maximum of size elements,  *
// is searched for the number stored in value. If the number is *
// found, its array subscript is returned. Otherwise, -1 is     *
// returned indicating the value was not in the array.          *
//***************************************************************
int binarySearch(string names[], int size, string value)
{
    int first = 0,         //First array element
    last = size - 1,       //Last array element
    middle,                //Mid point of search
    position = -1;         //Position of search value
    bool found = false;    //Flag
    while (!found && first <= last)
    {
        middle = (first + last) / 2;     //Calculate mid point
        if (names[middle] == value)      //If value is found at mid
        {
            found = true;
            position = middle;
        }
        else if (names[middle] > value)  //If value is in lower half
            last = middle - 1;
        else
            first = middle + 1;          //If value is in upper half
        }
    return position;
}

程序格式不正确;程序不应调用 main()

basic.start.main/3 函数main不得在程序中使用...