Thread: How can i make a "letter could not be found" break?

  1. #1
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168

    How can i make a "letter could not be found" break?

    I've made a small vector, that holds single characters, and i've made a small function that iterates through the vector to find a letter the user inputs. It works fine if the letter is in the vector, but how could i make it say "letter could not be found" or something similar? here's my function if it's any use:

    Code:
    char search(vector<char> line, char letter)
    {
         char temp;
         vector<char>::const_iterator iter;
         iter = line.begin();
    
         while (*iter != letter)
         {
              if (*iter == letter)
              {
                   temp = *iter;
              }
              else
              {
                   iter++;
              }
         }
    
    return temp;
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
      if ( iter == line.end() )
        cerr<<"The letter you specified could not be found"<<endl;
    return temp;
    }
    Of course there are other problems (like what is the value of temp if the letter isn't found?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Initialize temp to some value that you know cannot be in the vector (like '\0'). If after the loop is done, the temp character is still that initial value, then you know the letter wasn't found.

    However, since you are just returning the letter that the user input, you don't need the temp variable at all. You can make temp a bool that is false and set it to true when you find the letter. At that point you can even break out of the while loop. Then, at the end of your function, check the bool variable and output the error if it wasn't found.

    Finally, good program design would probably actually have you just return true or false if the letter was found (or the index it was found at) and let the calling code output the error.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    while (*iter != letter)

    This is asking for a crash....

  5. #5
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    lol, well if it's asking for a crash then how else could i do it? with my limited experience it was the only way i could do it. Maybe using something like
    Code:
    for(iter = line.begin(); iter != end(); iter++)
    ??

  6. #6
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    you could just do
    Code:
    #include <algorithm>
    char search(vector<char> line, char letter)
    {
         temp =  std::find( line.begin(), line.end(), letter);
         if (temp == line.end() )// not found
              cout << "letter could not be found.\n"
         return *temp; //useless really since you have letter in letter
    but to be honest, you can ditch the search function and use find directly. especially since it was stated you are returning letter no matter what.

    Code:
    if (std::find( line.begin(), line.end(), letter)==line.end())
    cout << "letter could not be found.\n"
    //letter still equal letter
    Last edited by Darryl; 04-12-2005 at 11:06 AM.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    And instead of using vector<char> you can use std::string

  8. #8
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    alright thanks guys

    never used std::string before, so i think ill stick to what i know for the time being

  9. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Welshy, I think if you just take a few minutes to look at the capabilities of the string class, you'll immediately recognize its usefullness over C-strings.

  10. #10
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    but he's not using c-strings, he using a vector of chars. Now maybe he's storing sentences, which would be better in a string but maybe not.

  11. #11
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    I'm just learning how to use vectors and iterators, so i chose char for the simplicity factor. Line just stores a, b, c, d.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    for(iter = line.begin(); iter != line.end(); ++iter)

    That would be a good way to go. Make sure you determine whether you found the character or not by checking to see if the iter == line.end() after the loop or using a variable to remember that a match was found.

  13. #13
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    I've managed to get somewhere, in that it now recognises when a letter is or not found, but i get the positions as 97, 98, 99, 100, when the positions should be 0,1,2,3.

    Code:
    int search(vector<char> line, char letter)
    {
         int temp = 0;
         vector<char>::const_iterator iter;
    
         for(iter = line.begin(); iter != line.end(); iter++)
         {
                  if (*iter == letter)
                  {
                           temp = *iter;
                  }
                  else if (iter == line.end())
                       break;
         }
    return temp;
    }
    Code:
    int main()
    ....
                          cout << "What letter would you like to search for? ";
                          cin >> letter;
        
                          position = search(line, letter);
                          
                          if (position == 0)
                          {
                                   cout << "Sorry but that letter could not be found";
                          }
                          else
                          {
                                   cout << "\n\n" << letter << " is in position " << position;
                          }
    ....

  14. #14
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Well now you've change the requirements: The problem is that your function is returning the char and not the position.
    Code:
    int search(vector<char> line, char letter)
    {
         int temp = 0;
         int position = 0;
    
         for(; position <  line.size(); ++position)
         {
                  if (line[position] == letter)
                  {
                           temp = position+1;
                           break;
                  }
    
         }
    return temp;
    }
    
    int main()
    ....
                          cout << "What letter would you like to search for? ";
                          cin >> letter;
        
                          position = search(line, letter);
                          
                          if (position == 0) 
                          {
                                   cout << "Sorry but that letter could not be found";
                          }
                          else
                          {
                                   cout << "\n\n" << letter << " is in position " << position;
                          }
    ....
    Last edited by Darryl; 04-12-2005 at 01:42 PM.

  15. #15
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    Ah your a star Darryl, dont know how i didnt think of that before, i was nearly there. Have some rep points

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-19-2009, 07:19 PM
  2. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  3. Writing a program to make a calendar
    By Northstar in forum C Programming
    Replies: 17
    Last Post: 11-07-2007, 11:34 AM
  4. make Child Dialog not Popup?
    By Zeusbwr in forum Windows Programming
    Replies: 5
    Last Post: 04-08-2005, 02:42 PM
  5. Converting Numbers to Words
    By denizengt in forum C Programming
    Replies: 20
    Last Post: 11-05-2003, 09:19 PM