ok need some help plz

This is a discussion on ok need some help plz within the C++ Programming forums, part of the General Programming Boards category; My program only reads the first 'a'. There are a few variations of find (it's overloaded). You can use the ...

  1. #16
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    My program only reads the first 'a'.
    There are a few variations of find (it's overloaded). You can use the index parameter to find the next occurance of the character.

    From cppreference.com.
    find
    Syntax:
    #include <string>
    size_type find( const string& str, size_type index );
    size_type find( const char* str, size_type index );
    size_type find( const char* str, size_type index, size_type length );
    size_type find( char ch, size_type index );

    The function find() either:

    returns the first occurrence of str within the current string, starting at index, string::npos if nothing is found,

    returns the first occurrence of str within the current string and within length characters, starting at index, string::npos if nothing is found,

    or returns the index of the first occurrence ch within the current string, starting at index, string::npos if nothing is found

  2. #17
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> Why would you show him an infinate loop program and tell him to try it out?

    I told a friend once to try it out. (He didn't know about loops, he had started just 2 days ago.)
    I told him for was a neat and weird function that did cool things.
    He called me the next day saying how his system crashed.
    But to make matters worse he said he tried it 2 times over again just to make sure the crash wasn't a coincidence.
    It was hard not to laugh.

    I don't mean to imply that newtoprog would fall for it, his user name had just reminded me of the incident.

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >for (i = tries; i>=0; i--) // Used a forloop to decrement the tries
    If tries was 3, this would give you 4 tries. You could change it to:
    Code:
    for (i = tries; i>0; i--)        // Used a forloop to decrement the tries
    > need to find all instances of a letter in a string, ex: Alpha: has two 'a'. My program only reads the first 'a'.
    Try something like this within the for-loop:
    Code:
    for (i = tries; i>0; i--)        // Used a forloop to decrement the tries
    {
       cout<<"Please enter a letter to be guessed: ";
       cin>> letter;
       cin.ignore(80,'\n');
    
       //int pos = word.find(letter); // Find if the letter is in the word and
       string::size_type pos = word.find(letter,0); // Find if the letter is in the word and
                                 // displays it
    
       if (pos != string::npos)
       {
          guess[pos] =  letter;
          pos = word.find(letter, pos+1);
          while (pos != string::npos)
          {
             guess[pos] = letter;
             pos = word.find(letter, pos+1);
          }
          cout << "Correct. You now have: " << i << " left" << endl;
          cout << guess << endl;
       } 
       else
       {
          cout<<"Wrong! You now have: "<<i<< " left"<<endl;
          cout <<guess<<endl;
       }
    }

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    And you should probably add an extra condition to the while loop to make sure pos hasn't gone past the end of the string:
    Code:
          while (pos < word.length() && pos != string::npos)

  5. #20
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Just so you know, your so called "GOOD ideas" that you want, have already been posted in this thread. Just because I didn't post code on exactly how to do it, doesn't mean my idea was bad, it means you either:
    A. Didn't understand or know how to use it.
    B. Expect others to just code for you, and therefor abstract discussion of solutions would be wasted upon you.

    If it is A, then please ask... "How exactly would a loop work to do this?" or "How would I iterate through a string?" or any number of questions like that.

    If it is B, then please remove yourself form the forum.

  6. #21
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    My point exactly wraithan -- check my posts on this thread

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. plz help me...
    By sweetchakri in forum C Programming
    Replies: 1
    Last Post: 03-03-2009, 10:50 PM
  3. [Request] Need Help Plz
    By TylerD in forum Tech Board
    Replies: 4
    Last Post: 01-03-2009, 08:54 AM
  4. Anyone plz help me
    By Rose_Flowers in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-17-2003, 12:01 PM
  5. help plz plz
    By nsssn73 in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 08:44 AM

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