can someone please explain this

I am new to c++, about 3 months into it. I asked this question on another forum and when I asked for an explanation I was informed I didn't need to understand. Anyway, the code posted below works but I have no understanding of <algorithm> and need to know another way of doing this. My project is to write a program to accept from the user one line of text and count and output the number of lowercase letters. I have been reading and trying to figure out how to do this for the greater part of the day, finally broke down asked online and got an answer way over my head. The part I don't understand should be in bold. Thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
	
	string text;
	int count = 0;
	cout << " Enter one line of text: ";
		
	if(getline(cin, text))
	{
        size_t count = count_if(text.begin(), text.end(),
               [](unsigned char ch) { return islower(ch); });
        cout << count << "\n";
    }
}
		
You can look up what count_if means in the reference section.

That other piece [](unsigned char ch) { return islower(ch); } is a lambda function. It's basically a function you can declare in place so you don't have to give it a name.

In this case, I'm not sure why they didn't just pass islower to count_if directly:
size_t count = count_if(text.begin(), text.end(), islower);
I have no idea what a lambda function is, other than the definition you just gave. My instructor gives bits and pieces of code and expects us to be able to sub our parts in as needed. I am at a loss. Is there another way you can show me how to do this? After the if(getline(cin, text)), how can I test for lowercase letters. I'm really honestly trying to learn this and not asking for you or anyone to do this for me. This is what I was given by my instructor to complete an 8 part assignment, this question is the first part.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <fstream>
#include <iostream>
#include <cctype>

using namespace std;

ifstream fin;
char ch;
int count = 0;
fin.open("myfile.txt"0);
while(!fin.eof())
{
fin >> ch;
if('a' <= ch && ch <='z')
count++;



I'm not sure what to do with this. I've tried using everything i know, which isn't much, but can not figure this out. Part 2 of the project is reading from a file which is what is confusing me. It seems this is what "his code" is wanting me to do. I tried using fout to output to a file but my lack of knowledge is hindering progress. Any shove in the correct direction would be greatly appreciated.
Hi:

I'm usually the one asking questions on here but I sincerely tried to help you.
I'm not so good at c++ either but I wrote a little thing it appears to work I just tested it once. It doesn't use any of that code you were confused on it just uses a function called size or length both are the same. I used size here it tells you the what it the name says "the size". I went here http://www.asciitable.com/ and got the values for lower case letters which I used in the loop. I hope that this works properly. If it doesn't I apologize like I said I'm usually the one asking questions :)
Let me know how it works.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
//Gone
using namespace std;




int main()
{
	
	string text;
	int count = 0;
	cout << " Enter one line of text: ";
	
	getline(cin,text);
	
	for( int i = 0; i < text.size(); i++)
	{
		if(text[i] <= 122 && text[i]>= 97)
			count++;
	}
	
	cout << count;
	// We use this in class to pause the screen
	system("pause");
}
Hi;

I don't understand your post involving
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <fstream>
#include <iostream>
#include <cctype>

using namespace std;

ifstream fin;
char ch;
int count = 0;
fin.open("myfile.txt"0);
while(!fin.eof())
{
fin >> ch;
if('a' <= ch && ch <='z')
count++;


Can you be more specific about what is troubling you?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <fstream>
#include <iostream>
// I don't know why that was here.
using namespace std;

int main()
{
     ifstream fin; //This declaring fin. ( I think)
     char ch;
     //int count = 0;  
     fin.open("myfile.txt"); //This opens up a file.
  while(!fin.eof()) //This keeps the loop going "while, not end of file" 
 {
     fin >> ch;
     if('a' <= ch && ch <='z')
     //I added this below just to see if it was working.
	 cout << ch << endl;
	//count++;   
}
system("pause");
}


I tried to explain this if anyone see's anything I said wrong correct it. I explained to the best of my ability.

So you need to create a resource file for this and title it the same as the file you are opening above." Resource files" is on the left where all the other crap is like (project name).cpp. Right click add, new item. You will see a list of crap like c++ file.cpp. If you have one that say's .txt after use that and type the name in. If you don't have it then just type the name and type .txt after or type it from the start. I don't have it so I just type .txt.
I took one of the libraries it didn't do anything to my knowledge. Also you don't need count because the loop isn't updating it's using !fin.eof so that thing is going to run until the end of the file.


I don't know what the deal was with your actual question so I tried to just explain as much as I can.
This sample code was previously posted:
1
2
    if ('a' <= ch && ch <='z')
        count++;

That is part of a solution.
All you need to do is step through the input string one character at a time. use the above code to accumulate the total for each character ch.
Let's say your input is string text;, then each individual character can be referred to like this: text[n], use this instead of ch in the code above.
Note, n is the subscript, it should be varied using a loop, from 0 to one less than text.length()


Topic archived. No new replies allowed.