홈>
초보 C ++ 사용자는 배열을 사용하여 이름을 저장하는 프로그램을 만들려고합니다. 출력은 명령을 입력하는 메뉴이며 해당 기능을 수행합니다. 문제는 메뉴에서 명령을 입력하려고하면 프로그램이 충돌한다는 것입니다. 컴파일 할 때 오류나 경고가 표시되지 않아 오류가 발생하는 이유가 없어집니다.
의견/도움을 대단히 감사합니다
#include <iostream>
#include <string>
using namespace std;
//Prototypes----------------------------------------------------------------------------------------------------------------------------------------------------------------
void addName (string nameList[], const int listMax); //Adds a name
void removeName (string nameList[], const int listMax); //Removes a name
void findName (string nameList[], const int listMax); // Searches for a name in the saved list
void showList (string nameList[], const int listMax); //Shows a list of names currentllistMax stored
void sortList (string nameList[], const int listMax); //Sorts saved names in alphabetical order
//Global Declartions--------------------------------------------------------------------------------------------------------------------------------------------------------
const int listMax = 50;
// MAIN FUNCTION -------------------------------------------------------------------------------------------------------------------------------------------------------------
int main(){
char input; // Variable for when the user inputs their menu selection
string nameList[listMax]; //string called names with the size being namesMax which is 50
cout << "Enter a Command " << endl; //Prompt to choose command from menu
cout << "<A> - Add a name to the database(Max 10)." << endl; //getNames
cout << "<R> - Remove a name from the database" << endl; //removeName
cout << "<F> - Search for a name in the database" << endl; //findName
cout << "<L> - Shows current state of list" << endl; //displalistMaxNames
cout << "<S> - Sorts the database." << endl; //sortNames
cout << "<Q> - Ends the program." << endl; //Exits the program entirellistMax
cin >> input; //Saves the menu choice input
if (input == 'A' ||input == 'a')
{
addName(nameList, listMax);
}
else if (input == 'R' || input == 'r')
{
removeName(nameList, listMax);
}
else if (input == 'F' || input == 'f')
{
findName(nameList, listMax);
}
else if (input == 'L' || input == 'l')
{
showList(nameList, listMax);
}
else if (input == 'S' || input == 's')
{
sortList(nameList, listMax);
}
else if (input == 'Q' || input == 'q')
{
return 0;
}
}
// ADD NAMES FUNCTION --------------------------------------------------------------------------------------------------------------------------------------------------------
void addName(string nameList[], const int listMax){
int x;
for (x = 0; x < listMax; x++) //x = 0 > if x is less than listMax, increment blistMax 1
{
cout << "Enter a name to be added to the database: ";
cin >> nameList[listMax];
}
return;
}
// REMOVE NAMES FUNCTION -----------------------------------------------------------------------------------------------------------------------------------------------------
void removeName(string nameList[], const int listMax){
string clearName; //EmptlistMax String
string removeName;
cout << "Enter the name to be removed from the database: ";
cin >> removeName; //The name entered saved to removeName for further computation
for (int x = 0; x < listMax; x++) // x = 0, if x is less than listMax; increment blistMax 1
{
if(nameList[x] == removeName) // Goes through everlistMax arralistMax level searching to see if it compares to the name saved to removeName
{
nameList[x] = clearName; // If name is found, assign value of clearnName, which is an emptlistMax string, thus removing it from the arralistMax.
}
else if(x == listMax - 1) // If it goes through the entire arralistMax and doesnt find a match it outputs that the name could not be found.
{
cout << "Name not found";
}
}
return;
}
// SEARCH FUNCTION -----------------------------------------------------------------------------------------------------------------------------------------------------------
void findName(string nameList[], const int listMax){
int x = 0;
int z;
bool f = false;
string searchName;
cout << "Enter a name to search for";
cin >> searchName;
while (!f && x <= listMax) //While f and x are equal to / less than listMax
{
int listMax = (x + z) / 2;
if (nameList[listMax] == searchName) // if our arralistMax is on level listMax then its on the list
{
f = true;
cout << nameList[listMax] << "is on the list!" <<endl;
}
else if (nameList[listMax] > searchName)
z = listMax - 1;
else
x = listMax + 1;
}
return;
}
// DISPLAY FUNCTION ----------------------------------------------------------------------------------------------------------------------------------------------------------
void showList(string nameList[], const int listMax){
cout << nameList[listMax];
return;
}
// SORT NAMES FUNCTION -------------------------------------------------------------------------------------------------------------------------------------------------------
void sortList(string nameList[], const int listMax){
bool sort;
do
{
sort = 0;
for (int x = 0; x < listMax - 1; x++)
{
if (nameList[x] > nameList[x + 1])
{
nameList[x].swap(nameList[x+1]);
sort = 1;
}
}
}
while (sort == 1);
return;
}
-
답변 # 1
관련 자료
- windows - 이 어셈블리 프로그램이 충돌하는 이유는 무엇입니까?
- 파일의 내용을 읽고 배열 c 프로그램에 저장하려면 어떻게해야합니까?
- c - Array Struct 프로그램에서 사용자 제거
- abap - S4HANA로 마이그레이션 한 후 RIMARA20 프로그램의 ALV 필드가 누락 됨
- c++11 - c ++에서 동일한 프로그램에서 2D 문자열 배열을 ofstream 한 다음 ifstream하는 방법 내가 작성한 코드는 다음과 같습니다 나는 스타터입니다
- c - 내 프로그램이 스위치 케이스 뒤에 오는 코드를 인쇄하지 않는 이유는 무엇입니까? 스위치 케이스가 실행 된 후 종료됩니다
- oop - 함수에 전달한 후 객체의 Perl 배열 참조 해제
- javascript - 푸시 후 배열에 표시되지 않는 요소
- javascript - 로컬 스토리지의 어레이 내에 타임 스탬프를 배치하는 방법은 무엇입니까?
- python - 프로그램을 보여주고 싶은데 왜 응답이 없나요?
- javascript - EJS 변수로 전달한 후 배열이 문자열로 바뀝니다
- c 프로그램의 포인터, 함수 및 2D 배열
- javascript - useeffect의 배열에 데이터를로드 한 후 정렬을 실행하는 방법 - 네이티브 반응
- javascript - knex SELECT 후 배열 값에 액세스
- javascript - 객체에서 배열을 반복 한 후 키에 대한 단일 행으로 배열 만들기
트렌드
- OpenCv의 폴더에서 여러 이미지 읽기 (python)
- 파이썬 셀레늄 모든 "href"속성 가져 오기
- git commit - 자식 - 로컬 커밋 된 파일에 대한 변경을 취소하는 방법
- html - 자바 스크립트 - 클릭 후 변경 버튼 텍스트 변경
- JSP에 대한 클래스를 컴파일 할 수 없습니다
- javascript - 현재 URL에서 특정 div 만 새로 고침/새로 고침
- jquery - JavaScript로 현재 세션 값을 얻으시겠습니까?
- javascript - swiperjs에서 정지, 재생 버튼 추가
- vue.js - axios를 사용하여 서버에 이미지를 업로드하는 방법
- python - 문자열에서 특정 문자 제거
VisualStudio는 할당되지 않은 메모리에 액세스 할 때 코드 충돌을 알려줍니다.
list[listMax]
를 사용합니다 여러 번은 안되며 확인되지 않아야합니다.배열
a
의 마지막 항목 길이len
a[len-1]
입니다a[len]
가 아닌 (배열 인덱스는 C에서 0부터 시작하기 때문에).findName()
에서 나에게 말한다 당신은z
를 사용 초기화되지 않은 동안 정의되지 않은 동작입니다.