Question: The comment explains what the following loop does. Use a function instead. ` ` ` / / Counts the number of spaces int spaces =

The comment explains what the following loop does. Use a function instead.
```
// Counts the number of spaces
int spaces =0;
for (int i =0; i input.length(); i++)
{
if (input.substr(i,1)==""){ spaces++; }
}
```
```
int count_char(const string& str, char c){
int count =0;
for (int i =0; i str.length(); i++){
if (str[i]== c){
count++;
}
}
return count;
}
int main()
{
string str;
getline(cin, str);
cout count_spaces(str) endl;
return 0;
}
```
\(\times \) Failed to compile
```
helper_function.cpp: In function 'int main()':
helper_function.cpp:24:12: error: 'count_spaces' was not declared in this scope
24| cout count_spaces(str) endl;
^ ~ ~ ~ ~ ~ ~
```
Note: Although the reported line number is in the uneditable part of the code, the error actually exists in your code. Tools often don't recognize the problem until reaching a later line.
The comment explains what the following loop

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Programming Questions!