Very simple string help needed

This is a discussion on Very simple string help needed within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I am doing an excersise from a c++ book (not for school) and am having a problem. What ...

  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 04:09 PM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    The double quotes indicates that it's a string, so obviously that's wrong.
    Getting rid of them is a step in the right direction as it will send the variable of that name.
    However, whatever did you think the find function would do? It finds the first instance of the string you pass in the string object it's a member of. It does absolutely no such thing as find number of instances it occurs within a string. However, the find function should have an overloaded version which specifies where to begin searching. Also consider the possibility that there is not any occurrence of the letter, or substring, in the entered message.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    OK, you are correct that you need to remove the quotes, so that it checks the variable named SearchLetter instead of the actual literal string "SearchLetter".

    Next, the return value of "find" is
    Quote Originally Posted by http://www.cplusplus.com/reference/string/string/find/
    The position of the first occurrence in the string of the searched content.
    So if the string is "test", and we look for character 't', it will return 0, because the first 't' is at index 0. Recall that arrays start at 0, not 1. If the string was "a test", looking for 't' would return 2, since thats the index of the first 't'.

    So you see problem that this only returns the first instance of whatever character you're looking for. You'd have to continue "find"ing the next occurrence of the character, by repeatedly calling "find". You can pass a second parameter to "find", stating where to start searching from. So on a string "test", the first find for 't' will return 0. You can then call it again, starting at one character past where the last 't' was found, so in this case it would be at 0 + 1 = 1
    Code:
    index = message.find(SearchLetter, 1);
    This would return the index of the second 't', which is 3.

    If you have done loops, in particular, "for" loops, it would probably be easier to loop over the characters in the string, and compare each character with SearchLetter, and have a counter to count the number of times it appears.

    Also, note that 't' is not the same as 'T'. So a search for 't' in "Test" will find only the 't' at the end.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Thanks for trying to explain to me how to fix my program. I understood all of what you are saying but I just do not know how I would make a loop and create a counter that will count how many times it finds the letter. I know what a for loop is and everything but just don't know how I would start on it in this instance? Thanks

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    Start with writing a flowchart. It should get you started.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    I Can't flowchart it right now as I don't have any flowcharting software. Any other advice on how to do the for loop would be greatly appreciated though.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    Pen and paper will do just fine. It's a very important concept for structuring the logic needed for a function and for an inexperienced programmer, it is a must.
    Pseudo code will also do. Remember that these are tools to help you structure the logic in your program--one of the most important fundamentals in making you a programmer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Ok I flowcharted it on paper. I am still as confused as before as to what to put in the for loop. please help?

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Have you thought about the alternative ways to do this? Are you planning on using a for loop to iterate over each character? If so, start with a simple for loop that iterates over the characters in the string, and print each character one by one.

    After you have that, change it so that it only prints characters that are equal to, say 't'.

    After that, change it so that it only prints characters that are equal to SearchLetter.

    After that, declare (and initialize) a variable, and increment it (add one to it) each time the character equals SearchLetter.

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Wow, I don't even know how to create a loop that will iterate over each letter of the sting. I guess I have no choice but to give up. Thanks for trying.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    Don't give up. Let's go over this.
    What do you have right now? Try putting in pseudo code.
    What does your program currently do and what do you intend it to do?
    How would you go about doing this in reality?
    If you were given a text and told to find the occurrences of a specific letter, how would you do it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    As mentioned, don't give up. If you give up on yourself this easily/quickly, then you wont ever accomplish anything. (Maybe harsh, but it is the truth.)

    Reading examples and the "manual" for each function is an extremely useful and wise thing to do. Check out either of these links, both of which achieve the same thing. If you pay close enough attention to it, then...well, you'll be glad that you did. Thats all Ill say.
    - operator[] - C++ Reference
    - at - C++ Reference

  13. #13
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Right now I have
    Code:
    #include <iostream>
    #include <string>
    using namespace std;	
    
    int main ()
    	
    {
    	string message;
    	int j, i;
    	
    
    	cout << "Enter a String!\n";
    
    	getline(cin, message);
    
    	char SearchLetter;
    
    	cout << "Enter a letter to be searched\n ";
    	cin >> SearchLetter;
    
    	j = message.find(SearchLetter, 0);
    
    	for(i = 0; i <= j; i ++);
    	
    
    	
    	cout << "The number of times that letter appears in the string is " << i  << endl;
    
    	
    
    	system ("pause");
    	return 0;
    }
    Currently my program finds the index of where the letter appears in the string.
    It's supposed to find out how many times the letter appears in the string.
    In reality I would count from 1 til however many times I see the letter.

  14. #14
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    First, try and understand the syntax of a for loop. It may seem weird at first, but it actually is easy to understand after you practice with them. There are tutorials on this site, one of which talks about loops (Cprogramming.com Tutorial: Loops). Give it a read. Your current for loop is (logically) incorrect.

    Next, see the 2 links I posted above. With those 3 links, and the other suggestions given above, you should be able to do this all.

  15. #15
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    I have been working on this small "simple" program for almost 4 hours and I still can't figure it out. Thanks anyway

Page 1 of 2 12 LastLast
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, 01:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21