code problem

I am trying to write a function in which the user enters a sentence in a string. Then specifies a letter to count in the sentence. The function will serarch the array to see how many times the letter specified appears. This is what I have so far....any help?????

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{

string str;
int i,index = 0, count = 0;
int letter;
char ch = 0;


cout << "Enter a string: " << endl;
cin >> str;
cout << "Enter the character you wish to count: " << endl;
cin >> letter;


index = str.find(ch, letter);

for (i = ch; i < str.length(); i++)
{
ch = str.at(letter);
count = index+1;
index = str.find(ch,letter);
if (isalpha(letter))
count++;
}

cout << "The character " << letter << "was found " << count << " times" << endl;

return 0;
}
You have a pretty thoughtful start...

I suggest that you stay away from string::find() for now, and just count through the characters in the string yourself.

for (i = 0; i < str.length(); i++)

Use toupper() or tolower() to compare your letters.

Good luck!
Topic archived. No new replies allowed.