C++ Class Help.

Using a text editor (i.e. Notepad), create a text file called Text1.txt and place it into a folder of your choosing.
Fill Text1.txt with a sentence and save the file.
Then, write a C++ program that performs the following:
1. Reads the sentence from the file Text1.txt and display it on the screen.
2. Asks the user to enter a lowercase letter (a-z) with the keyboard.
3. Count and display the number of times that the entered letter appears in the sentence read from the file.

This Is What I have so far, dont know how to finish it.

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

using namespace std;

int main()
{
ifstream inFile ("d:/Text1.txt", ios::in);
char sentence[80];

if(!inFile)
{
cerr<<"File Text1.txt could not be opened"<<endl;
exit(1);
}

inFile.getline(sentence,80);

cout<<endl;
cout<<"The file Text1.txt contains the following sentence:";
cout<<endl;
cout<<sentence;
inFile.close();
cout<<endl;

cout<<"Please enter a lowercase letter between (a-z):";
char letter;
cin>> letter;










return 0;

}
What you have left to do is to count how many times letter occurs in sentence.
Really dont how to that, that is my problem. Can you help?
So in real life if you have a sentence "hello world!" on a paper and you want to know how many times the letter l occurs in that sentence, how would you do that?
Last edited on
Need a word count, and maybe a while loop? I have tried different ways to get it but it wouldnt work.
Last edited on
Sounds like it could work. Try that!
Once again I have and still not working. Do you have any ideas to help me complete this program?
On a piece of paper, write down EVERY STEP you do to see how many times a letter is in a word. For instance:
look at first letter
  is this the letter I am counting?
    if it is, add to the count in my head
look at second letter
  is this the letter I am counting?
    if it is, add to the count in my head
[etc...]
Last edited on
Ok this is what I have so far. I can now input different letters from a to z, but my program isnt counting them and telling my how many "a" is in my sentence, or "b", etc, etc.

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

using namespace std;

int main()
{
ifstream inFile ("e:/Text1.txt", ios::in);
char sentence[80];

if(!inFile)
{
cerr<<"File Text1.txt could not be opened"<<endl;
exit(1);
}

inFile.getline(sentence,80);

cout<<endl;
cout<<"The file Text1.txt contains the following sentence:";
cout<<endl;
cout<<sentence;
inFile.close();
cout<<endl;

cout<<"Please enter a lowercase letter between (a-z):";
char letter;
cin>> letter;



char* ch_ptr;
int counter_letter = 0;
for (ch_ptr = sentence; *ch_ptr != '\0'; ++ch_ptr);

while (*ch_ptr==letter)
++counter_letter;

cout<< "The number of occurences of the letter " << letter << " is " << counter_letter << endl << endl;



return 0;

}
So far Im on a good track, but my program isnt counting how many "a", "b", through "z". Im completely suck here. HELP!!!! PLEASE!!!!
It is almost working. You have a semicolon after the for loop that shouldn't be there, and the while loop should be an if statement I think
Peter87 I want to say thank you for taking time out of your day to help me solve my program. I really appreciate it so much. This is the correction I have made from your last comment. I am almost done, but my program is still not showing me the number of occurrences of the letter, it keeps on giving me 0 each time, just not seeing why.

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

using namespace std;

int main()
{
ifstream inFile ("e:/Text1.txt", ios::in);
char sentence[80];

if(!inFile)
{
cerr<<"File Text1.txt could not be opened"<<endl;
exit(1);
}

inFile.getline(sentence,80);

cout<<endl;
cout<<"The file Text1.txt contains the following sentence:";
cout<<endl;
cout<<sentence;
inFile.close();
cout<<endl;

cout<<"Please enter a lowercase letter between (a-z):";
char letter;
cin>> letter;


char* ch_ptr;
int counter_letter = 0;
for (ch_ptr = sentence; *ch_ptr != '\0'; ++ch_ptr);

if (*ch_ptr==letter)
++counter_letter;

cout<< "The number of occurences of the letter " << letter << " is " << counter_letter << endl << endl;










return 0;

}
You forgot to remove the semicolon after the for loop.
If I do, the program would stop working if I remove the semicolon.
1
2
3
for (ch_ptr = sentence; *ch_ptr != '\0'; ++ch_ptr); // I'm talking about the semicolon here
if (*ch_ptr==letter)
	++counter_letter;


1
2
3
for (ch_ptr = sentence; *ch_ptr != '\0'; ++ch_ptr) // It will work much better without it
	if (*ch_ptr==letter)
		++counter_letter;
You are Awesome thank you, this program is so sensitive. I have one more question I need not to exit after I put in one function.
Topic archived. No new replies allowed.