これはしばらくの間私に問題を与えてきました、そして私がコードに加えた調整は違いを生まないようです。ファイルから読み取ったテキスト行の数字を見つけて、後で使用できるようにその数字を別の文字列に保存しようとしています。最初のコピーは成功したように見えますが、数字が格納されている文字列を出力しようとすると、出力は空白行だけになります。
コードとインクルードヘッダーファイルは次のとおりです。
#include<iostream>
#include<string>
#include<fstream>
#include<cctype>
using namespace std;
int main()
{
ifstream inFile;
string temp;
short count = 0;
char fileName[20];
string info1;
cout << "Enter the name of the file to be used: " << endl;
cin >> fileName;
inFile.open(fileName);
if(!inFile)
{
cout << "Error opening file." << endl;
}
else
{
getline(inFile, info1);
cout << info1 << endl;
for(short i = 0; i < info1.length(); i++)
{
if(isdigit(info1[i]))
{
temp[count] = info1[i];
cout << temp[count] << endl;
count++;
}
}
cout << temp << endl;
}
inFile.close();
return 0;
}
そして、出力は次のとおりです。
Enter the name of the file to be used:
input.txt
POPULATION SIZE: 30
3
0
明らかに、それは期待通りに温度を出力していません。任意の支援やアドバイスをいただければ幸いです。