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 realWords;

map realCount;

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: ";

for (const auto& item : realc) {

cout << item.first << ": " << item.second << endl; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To fix the code and ensure that values like 256 and 1625 are counted correctly we need to address the logic in the isReal function and also check how ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!