Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Is it possible to separate this code into the functions that I have begun creati

ID: 3577423 • Letter: I

Question

Is it possible to separate this code into the functions that I have begun creating down at the bottom of the program? I am having issues splitting it up into divided functions.


18 #include <iostream>
19 #include <iomanip>
20 #include <string>
21 #include <fstream>
22 #include <ctype.h>
23 #include <sstream>
24
25 using namespace std;
26
27 void chooseFile();
28 void openFile();
29
30 int i = 0,j = 0,k = 0;
31 double line=0;
32 string lines[1000];
33 string word;
34 ifstream ifs;
35 string concord[1000];
36 char file[20];
37 int count[1000] = {0};
38 int idx = 0;
39
40 int main()
41 {
42 chooseFile();
43
44 //openFile();
45 string w;//temparary string
46
47 cin.get(file,100);
48 ifs.open(file);
49 if(!ifs.is_open())
50 {
51 cout<<"Unable to open file"<<endl;
52 }
53 while(ifs.good())
54 {
55
56 getline(ifs,word);//read line by line
57
58 //removing punctations and converting upper case letters to lower case
59
60 for(int i=0; i<word.length(); i++)
61 {
62
63 if(!isalpha(word.at(i)))
64 {
65
66 if(word.at(i) != ' ')
67 for(int j = i; j < word.length()-1; j++)
68 {
69 word.at(j)=word.at(j + 1);
70 }
71
72 if(i == word.length()-1)
73 word.at(i) = ' ';
74 }
75
76 if(!islower(word.at(i)))
77 {
78 word.at(i) = tolower(word.at(i));
79 }
80 }
81
82 istringstream iss(word);
83
84 while(iss >> w) //read each word in each line
85 {
86 bool flag = false;
87 int matchedAt = -1;
88
89 for(int k = 0; k < idx; k++)
90 {
91 if(concord[k] == w)
92 {
93 flag = true;
94 matchedAt=k;
95 }
96 }
97
98 if(flag != true)
99 {
100 concord[idx]=w;
101 count[idx]=1;
102 stringstream num;
103 num<<line+1;
104 lines[idx]=num.str();
105 idx++;
106 }
107 else
108 {
109 count[matchedAt]++;
110 stringstream numb;
111 numb<<line+1;
112 lines[matchedAt]=lines[matchedAt]+","+numb.str();

113 }
114 }
115
116 line=line+1;
117 }
118
119
120 //print concord
121
122 cout<<std::left<<setw(15)<<"Word"<<setw(3)<<" : "<<"count"<<setw(3)<<" : "<<"Occurences"<<endl;
123
124 cout<<"----------------------------------------------------------------------------"<<endl;
125
126 for(int l = 0; l < idx; l++)
127 cout<<std::left<<setw(15)<<concord[l]<<setw(5)<<" : "<<count[l]<<setw(3)<<" : "<<lines[l]<<endl;
128
129 //print();
130
131 return 0;
132
133 }
134
135 void chooseFile()
136 {
137 char file[20];
138 cout<<"Enter the text file name: "<<endl;
139 //read file name
140 }
141
142 void run()
143 {
144
145 }
146
147 void print()
148 {
149
150 }
151
152 void openFile()
153 {
154
155
156 }

  

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <ctype.h>
#include <sstream>

using namespace std;

void chooseFile();
void openFile();
void print();

int i = 0,j = 0,k = 0;
double line=0;
string lines[1000];
string word;
ifstream ifs;
string concord[1000];
char file[20];
int count[1000] = {0};
int idx = 0;

int main()
{
      chooseFile();
      openFile();
      print();
     return 0;

}

void chooseFile()
{
     char file[20];
     cout<<"Enter the text file name: "<<endl;
     //read file name
}

void run()
{
     string w;
     while(ifs.good())
     {
           getline(ifs,word);//read line by line

           //removing punctations and converting upper case letters to lower case

           for(int i=0; i<word.length(); i++)
           {

               if(!isalpha(word.at(i)))
               {

                    if(word.at(i) != ' ')
                      for(int j = i; j < word.length()-1; j++)
                      {
                         word.at(j)=word.at(j + 1);
                      }

                     if(i == word.length()-1)
                         word.at(i) = ' ';
                }

                if(!islower(word.at(i)))
                {
                     word.at(i) = tolower(word.at(i));
                }
            }

            istringstream iss(word);

            while(iss >> w) //read each word in each line
            {
                bool flag = false;
                int matchedAt = -1;

                for(int k = 0; k < idx; k++)
                {
                     if(concord[k] == w)
                     {
                          flag = true;
                          matchedAt=k;
                     }
                }

                if(flag != true)
                {
                    concord[idx]=w;
                    count[idx]=1;
                    stringstream num;
                    num<<line+1;
                    lines[idx]=num.str();
                    idx++;
               }
               else
               {
                    count[matchedAt]++;
                    stringstream numb;
                    numb<<line+1;
                    lines[matchedAt]=lines[matchedAt]+","+numb.str();

               }
           }

           line=line+1;
      }

}

void print()
{
      cout<<std::left<<setw(15)<<"Word"<<setw(3)<<" : "<<"count"<<setw(3)<<" : "<<"Occurences"<<endl;

     cout<<"----------------------------------------------------------------------------"<<endl;

     for(int l = 0; l < idx; l++)
          cout<<std::left<<setw(15)<<concord[l]<<setw(5)<<" : "<<count[l]<<setw(3)<<" : "<<lines[l]<<endl;

}

void openFile()
{
     cin.get(file,100);
      ifs.open(file);
      if(!ifs.is_open())
      {
          cout<<"Unable to open file"<<endl;
      }
      else
      run();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote