Thread: Why my code will cout the string twice?

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    3

    Why my code will cout the string twice?

    This is a password check code.When I enter the right password,it outputs "access allowed!access allowed!"
    When password is wrong,it outputs "try again!try again!".
    Why is this happened? Thanks for any help!

    Here is my code:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int check(string x)
    {
        if(x=="pppp")
             {
                 cout<<"access allowed!";
                 return 3;
             }
             else
             {
                 cout<<"try again!";
                 return 1;
             }
    
    
    }
    
    
    int main()
    {
        int i;
        string pass;
        do
        {
             cout<<"enter a password:";
             cin>>pass;
             check(pass);
             i=check(pass);
        }while(i==1);
    
    
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,578
    When password is wrong,it outputs "try again!try again!".
    Probably because you call the check() function twice.

    Jim

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    3
    you're right!
    I call the check() twice:
    Code:
    check(pass);
    i=check(pass);
    Thanks so much!

  4. #4
    Registered User
    Join Date
    May 2016
    Posts
    2
    Hello - I would just like to add a quick way to check - is if you have a function with a type other than void it is returning something - so you if see a call without anything to catch the return value you know something will go unexpected - as you see in your first function call

    I know you found the problem I just wanted to show you a way of thinking to problem solve

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-28-2016, 02:14 PM
  2. Replies: 5
    Last Post: 01-27-2016, 02:58 PM
  3. cout code, endl, and \n help?
    By ZezXion in forum C++ Programming
    Replies: 5
    Last Post: 11-15-2012, 06:16 PM
  4. help woth cout 3d string array
    By j_d in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2010, 12:19 AM
  5. cout << string
    By siavoshkc in forum C++ Programming
    Replies: 10
    Last Post: 07-26-2007, 12:51 PM

Tags for this Thread