Thread: Very simple string help needed

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    120

    Very simple string help needed

    Hey guys,
    I am doing an excersise from a c++ book (not for school) and am having a problem. What I am trying to do is to have the user enter a string, then enter a letter. After the letter is entered the program should tell you how many times that letter appeared in the string. Example- the user types in "hello" then the user typed in 'l' for the letter. The program should say that 'l' appears 2 times. no matter what string i type and no matter what letter i want it to find it will always print -1. WHY?

    Code:
    #include <iostream>
    #include <string>
    using namespace std;	
    
    int main ()
    	
    {
    	string message;
    	int j;
    	
    
    	cout << "Enter a String!\n";
    
    	getline(cin, message);
    
    	string SearchLetter;
    
    	cout << "Enter a letter to be searched\n ";
    	getline(cin, SearchLetter);
    
    	j = message.find("SearchLetter");
    
    	cout << "The number of times that letter appears in the string is " << j << endl;
    
    	
    
    	system ("pause");
    	return 0;
    }
    *Edit* I got rid of the quotes in around the "SearchLetter" in the message.find() function, and now it will give me different answers but it is the wrong answer!?
    Last edited by nick753; 02-13-2010 at 05:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM