Question
I NEED HELP FIXING THE ITERATIONS FOR THE MAP AS ITS NOT GIVING ME PROPER INPUT FOR THE FILES IM TESTING ONE OF THEM BEING
I NEED HELP FIXING THE ITERATIONS FOR THE MAP AS ITS NOT GIVING ME PROPER INPUT FOR THE FILES IM TESTING
ONE OF THEM BEING A FILE WITH JUST ALL NUMBERS. LIKE THE ONE BELOW
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
bool isInteger(string word) {
if (!isdigit(word[0]) && word[0] != '-' && word[0] != '+') return false;
for (int i = 1; i < word.length(); i++)
if (!isdigit(word[i])
return false;
//return true when all statements are passed
return true;
}
/* Creat a bool function that checks to see if a word is a Real number */
bool isReal(string word)
{
bool decimall = false;
int decimalc = 0;
if (!isdigit(word[0]) && word[0] != '-' && word[0] != '+') return false;
for (int i = 1; i < word.length(); i++) {
if (word[i] == '.') {
decimall = true;
decimalc++;
}
else if (!isdigit(word[i]))
//return false
return false;
}
return decimall && decimalc == 1;
}
bool isSpecial(string word) {
if (word[0] != '$')
return false;
if (!isalpha(word[1]))
//return false
return false;
for (int i = 2; i < word.length(); i++)
if (!isalnum(word[i]) && word[i] != '_')
//return false
return false;
//After all condition are met, return true
return true;
}
int main(int argc, char* argv[]) { //if argugement counter is not 2
if (argc != 2)
{ //print out NO SPECIFIED INPUT FILE
cout << "NO SPECIFIED INPUT FILE NAME." << endl;
//ends the program
return 0;
}
ifstream inFile(argv[1]);
//if not an infile
if (!inFile) {
//print out CANNOT OPEN THE FILE
cout << "CANNOT OPEN THE FILE " << argv[1] << endl;
//ends the program
return(0); }
map
map
map
string line;
while (getline(inFile, line)) {
stringstream ss(line);
/* Create an empty string word */
string word;
/* Create while loop that reads each word by whitespaces in the file line */
while (ss >> word) {
/* Checks if the word satisfies the bool function of isInteger */
if (isInteger(word)) {
/* Create a int named value that is equal to the conversion of the words in the file into int values */
int value = stoi(word);
/* Increment the int value as a key in the map integerc */
integerc[value]++; }
/* Else if statement checks if the word satisfies the isReal bool function */
else if (isReal(word)) {
/* Create a double named value that is equal to the conversion of the words in the file into doubles */
double value = stod(word);
/* Increment the double value as a key in the map realc */
realc[value]++;
}
/* Else if statement checks if the word satisfies the isSpecial bool function */
else if (isSpecial(word)) {
/* Increment the word as a key value in the map realc */
specialcc[word]++;
} }
}
/* If statement that checks if map integerc, realc and specialcc are empty */
if (integerc.empty() && realc.empty() && specialcc.empty())
{
//Print out File is empty
cout << "File is empty." << endl;
return 0;
}
/* print out the number of integers, real numbers and special word characters */ cout << "Number of integers in the file: " << integerc.size() << endl; cout << "Number of reals in the file: " << realc.size() << endl; cout << "Number of special names in the file: " << specialcc.size() << endl;
/* Create iterator "it" of type map with key and value being an int value */
map
/* Create iterator "itr" of type map with key being a double value and the value being an int */
map
/* Create iterator "itrr" of type map with key being a string value and the value of the key being an int value */
map
/* Create for loop that goes through each element in the map integerc */
for (it = integerc.begin(); it != integerc.end(); it++) {
cout << "List of integer values and their number of occurrences: " << endl;
/* Print out the key values and the values of map integerc */
cout << it->first << ": " << it->second << endl; }
/* Create for loop that goes through each element in the map realc */
for (itr = realc.begin(); itr != realc.end(); itr++) {
cout << "List of real values and their number of occurrences: " << endl;
/* Print out the key values and the values of map realc */
cout << itr->first << ": " << itr->second << endl; }
/* Create for loop that goes through each element in the map specialcc */
cout << "List of special names and their number of occurrences: " << endl;
for (itrr = specialcc.begin(); itrr != specialcc.end(); itrr++) {
cout << "List of special names and their number of occurrences: " << endl;
/* Print out the key values and the values of map specialcc */
cout << itrr->first << ": " << itrr->second << endl;
}
/* After the program is ran return 0 as the main program is done */
return 0; }
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