2014-02-28 2 views
-1

저는 C++의 초보자입니다. 재미있는 작은 C++ 코드를 구현하려고합니다.은 지정된 하위 문자열을 포함하는 모든 문자열을 인쇄합니다.

프로그램은 모든 친구들의 이름이 들어있는 텍스트 파일에서 입력을 읽습니다.

프로그램의 목적은/인쇄 지정된 편지/닉 이름으로 시작하는 모든 이름을 반환하는 것입니다

예를 들어

Nick 
Joseph 
Jack 
Robert 
Paul 
David 

I 입력 'J'는 결과가 조셉 할 필요가있는 경우 및 잭

내가 입력 'P'또는 'PA'결과는 하나의 논리 권리를 얻기 위해 나를 인도시겠습니까

해야합니다. 미리 감사드립니다.

감사합니다, 파반

+0

당신은 무엇을하려고 했습니까? –

+1

당신은 이미 기본 로직 세트를 가지고 있습니다. 당신은 a) 입력을 요구합니다. b) 입력을 받아들입니다. c) 문자열을 입력으로 검색합니다. d) 결과를보고합니다. –

+0

#include #include using namespace std; int main() { ifstream 입력; input.open ("words.txt"); char word [80]; char 출력; if (input.fail()) { cout << "파일이 없습니다."<< endl; cout << "exit program"<< endl; return 0; } while (! input.eof()) { input >> output; cout << 출력; } input.close(); return 0; } – user3366438

답변

1

를 Ths 논리은 다음과 같이 진행됩니다

read the desired prefix from user 
repeat 
    read one line from file 
    if the line starts with the desired prefix print it 
until there are no more lines 
+0

#include #include using namespace std; int main() {ifstream 입력; input.open ("words.txt"); char word [80]; char 출력; if (input.fail()) {cout << "파일이 존재하지 않습니다"<< endl; cout << "exit 프로그램"<< endl; 0을 반환; } while (! input.eof()) {input >> output; cout << 출력; } input.close(); 0을 반환; } – user3366438

+0

안녕하세요 @ 마이클, Logic에 대해 감사드립니다. for 루프를 사용하여 텍스트 파일의 모든 줄에서 사용자가 원하는 입력을 검색 할 수 있습니까? pls 도움! 아마 제발 코드를 안내해 주시겠습니까? 감사 – user3366438

0
#include <iostream> 
#include <fstream> 

using namespace std; 

int main() 
{ 
    ifstream input; 
    input.open("words.txt"); 
    char word[80]; 
    char output; 

    if (input.fail()) 
    { 
    cout << " the file doesnt exist" << endl; 
    cout << " exit program" << endl; 

    return 0 ; 
    } 

    while (!input.eof()) 
    { 
    input >> output; 
    cout << output; 
    } 
    input.close(); 

    return 0; 
} 
관련 문제