2017-11-30 1 views
0

두 개의 텍스트 파일이 있습니다.키워드를 사용하여 텍스트 파일에서 숫자를 반환하십시오.

파일 (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; 
    } 
+1

입니다 디버거에서 'value'의 가치는 무엇이라고 말 했나요? – UKMonkey

+0

내가 망고에 들어갈 때 얻을 수있는 가치는 사과 즉 5 가치입니다. 다른 키워드를 사용해도 마찬가지입니다. –

+1

그건 디버거가 아니기 때문에 프로그램을 실행하고 사용하려고합니다. 가서 읽어봐. https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – UKMonkey

답변

0

1) 다음과 같은 라인 sym (요청 된 과일)의 가치를 파괴 그 후에 당신은 그것을 돌려 줄 수 있습니다.

2) 당신은 결코 파일에서 가져온 값을 확인하지가 요청 된 과일인지, 단지 첫 번째! (또한 위의 라인에서 일어나야한다!)

+0

귀하가 언급 한대로 변경했습니다. 반면 (inResultFile >> >> 이름 값) \t \t \t \t { \t 경우 (이름 == SYM) \t \t \t 리턴 값; \t \t}. 그러나 출력은 -nan (ind)이됩니다. –

+0

@RestingPlatypus 아마도 'value'로 문자열을 읽습니다. double 값이 아니기 때문에, 'value'는 nan이됩니다. 'cout'을 사용하여 어떤 프로그램을 읽는지 디버깅하십시오. –

+0

@RestingPlatypus'-1'을 반환하면 파일 내용을 확인하십시오. –

0

당신이해야 정확한 값을 얻을려고 돌아 아래와 같은 과일의 비교를 수행합니다 -

string fruit = null; 
    while(inResultFile >> fruit >> value) 
    { 
    if(fruit == sym) 
     return value; 
    } 

당신의 방법의 말은 반환 값이 0이면 바로 과일 요를 의미 확인 아래 라인 주에서

else 
    cout << "Sorry, the file could not be openend." << endl; 
    return 0;//no fruit found 

를 사용 선택한 파일을 사용할 수 없습니다.

나는 아래 파일을 사용하고 있는데, 나를 위해 파일을 만들고있다. txt 입력 파일을 확인하십시오. 데이터 오류

class Fruit 
{ 
    public: 
    double Price(string & sym); 
}; 

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("file1.txt", ios::in); 
    if (inResultFile.is_open()) 
    { 
     double value=-1; 
     string name; 
     while (inResultFile >> name >> value) 
     { 
      cout<<name<<value<<endl; 
      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; 
} 

그리고 내 출력

망고
Apple5이어야합니다
Mango10
당신이 브레이크 포인트를 넣을 때 입력 심볼의 선택 가격은 그래서 10

+0

나는 이것을했다. 그러나 그것은 나에게이 이상한 결과를 준다. -nan (ind). –

+0

@RestingPlatypus 파일에서 읽은 것을 디버깅하십시오. 읽은 것을 확인하기 위해'cout << fruit << value << endl;'을 추가하십시오. 또한 함수가 반환하는 값을 출력하십시오. –

+0

@RestingPlatypus, 코드를 수정했습니다. 확인해주십시오. 또한 Bonje Fir의 조언을 따르십시오. –

관련 문제