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

C++ 在以下代码运行之后,如何在程序再次运行之前清除用户声明的变量empName?,c++,C++,我创建的以下程序采用字符串类型的数组,其值由员工姓名组成,然后由函数sortArray排序 在此之后,它要求用户输入员工姓名,以查看其是否在数组中 搜索名称,输出告诉用户名称在原始数组中的位置(如果找到),或者如果未找到,则告诉用户未找到名称 最后,我有一个if,then语句询问用户是否希望重新运行程序,以便他们可以搜索其他名称(如果他们愿意) #include "stdafx.h" #include <iostream> #include <string> using

我创建的以下程序采用字符串类型的数组,其值由员工姓名组成,然后由函数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.\n\n";

    //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.\n\n";
    }

    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

主功能不得在程序中使用


循环在哪里?变量是全局变量吗?或者它们主要是申报的?如果它们是在main中声明的,它们只会重新分配它们自己,而您不必担心它。If/else不是一个循环。而且自己调用main的定义不太明确-不要这样做。这不是循环结构,而是使用if-else条件结构对主函数使用递归。你到底想在这里做什么?清除变量是什么意思?是否要将它们重置为某个默认值?还有,我们在这里讨论哪些变量?在if语句下应该有什么?你不必给出确切的答案,只要给我指出正确的方向就行了;;{/*重复逻辑在这里*/}。当用户表示要退出时,使用break语句退出循环。有关详细信息,请参阅您最喜欢的C++教材——这是真正的C++ 101的东西。