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!!!!!!

#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 question do you have. What you have posted seems reasonable enough. Does it do what you think it should?

Do you the comparison of characters to be case sensitive (F is different from f) or not (F will count as f)? If the latter then you will want to use tolower() to convert each char to lower case if it isn't already before counting the number of fs in the example.

The problem is doing step 3 which is Count and display the number of times that the entered letter appears in the sentence read from the file.

I would decide how you are going to handle cases (F is, or not, equal to f) and start looping through the sentence array. While you are looping, take note of each time the given char appears. Each time you encounter a new line, take note of that (or just print it), and do any needed formatting.

The best way to do it is to have an integer variable, initialized to zero, which will hold the number of times the letter appears. You would then have a for() loop going from 0 to 79 (that is, it would continue while the index would be less than 80), and inside the loop you would have a conditional comparing the current array element in sentence to the letter . If the two characters match, then you would increment (add one to) the counter variable.

As an aside: you might want to use named integer constant for the number of characters you're reading in, rather than using a magic number the way you are now. It would make reading and changing the program easier, and it is a good habit to get into. For example, instad of what you have now, you could have written:

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cstdio>
#include<cstring>

using namespace std;

const int LINE_SIZE = 80;

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

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

    inFile.getline(sentence, LINE_SIZE);

    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;
}

(Note also the use of the code-tags, which allows the formatting of the code to be retained. You should always post you code with code tags on this forum.)

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;

}

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

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

Too many problems with that to try to fix it. Try something more like:

int length = strlen(sentence);
for(int i = 0; i < length; ++i)
 if the char in sentence with index i equals letter
   increment counter_letter

So replace it that part with yours?

So far Im on a good track, but my program isnt counting how many "a", "b", through "z". Im completely suck here. HELP!!!! PLEASE!!!!

Thank I understand, the program works great now but how can I have it start over instead of exiting after I input the first information?

Enclose the part you want to repeat in a loop. There are several types of loops available. Basically for repetetions a known number of times, "for" loops are usually used. For repetetions an indefinate number of times, "while" loops are usually used. If you aren't familiar with loops then I recommend you look them up in your favorite reference book/material. As much fun/useful this board may be, it doesn't and never will replace a good reference book/material.

#include <iostream>
using namespace std;
int main () {
    int cifre;
    cout<<"how many numbers has te binary number? "<<endl;
    cin>>cifre;
    int broj;
    cout<<"the number is: "<<endl;
    cin>>broj;
    int niz[cifre];
    for(int i=0;i<cifre;i++){
        niz[i]=broj%10;
        broj=broj/10;
    }
    double stepeni[cifre];
    for(int i=0;i<cifre;i=i+1){
        stepeni[i]=pow(2.0,i);    
    }
    int dekadni=0;
    for(int i=0;i<cifre;i++){
        if(niz[i]==1)
                dekadni=dekadni+stepeni[i];
    }
    cout<<dekadni<<endl;
    system("pause");
    return 0;
}
commented: If this is an attempt to answer the question, STOP CHEATING!!! If you are asking a question, this is not your thread -- start a new one! -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.