Code repetition
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: Code repetition

  1. #1
    Join Date
    May 2017
    Posts
    173

    Code repetition

    hello there . I had a little problem with my code which I could solve I suppose to read something from user then tell him if palindrome or not the problem is that any word I enter is palidrome and when I repeat it I also got something wrong Do I have to use array instead?

    Code:
      #include < iostream >
    #include <string>
    #include <algorithm>
    using namespace std;
    
    
    bool is_palindrome( string &s)
    {
      return  equal ( s.begin(), s.end(), s.rbegin() );
    }
    
    
    int main ()
    {
    	string input; char choice; int lc = 1;
    	
     do {	
    
    	  cout << "\nExecution number " << lc << ":" <<endl;
    	  cout << "-------------------" << endl;
         
    	  cout << "\nPlease enter a word or a phase :\n" << endl;
          
    	  getline ( cin , input );
    
    	  
    		is_palindrome ( input );
    
    	 if ( is_palindrome )
    
    		cout << "\n\"" << input << "\"" << " is palidrome.\n" << endl;
    
    	else
    		cout << "\n\"" << input << "\"" << " is not palidrome.\n" << endl;
    
    	do {
    
    	   cout << "\nDo you want to do another computation ? Y/N \n" << endl;
    	   cin >> choice;
    	 
    	}while (  choice != 'Y' && choice != 'y' && choice != 'n' && choice != 'N' && cout << "\nSorry wrong input!\n" );
         
    	 ++lc;
     
       }while ( choice == 'y' || choice == 'Y' );
    	
        cout << "\n";
        system ( "pause" );
    	return 0;
    }

    I tried cin.ignore ( 1000 , '\n' ) before the function call didnt work ;


    this is the output I had

    Execution number 1:
    -------------------

    Please enter a word or a phase :

    elsa

    "elsa" is palidrome.


    Do you want to do another computation ? Y/N

    y

    Execution number 2:
    -------------------

    Please enter a word or a phase :


    "" is palidrome.


    Do you want to do another computation ? Y/N


    thx for your help
    Last edited by david16; July 7th, 2017 at 08:29 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: Code repetition

    Firstly,
    Code:
    	is_palindrome ( input );
    
    	 if ( is_palindrome )
    Don't you mean
    Code:
    	 if ( is_palindrome(input) )
    Didn't the compiler report a problem?

    I tried cin.ignore ( 1000 , '\n' ) before the function call didnt work
    The solution is to use cin.ignore(), but not at the place where you tried it. Before I show the solution, have a good think about what is happening with the input stream.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  3. #3
    Join Date
    May 2017
    Posts
    173

    Re: Code repetition

    I guess the input stream are linked together that why We use cin.ignore () but you said I used it in wrong place ??
    Last edited by david16; July 7th, 2017 at 09:22 AM.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: Code repetition

    Quote Originally Posted by david16 View Post
    I guess the input stream are linked together that why We use cin.ignore () but you said I used it in wrong place ??
    Yes.

    Also
    Code:
    bool is_palindrome( string &s)
    would be better defined as
    Code:
    bool is_palindrome(const string &s)
    as the contents of string s isn't/shouldn't change.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  5. #5
    Join Date
    May 2017
    Posts
    173

    Re: Code repetition

    Oh yes you were right . The position of the cin.ignore () should be at the end of the do while loop that repeat the program . So before the while I plug it there right ??

  6. #6
    Join Date
    May 2017
    Posts
    173

    Re: Code repetition

    Sorry can you explain why bool is_palindrome(const string &s) with const ?? did know why its better ?

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: Code repetition

    Quote Originally Posted by david16 View Post
    Oh yes you were right . The position of the cin.ignore () should be at the end of the do while loop that repeat the program . So before the while I plug it there right ??
    If you put it there, it may seem to work - but try entering aaa as the input to the 'another computation' prompt.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  8. #8
    Join Date
    May 2017
    Posts
    173

    Re: Code repetition

    yes but even with bool is_palindrome(const string &s) I'm getting that issue you said . about

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: Code repetition

    Quote Originally Posted by david16 View Post
    Sorry can you explain why bool is_palindrome(const string &s) with const ?? did know why its better ?
    Code:
    bool is_palindrome(const string &s)
    You are passing s by reference, so any changes to s in the function are passed back to the corresponding argument variable in the calling function. If the function is not supposed/required to change s, then passing by const reference will cause a compiler error if s is changed by mistake in the function. Any error detected by the compiler is much better than trying to debug a problem! In general, if the contents of any variable isn't supposed to change, then it should be defined as const.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  10. #10
    Join Date
    May 2017
    Posts
    173

    Re: Code repetition

    Oh I see what you mean with cin.ignore () . Where you thing I should put it ?

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: Code repetition

    Quote Originally Posted by david16 View Post
    yes but even with bool is_palindrome(const string &s) I'm getting that issue you said . about
    For your code, having const/not const there will make no run-time difference. It's just good practice you should get into the habit of using where appropriate.

    Hint. Try moving the cin.ignore() into the other loop.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  12. #12
    Join Date
    May 2017
    Posts
    173

    Re: Code repetition

    Yes got it . I should put that after the cin >> choice in the first do while loop where I ask if user want to repeat and I receive his choice

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: Code repetition

    Quote Originally Posted by david16 View Post
    Yes got it . I should put that after the cin >> choice in the first do while loop where I ask if user want to repeat and I receive his choice
    yep!

    PS Take a bonus point for using equal() to test for a palindrome.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  14. #14
    Join Date
    May 2017
    Posts
    173

    Re: Code repetition

    thx a lot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)