근본적으로 문제는 findLowest 함수가 내가 계획 한 일을하지 않는다는 것입니다. 나는 그것이 논리 오류라는 것을 알고 있지만 변수가 업데이트되지 않는 이유를 알 수없는 것 같습니다. 항상 기본값으로 인스턴스화됩니다.for 루프 용 논리 흐름
#include <stdafx.h>
#include <string>
#include <iostream>
using namespace std;
//Function prototypes
void findLowest(int regAccidents[], int arraySize, string regNames[]);
int getNumAccidents(string);
const int NUM_REGIONS = 5;
int main(){
string regionNames[NUM_REGIONS] = { "North", "South", "East", "West", "Central" };
int regionAccidents[NUM_REGIONS];
for (int i = 0; i < NUM_REGIONS; i++){//Populates the accident array
regionAccidents[i] = getNumAccidents(regionNames[i]);
if (regionAccidents[i] < 0)//checks to see if there are any accidents counts lower than 0
regionAccidents[i] = 0;
}
findLowest(regionAccidents, NUM_REGIONS, regionNames);
}
int getNumAccidents(string region){//returns the accidents for a specific region to be assigned to the regionAccidents[] array in main()
int regionAccidents = 0;
cout << "How many accidents for " << region << "? ";
cin >> regionAccidents;
return regionAccidents;
}
void findLowest(int regAccidents[], int arraySize, string regNames[]){ //used to determine what the lowest number of accidents is
int lowest = regAccidents[0]; //once that is found, update the lowRegion string of the accident location
string lowRegion = regNames[0];
for (int i = 0; i < arraySize; i++){
if (regAccidents[i] < regAccidents[i++]){
lowest = regAccidents[i];
lowRegion = regNames[i];
}
}
cout << "The region with the lowest amount of accidents is: " << lowRegion << endl;
cout << "The lowest number of accidents is: " << lowest << endl;
}
이이 문제를 보여줍니다 코드의 가능한 가장 작은 조각인가? 그렇지 않다면 무엇입니까? – ObscureRobot
아래쪽에는 findLowest 함수 만 있습니다. 사람들이 필요하다면 원래 프로그램에 대해 아무 것도 변경할 필요가 없기 때문에 내가 제공 한 전체 코드. – koodeta
자세한 정보를 제공해야합니다. 어떤 입력을 사용 했습니까? 이러한 입력을 감안할 때 결과는 무엇입니까? 어떤 예외? 어떤 컴파일러와 플랫폼을 사용하고 있습니까? 많은 정보를 놓치 셨습니다 –