Can't find where the value changes

This is a discussion on Can't find where the value changes within the C Programming forums, part of the General Programming Boards category; Trying to make a sort of username and password program. It appears to work fine, i printed almost everything that ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    11

    Can't find where the value changes

    Trying to make a sort of username and password program.
    It appears to work fine, i printed almost everything that gets stored so as to prove that my functions work.

    In the login function, it shows that pTemp->user.password = "ken". but the second time i print it, inside the while loop, in the first if, pTemp->user.password = a garbage value.

    I'm stumped as to where it changes.

    May anyone tell me where?

    Please go over my program for any other corrections and/or logic corrections

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    
    struct dataTag
    {
           char username[10];
           char password[10]; 
    };
    
    struct nodeTag
    {
            struct dataTag user;
            struct nodeTag *pNext;
    };
    typedef struct dataTag dataType;
    typedef struct nodeTag nodeType;
    
    nodeType *newNode(dataType Src, nodeType *pNext)
    {
             nodeType *pTemp;
             pTemp = malloc(sizeof(nodeType));
             pTemp->user = Src;
             pTemp->pNext = pNext;
             return pTemp;
    }
    
    void login(nodeType *pFirst, char user[], char pass[])
    {
         nodeType *pTemp;
         pTemp = pFirst;
         int error = 1;
         printf("pFirst->user.password = %s, pTemp->user.password = %s\n", pFirst->user.password, pTemp->user.password);
         getch();
        while((pTemp->pNext != NULL) && error==1)
        {
             printf("Now comparing: %s with %s\n", user, pTemp->user.username);
             getch();
             if(strcmp(user, pTemp->user.username) == 0)
             {
                printf("user check successeful!\nNow comparing: %s with %s\n", pass, pTemp->user.password);
                getch();
                if(strcmp(pass, pTemp->user.password) == 0)
                {
                   printf("password check successful! loop should exit!\n");
                   error = 0;
                   printf("error value successfully changed! %d\n", error);
                   getch();
                }
             }
             
             pTemp = pTemp->pNext;
        }
        
        if(error != 0)
                 printf("FALSE");
        else
                 printf("TRUE");
    }
    main()
    {
          dataType nFirst;
          nodeType *pTemp, *pFirst, *pSecond, *pThird, *pFourth;
          
          strcpy(nFirst.username, "pau");
          strcpy(nFirst.password, "ken");
          pFirst = newNode(nFirst, pSecond);
          
          strcpy(nFirst.username, "admin");
          strcpy(nFirst.password, "admin");
          pSecond = newNode(nFirst, pThird);
          
          strcpy(nFirst.username, "student");
          strcpy(nFirst.password, "dlsu1234");
          pThird = newNode(nFirst, pFourth);
    
          strcpy(nFirst.username, "commat2");
          strcpy(nFirst.password, "math");
          pFourth = newNode(nFirst, pTemp);
         
          printf("%s\n%s\n\n%s\n%s\n\n%s\n%s\n\n%s\n%s\n\n", pFirst->user.username, pFirst->user.password, pSecond->user.username, pSecond->user.password, pThird->user.username, pThird->user.password, pFourth->user.username, pFourth->user.password);
          /*pFirst->pNext = pSecond;
          pSecond->pNext = pThird;
          pThird->pNext = pFourth;
          pTemp=pFirst;*/
         char username[10], password[10];
        
         printf("Username: ");
         gets(username);
         printf("Password: ");
         int i;
         for(i=0;i<10;i++)
         {
            password[i]=getch();
            switch(password[i])
            {
                  case 13:  i=10;
                             printf("\n");
                             break;
                  case 8:   
                            i-=2;
                             printf("\b \b");
                             break;
                  default: printf("*");
            }
         }
         if(strlen(username) == 0)
         {
                     if(password[0] == 13)
                         printf("Complete the form.");
                     else
                         printf("Username missing.");
         }
         else
         {
             if(password[0] == 13)
                         printf("Password missing.");
             else
              login(pFirst, username, password);
         }
          getch();
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,624
    Have you tried using a debugger to help you debug this?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    11
    May you teach me how it works? All I see are a bunch of numbers.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by nirvana619 View Post
    May you teach me how it works? All I see are a bunch of numbers.
    Come now, let's not be lazy. Google "GDB tutorials"...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    11
    Yep, just did. Debugging is easier on Dev C++ than on Pelles C.

    Program fixed now, but I still had to manually point each structure to the next

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. could not find -lwsock32.lib
    By thomas_joyee in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2008, 12:28 PM
  3. How to find O of threads ?
    By jabka in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 12:25 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 07:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 08:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21