Thread: Prog won't work after i press enter...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    15

    Prog won't work after i press enter...

    ok its a password prog...when i type the password and press enter nothing happens....whats wrong?

    Code:
    /***  password lit un mot de passe sans l'afficher à l'écran et vérifie,    ***/
    /***  via la fonction "strlen", si on n'a pas dépassé une certaine longueur ***/
    /***  maximale prédéfinie                                                   ***/
    
    #include <stdio.h>                                             /* pour printf */
    #include <conio.h>                                      /* pour getche, getch */
    #include <string.h>                                    /* pour strlen, strcpy */
    
    main()
    {
        char buffer[81];                                 /* pour ranger la saisie */
        char pword[9];                                            /* mot de passe */
        char reply, new, i;                              /* Variables de contrôle */
    
        do
          {
    
             printf("Entrez un mot de passe (8 caractères au plus) : ");
    
             i = 0;
             while ((buffer[i] = getch()) != '\n')       /* lecture du mot de passe */
                   i++;
             buffer[i] = '\0';                                  /*  caractère nul */
    
             if (strlen(buffer) > 8)                 /* si mot de passe trop long */
                {
                   printf("Mot de passe trop long.\n");
                   printf("<Entrée> pour nouvelle saisie. Fin par <ECHAP>.\n");
                   new = getche();
                }
             else                                         /* mot de passe correct */
                {
                   strcpy(pword, buffer);               /* mémoriser mot de passe */
                   printf("\nMot de passe enregistré. Visualisation ? (o/n) ");
                   if ((reply = getche()) == 'o')
                       printf("\nVotre mot de passe est \"%s\"\n", pword);
                       new = 27;      /* pour finir do while, new doit recevoir 27 */
                }
             } while (new != 27);                     /* tant que nouvelle saisie */
    }
    thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > while ((buffer[i] = getch()) != '\n')

    Just use fgets. With your code, I can overflow your buffer and crash your program.

    The real problem is with your using 'getch'. This hangs the program. Use 'getchar ', or 'fgetc(stdin) ', or 'getc '.

    Quzah.
    Last edited by quzah; 07-03-2002 at 08:07 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    15
    if i change this while ((buffer[i] = getch()) != '\n') to this while ((buffer[i] = getchar()) != '\n') we can see the password...and i dont want that... what should i do? sorry im not really good in programming.
    Last edited by emperor; 07-03-2002 at 08:19 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always the search button?

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

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    15
    well i changed while ((buffer[i] = getch()) != '\n') to while ((buffer[i] = getch()) != '\r') and it works so im happy with that thanks for your help.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by emperor
    well i changed while ((buffer[i] = getch()) != '\n') to while ((buffer[i] = getch()) != '\r') and it works so im happy with that thanks for your help.
    Really? That's interesting to note. Is this on a MS based operating system? I'll have to try it here.

    IIRC, the '\r' is used in MS(read: dos/windows) OSs in conjunction with the '\n' character to denote a newline/carriage return. Mac's only use '\r', and *NIX only use '\n'. Again, I might be slightly off, but IIRC that's how it goes...

    Anyway, it's interesting to know.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    15
    yep im on windows xp

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by emperor
    yep im on windows xp
    Nod. Have you seen the (I think it's two lines?) simple code that will crash XP? I've yet to try it, and have been meaning to do so:

    In a nut shell, it's something like:
    Code:
    for(int i = 0; i < 5 ; i++)
        printf("\t\t\b\b\b");
    Some people have to make it more than a loop of 5. I'm not sure why it does it... Just another interestingly odd thing.

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

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    15
    yeah it works, it reboots my comp.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With a BlackJack Program in C
    By Jp2009 in forum C Programming
    Replies: 15
    Last Post: 03-30-2009, 10:06 AM
  2. Input for Struct?
    By stewade in forum C Programming
    Replies: 26
    Last Post: 05-03-2004, 11:52 AM
  3. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM
  4. Getting input without the user having to press Enter...
    By Ranedhel in forum C++ Programming
    Replies: 18
    Last Post: 07-16-2003, 05:36 PM
  5. Replies: 3
    Last Post: 06-29-2003, 05:50 PM