Thread: Looping with strcat

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    216

    Looping with strcat

    Hello

    I'm having a problem with my program. What it does is take the input in 1 character at a time, replace it with a '*', and when enter is pressed, check to see if it is the correct password.

    I have diagnosed the problem being with the strcat function. When it's commented out, the other code works fine. The program stops working after 2 characters are inputed. Thanks for your help!

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char password[] = "cheese";
        char input_pass[10] = "";
        char c;
        
        int t = 0;
        int r;
        
        printf("Enter password\n");
        
        while(1)
        {       
                c = getch();
                
                if(c == 13)
                     break;
                
                t++;
                strcat(input_pass,&c); // THIS IS THE PROBLEM I THINK
                gotoxy(t - 1, 1);
                printf("*");
        }
        
        r = strcmp(password,input_pass);
        
        if(r != 0)
        {
             printf("\n\nIncorrect password\n");
             goto done;
        }
        
        printf("\n\nCorrect!\n");
        
        done:
             
        getchar();
        return(0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    strcat doesn't append chars, only strings.

    You need something like
    Code:
    char c[2] = { 0, 0 };
    You put characters in c[0], then you can append by using strcat.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Your strcat() call certainly is the problem - strcat() needs to be passed two null-terminated strings. A single character won't do, as it will read past wherever that character is in memory until it accidentally reaches a null byte. Best bet is to keep track of how many characters have been entered and manually place them in your buffer. That way you can also make sure the buffer doesn't overflow.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Quote Originally Posted by JohnGraham View Post
    Your strcat() call certainly is the problem - strcat() needs to be passed two null-terminated strings. A single character won't do, as it will read past wherever that character is in memory until it accidentally reaches a null byte. Best bet is to keep track of how many characters have been entered and manually place them in your buffer. That way you can also make sure the buffer doesn't overflow.
    So, could I use strcpy and put the characters in input_pass? For the overflow problem, I could just change it to a for loop, and make it count to 10 before exiting the loop, or if break; has already been called.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    char pass[11] = {0}
    int x = -1;
     do 
      { x++;
      pass[x] = fgetc(stdin);
      printf("\b*");  
      } while ((x < 10) && (pass[x] !=\n));
      pass[x] = 0;
    
      if (strcmp(password,pass))
        exit(0);

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    That looks so much more confusing than it has to be...

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by binks View Post
    That looks so much more confusing than it has to be...
    Ok... then don't use it... no skin off my nose.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Could I use strcpy like so?
    Code:
    strcpy(input_pass, &c);
    Or will strcpy not work for single character variables?

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    No. Did you try to compile it?

    The "str" is a tip-off that it wants to operate on STRINGS - which really don't exist - they are really arrays of chars, terminated with a '\0' (null)

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by mike65535 View Post
    STRINGS - which really don't exist
    Of course they do. They aren't their own unique data type called "string", but:
    Quote Originally Posted by mike65535 View Post
    they are really arrays of chars, terminated with a '\0' (null)
    That's a string.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Of course they do. They aren't their own unique data type called "string", but:That's a string.

    Quzah.
    In pretend-ville maybe... everywhere else it's just a specially formatted array of characters.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    In pretend-ville maybe... everywhere else it's just a specially formatted array of characters.
    Which would be a valid statement if we were on the "Everywhere Else Board".


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by binks View Post
    Could I use strcpy like so?
    Code:
    strcpy(input_pass, &c);
    Or will strcpy not work for single character variables?
    For single characters just use array[element] = character (despite it's name, that is a numerical assignment) ... strcpy() is going to go running rough shod all overy your program stack looking for a 0 to stop on.

    You really should read up on "strings" in C... hit the books or grow on Google...

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Which would be a valid statement if we were on the "Everywhere Else Board".


    Quzah.
    Yeah but this ain't "pretend-ville" either...

    LOL.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Yeah but this ain't "pretend-ville" either...
    Right, so stop pretending that C doesn't have strings, just because you don't like how they work. There is a standard definition in C for what defines a string. If you don't like it, go write your own language.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcat
    By TGM76 in forum C Programming
    Replies: 3
    Last Post: 07-27-2010, 07:42 AM
  2. strcat()
    By valaris in forum C Programming
    Replies: 11
    Last Post: 07-24-2008, 04:55 PM
  3. strcat
    By linuxdude in forum C Programming
    Replies: 1
    Last Post: 12-11-2003, 04:55 PM
  4. Help with strcat
    By mmondok in forum C Programming
    Replies: 1
    Last Post: 02-02-2003, 06:33 PM
  5. strcat
    By dashing_76 in forum C Programming
    Replies: 3
    Last Post: 03-22-2002, 02:01 AM

Tags for this Thread