C++ 一旦我有了字符串的长度,我该如何将长度数字捕获为一个整数变量,以便在其他地方使用?
下面是我失败的尝试,终端读取0作为“lengthCapture”。我试着用谷歌搜索答案,但毫无乐趣 我的文本文件名为'Boogiedy.txt',读作'Boogiedy,Boogiedy,Boooooo' 终端显示:C++ 一旦我有了字符串的长度,我该如何将长度数字捕获为一个整数变量,以便在其他地方使用?,c++,variables,capture,string-length,C++,Variables,Capture,String Length,下面是我失败的尝试,终端读取0作为“lengthCapture”。我试着用谷歌搜索答案,但毫无乐趣 我的文本文件名为'Boogiedy.txt',读作'Boogiedy,Boogiedy,Boooooo' 终端显示: Boogiedy, Boogiedy, Boooooo 27 0 我的代码: #include <iostream> #include <fstream> #include <string> using namespace std; in
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;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
弦乐;
int lengthCapture=boogiedy.length();
ifstream输入文件(“boogiedy.txt”);
if(inputFile.is_open())
{
while(getline(inputFile,boogiedy))
{
cout获取字符串大小时,变量仍然为空
试试这个:
#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;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
弦乐;
int lengthCapture=0;
ifstream输入文件(“boogiedy.txt”);
if(inputFile.is_open())
{
while(getline(inputFile,boogiedy))
{
cout获取字符串大小时,变量仍然为空
试试这个:
#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;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
弦乐;
int lengthCapture=0;
ifstream输入文件(“boogiedy.txt”);
if(inputFile.is_open())
{
while(getline(inputFile,boogiedy))
{
库特