Thread: printf/cout

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    45

    printf/cout

    I have made this code for a password (displays '*' when you type)that works fin but i have 2 questions:
    *(mark 1)Why does the code not work good when I use coud instead of printf
    *(mark 2)If I do:
    Code:
    cout<<password;
    some crap will be displayed after the password, why is that??


    Code:
    char password[5];
    for(int i=0;i<5;i++)
    {
        while(!kbhit())
        { 
         printf("*")               //mark 1
         password[i]=getch();
         }
    }      
    	
    printf("Password was:  ");
    
    for(int x=0;x!=i;x++)  // mark 2
    cout<<password[x];  //

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    55
    It won't work with just:

    Code:
    cout << password;
    This is because you are reading in, and displaying, one character at a time using your for loops. You haven't used a null character to terminate the string, so whilst printing the characters one by one works, printing the entire string with cout, or even with printf won't work. Here's the equivalent printf call that won't work:

    Code:
    printf("%s", password);
    To fix this, you need to declare the password array, with 1 extra element (6 in this case), and after the first for loop, add the null character in this last array position. Something like this:

    Code:
    int x, i;
    char password[6];
    
    for(i=0;i<5;i++)
    {
        while(!kbhit())
        { 
           printf("*")               //mark 1
           password[i]=getch();
        }
    }
    password[5] = '\0'; /* \0 is the null character. */
    	
    printf("Password was:  ");
    
    for(x = 0; x != i; x++)  // mark 2
      cout << password[x];  //
    
    cout << endl << password << endl;
    Also, what's kbhit()?

    Hope that helps.

    John.

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Talking

    John

    Code:
    code:--------------------------------------------------------------------------------
    int x, i;
    char password[6];
    
    for(i=0;i<5;i++)
    {
        while(!kbhit())
        { 
           printf("*")               //mark 1
           password[i]=getch();
        }
    }
    password[5] = '\0'; /* \0 is the null character. */
    	
    printf("Password was:  ");
    
    for(x = 0; x != i; x++)  // mark 2
      cout << password[x];  //
    
    cout << endl << password << endl;
    --------------------------------------------------------------------------------
    You have C and C++ intermixed- not good practice. I would stick will either all C or all C++
    Mr. C: Author and Instructor

  4. #4
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    kbhit() - a function that returns 0 if you do not press a key. It is not defined in Standard C- mainly compiler dependent. My advice do not use it.
    Mr. C: Author and Instructor

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    55
    You have C and C++ intermixed- not good practice. I would stick will either all C or all C++
    That was based on his code, not mine. I just included the extra bits as a demonstration as to what he was asking. Thanks for the heads up on kbhit() though.

    John.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf/cout
    By nitin1 in forum C++ Programming
    Replies: 4
    Last Post: 10-20-2005, 06:29 AM