I need to find a "char" in a string, so the code dose not do that purpose. The count++ dose not increment the way it should so the number of time say the leter "e" would appear in the string is 47 it should be 7 times. Dose anyone see what is missing in my code. thanks for any input.
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <limits>
#include <string>
using namespace std;
int main()
{
char ch;
int count = 0;
char buffer[] = "She comes from a broken home, a tree fell on it";
cout << "Enter a letter to find in the string : ";
cin >> ch;
cout << "\n"<<buffer <<endl;
for (int i = 0; i < (strlen(buffer)); i++)
{
if (buffer[i] == ch)
{
count++;
}
}
cout << "Your leter "<<ch<<" is in the string " <<count<<" times "<< " with string size of "<< strlen(buffer)<<endl;
/*(Lastname_Lab9_p1.cpp)
Write a program that accepts both a string and a single character from the user. The
program should then determine how many times the character is contained in the string.
(Hint: Search the string using the find(str, ind) method). The method should be
used in a loop that starts the index value at zero, and then changes the index to one value
past the index of where the char was last found.*/
system("pause");
}
This post has been edited by Omegaclass: 22 November 2009 - 01:00 AM

New Topic/Question
Reply




MultiQuote






|