두 개의 텍스트 파일이 있습니다.키워드를 사용하여 텍스트 파일에서 숫자를 반환하십시오.
파일 (1 명) 내용 : 애플 5 망고 (10) 오렌지 15
파일 2 명 내용 : 애플 10 망고 (15) 오렌지 (20)
I 파일 모두의 내용은 다음과 같을 키워드 (여기서는 과일의 이름)를 사용하고 임의로 파일 중 하나를 선택하고 해당 키워드에 해당하는 숫자 값을 반환하는 프로그램을 만들려고합니다. 아래는 제 코드입니다. 그러나이 프로그램을 실행하면 첫 번째 값만 표시되고 해당 값은 표시되지 않습니다. 내가 도대체 뭘 잘못하고있는 겁니까? 당신이 요청 된 값에 도달 할 때까지 순차적으로 파일을 읽을 수 있어야합니다 및 :
while (inResultFile >> sym >> value)
{
return value;
}
참고 :
double Fruit::Price(string & sym)
{
ifstream inResultFile;
string file_selected;
int choice;
string line;
/*choice = (rand()%2);
switch (choice)
{
case 0:
file_selected = "file 1.txt";
break;
case 1:
file_selected = "file 2.txt";
break;
}*/
inResultFile.open("file 1.txt", ios::in);
if (inResultFile.is_open())
{
double value=-1;
string name;
while (inResultFile >> name >> value)
{
cout<<name<<value;
if(name==sym)
return value;
}
}
else
cout << "Sorry, the file could not be openend." << endl;
return -1;
}
int main()
{
Fruit Obj;
string symbol;
double f_Price;
cout << "Enter a keyword to get the fruit price" << endl << endl;
cin >> symbol;
f_Price = Obj.Price(symbol);
cout << "The selected price of the input symbol is " << f_Price << endl;
return 0;
}
입니다 디버거에서 'value'의 가치는 무엇이라고 말 했나요? – UKMonkey
내가 망고에 들어갈 때 얻을 수있는 가치는 사과 즉 5 가치입니다. 다른 키워드를 사용해도 마찬가지입니다. –
그건 디버거가 아니기 때문에 프로그램을 실행하고 사용하려고합니다. 가서 읽어봐. https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – UKMonkey