문자열의 길이를 알고 나면 다른 곳에서 사용하기 위해 길이 숫자를 정수 변수로 캡처하려면 어떻게해야합니까?

네이선 스튜어트

아래는 실패한 시도이며 터미널은 'lengthCapture'로 0을 읽습니다. 나는 기쁨없이 대답을 검색해 보았다.

내 텍스트 파일의 제목은 'Boogiedy.txt'이고 'Boogiedy, Boogiedy, Boooooo'로 표시됩니다.

터미널은 다음과 같습니다.

Boogiedy, Boogiedy, Boooooo
27 
0

내 코드 :

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string boogiedy;
    int lengthCapture = boogiedy.length();

    ifstream inputFile ("boogiedy.txt");

    if(inputFile.is_open())
    {
            while (getline(inputFile,boogiedy))
            {
                cout <<boogiedy<< '\n';
                cout <<boogiedy.length() << endl;
            }
    }
    else
        cout << "file is not open" << '\n';

    inputFile.close();

    cout << lengthCapture << endl;
}
훌륭함

문자열의 크기를 얻을 때 변수는 여전히 비어 있습니다.

이 시도:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string boogiedy;
    int lengthCapture = 0;

    ifstream inputFile ("boogiedy.txt");

    if(inputFile.is_open())
    {
            while (getline(inputFile,boogiedy))
            {
                cout <<boogiedy<< '\n';
                cout <<boogiedy.length() << endl;
                lengthCapture += boogiedy.length();
            }
    }
    else
        cout << "file is not open" << '\n';

    inputFile.close();

    cout << lengthCapture << endl;
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관