2017-12-05 10 views
-1

CSV 파일의 데이터를 저장하고 핵심어를 검색하는 프로그램을 만들려고합니다. 나는이 코드를 가지고 있고, 표시 오류가없는,하지만 늘를 요청하는 프로그램의 과거를 실행 '단어를 입력 :'(? 여러 단어를) 정확한 문자열을 포함하는 std::cin을 얻기 위해C++ CSV 파일에서 단어 검색

#include <iostream> 
#include <string> 
#include <fstream> 
#include <vector> 
#include <sstream> 

using namespace std; 


int main(){ 

    vector<string> student(860); 
    vector<string> question(860); 
    vector<string> answer(860); 

    int count = 0; 
    string word; 

    ifstream file("data2.csv"); 

    if (!file.is_open()) 
     cerr << "File not found" << endl; 

    while (file.good()){ 

     for (int i = 0; i < 860; i++){ 

      getline(file, student[i], ','); 
      getline(file, question[i], ','); 
      getline(file, answer[i], ','); 
     } 
    } 

    cout << "Enter word: "; 
    cin >> word; 

    for (int j = 0; j < 860; j++){ 
     if (answer[j].find(word) != std::string::npos){ 
      cout << student[j] << question[j] << answer[j] << endl; 
      count++; 
     } 
    } 

    cout << count; 

    return 0; 
} 
+0

@JakeFreeman 그것은 무한 루프가 아니며 그는'Enter Word'를 실행한다고 !! 내 대답을 아래에서 읽으십시오 – organicoman

+0

@organicoman 좋아, 내가 문장을 잘못 읽었습니다 –

답변

0

그 사용자가 입력 한 경우 사용자가 지정한 두 개 이상의 단어를 지원하는 std::getline()을 사용할 수 있습니다. 이것이 당신이 원하는 것인지 아닌지 여부는 문제 진술로 명확하지 않습니다. 그렇지 않으면 당신이 찾고있는 모두가 한 단어의 입력이라면 나는 단지 std::cin을 사용하는데 문제가 없다. 응답 문자열을 검색하기위한 루프, 당신은 당신이 하드 코딩 값으로 반대 통해 반복하고있는 벡터의 크기에 내장 사용하는 것을 선호한다 당신의 또한

std::string match; 
std::cout << "Enter the exact string to search for: "; 
std::getline(std::cin, match); 
std::cout << "\nSearching for \"" << match << "\"" << std::endl; 

.

for (size_t j = 0; j < answer.size(); ++j) 
{ 
    std::size_t found = answer[j].find(match); 
    if (found != std::string::npos) 
    { 
     std::cout << "found \"" << match 
      << "\" starting at character " << found << std::endl; 
     count++; 
    } 
} 
std::cout << "occurrences of match criteria: " << count << std::endl; 
+0

고마워 이것이 도움이되었다! 나는 그것을 내 코드에 구현할 것이다. – JerryB

0

오류는 getline에서 처음 호출되는 쉼표까지 2 쉼표 때까지 두 번째 호출, 다음 새 라인에 (다음 학생의 과거 세 번째 쉼표로 3R 호출을 단어를 추출 이름).

는 그래서 getline 호출에서 다음 반복은 question으로 students 이름을 가져옵니다 후 다음 questionanswer, 총 엉망으로 다음 students 이름으로 answer! ;) 그래서 당신은 당신이 찾고있는 단어를 찾지 못할 것이므로 결과가 없습니다. 알 겠어?

것은이 문제를 해결하기 위해, 지난 getline 그래서 getline(file, answer[i]);

0

에, 나는이 문제를 해결하는 가장 좋은 방법은 아니라는 것을 알고 변경하려고합니다. 그러나 시간 위기에서 이러한 몇 가지 오류를 수정하고 시스템을 추가 할 수있었습니다 ("일시 중지"). 내 코드의 끝에서 결과를 볼 수 있도록

#include <iostream> 
#include <string> 
#include <fstream> 
#include <vector> 
#include <sstream> 

using namespace std; 


int main(){ 

    vector<string> student(860); 
    vector<string> question(860); 
    vector<string> answer(860); 

    int count = 0; 
    string word; 

    ifstream file("data2.csv"); 

    if (!file.is_open()) 
     cerr << "File not found" << endl; 

    while (file.good()){ 

     for (int i = 0; i < 860; i++){ 

      getline(file, student[i], ','); 
      getline(file, question[i], ','); 
      getline(file, answer[i], '\n'); 
     } 
    } 

    cout << "Enter word: "; 
    cin >> word; 

    for (int j = 0; j < 860; j++){ 
     if (answer[j].find(word) != std::string::npos){ 
      cout << student[j] << question[j] << answer[j] << endl; 
      count++; 
     } 
    } 

    cout << count << endl; 
    system("pause"); 

    return 0; 
}