This has been giving me trouble for some time now and no adjustments I make to the code seem to make a difference.
I am attempting to locate the digits in a line of text read from a file, and store said digits into another string to be used later. the initial copying seems successful, but when attempting to output the string the digits are stored in, the only output is a blank line.
Here is the code and included header files:
#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;
}
And the output is as follows:
Enter the name of the file to be used:
input.txt
POPULATION SIZE: 30
3
0
Evidently, it is not outputting temp as expected.
Any assistance or advice would be appreciated.
Use this,
temp+=info1[i];instead of
temp[count] = info1[i];