Question
Need help fixing this code: Trying to read from an input file like this 2.5 0.2 -25.6 -25.60 2.5 2.5 +16.25 12.6.7 2.5 .5 5.
Need help fixing this code:
Trying to read from an input file like this
2.5 0.2 -25.6 -25.60 2.5 2.5 +16.25 12.6.7 2.5 .5 5. 0.7 16.25 -25.6 2.5
But its giving me an output of
List of real values and their number of occurrences: +16.25: 1 -25.6: 2 -25.60: 1 0.2: 1 0.7: 1 16.25: 1 2.5: 5
But -25.6 should have a value of 3 and 16.25 should have a value of 2
bool isReal(const string& word)
{ if (word.size() < 3) { return false; }
int i = 0, dots = 0;
if (word[0] == '+' || word[0] == '-') { if (word.size() == 1) { return false; } ++i; }
for (; i < word.size(); ++i) { if (!isdigit(word[i])) { if (word[i] == '.') { ++dots;
if (dots > 1) { return false; } } else { return false; } } } return dots == 1; }
vector
map
string line;
while (getline(in, line)) { string word; istringstream iss(line);
while (iss >> word)
{
if (isReal(word))
{ realWords.push_back(word); ++realCount[word]; } }
} cout << " List of real values and their number of occurrences:" << endl; for (const auto& w : realCount)
cout << w.first << ": " << w.second << endl;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started