以下代码运行后,如何在程序再次运行之前清除用户声明的变量 empName
After the following code runs, how do I clear the user declared variable empName before the program runs again?
我创建的以下程序采用一个字符串类型的数组,其值由员工姓名组成,然后由函数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
不得在程序中使用...
相关文章:
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 代码在main()中运行,但在函数中出现错误
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何在运行中期切换GTK CSS style_context
- 如何在MS Visual Studio 2019中运行QT UI
- 如何通过cpp程序运行shell脚本
- IPC使用多个管道和分支进程来运行Python程序
- 删除指向指针的指针是运行时错误吗
- 如何用参数值调用函数(仅在运行时已知)
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- c++中的指针和运行时错误
- 如何使用 Xcode 在运行时C++清除屏幕
- 以下代码运行后,如何在程序再次运行之前清除用户声明的变量 empName
- 向量:清除/pop_back不会运行析构函数
- 清除正在运行的进程 C++ 的堆内存