getting weird characters and numbers when searchin

This is a discussion on getting weird characters and numbers when searchin within the C Programming forums, part of the General Programming Boards category; hey everyone i am getting something like this Record found at index -1 with some weird characters 45 p 00/3434983984/349834 ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    46

    getting weird characters and numbers when searchin

    hey everyone i am getting something like this

    Record found at index -1
    with some weird characters 45 p 00/3434983984/349834

    when i am suppose to be getting a name the grade and a birthdate

    Code:
    int srch (STUDENT student[], char* ssn)
    {
      
      char Name[256];
      int tempstu;  
    
      while ((strcmp(ssn, "q"))) 
       {
         printf("\n Please enter social security number (press q to quit): ");    
         scanf("%s", ssn);
         tempstu = recSrch(student, ssn, 0, SIZE - 1);
         printf("\nRecord found at index %d:", tempstu);
         strcat(strcat(strcpy(Name, student[tempstu].fName), " "), 
    student[tempstu].lName);
         printf("\n%30s %9d  %13s     %02d/%02d/%4d", Name,
                 student[tempstu].grade, student[tempstu].ssn,
                 student[tempstu].bDate.month, student[tempstu].bDate.date,
                 student[tempstu].bDate.year);
    }
          printf("\nThank You.");
    }
    
    /*===============================recSrch=====================================*/
    // this function searches for the input social security number and returns
    // the result
    
    int recSrch (STUDENT student[], char* ssn, int lb, int ub)
    {
       int result;
       int mid = (lb + ub) / 2;
    
       if (lb > ub)
          {      
           result = -1;
          }
       else if (strcmp(ssn, student[mid].ssn) == 0)
               {
                result = mid;
               }    
       else if (strcmp(ssn, student[mid].ssn) < 0)
               {      
                result = recSrch(student, ssn, lb, mid - 1);
               }
       else
        { 
         result = recSrch(student, ssn, mid + 1, ub);
        }
       return result;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    nevermind got it

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I am surprised to learn that this works:

    Code:
    strcat(strcat(strcpy(Name, student[tempstu].fName), " "), 
    student[tempstu].lName);
    But otherwise I don't see anything wrong, have you verified that the records are okay already?
    The rest of the info is fine? Have you tried printing this infomation this way anywhere else it might be applicable to see if it works anywhere?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MK27 View Post
    I am surprised to learn that this works:

    Code:
    strcat(strcat(strcpy(Name, student[tempstu].fName), " "), 
    student[tempstu].lName);
    Yes, that works, because strcat, strcpy and other such functions return the pointer to the destination string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seeding with numbers, generating with characters?
    By muzihc in forum C Programming
    Replies: 1
    Last Post: 10-20-2008, 11:50 PM
  2. Numbers printed with characters
    By bnkslo in forum C Programming
    Replies: 15
    Last Post: 05-08-2008, 07:07 AM
  3. Convert string of characters to integers
    By Digital_20 in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2008, 09:40 PM
  4. displaying characters per line and checking prime numbers
    By xclarkiex5x in forum C++ Programming
    Replies: 5
    Last Post: 10-14-2007, 12:28 AM
  5. How would I only enter numbers and not characters
    By chrismax2 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2004, 03:19 PM

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