0

I have some code: I'm trying to compare xID with a string version of the ID. I want to convert xID to string, so I decided to try to use sprintf and I'm really running into a lot of issues. I have tried using snprinft and asprintf and it doesnt work. * cant find library

how can i make this integer a string with 8 digits and leading 0s? if theres something im doing wrong with sprintf, please let me know. ive been looking at this trying to read stuff for over an hour and am so lost

bool LinkedList::checkID(Node* head, int xID){

    Node* ptr = head;
    char stringID[8]; // changed to 36

    sprintf (stringID, "%08d \n", xID);
    cout << stringID << endl; // flushed cout and changed formatting

    while (ptr != NULL){
        if (strcmp(ptr->student.ID, stringID) == 0){
            return false;
        }
        ptr = ptr->next;
    }
    return true;
}

1 Answers1

0

stringId needs to be the length of the expected string + 1 for the null terminator.

So you should have:

char stringID[9];

and you stated in the comments that you want to make the id into the string and then compare that, so you want:

sprintf (stringID, "%08d", xID);
cout << "Preceding with zeros: " << stringID << endl;
Dominique McDonnell
  • 2,510
  • 16
  • 25
  • How can I print that string? I fear if I use if (strcmp(ptr->student.ID, stringID) == 0) it will not be 0. When I try to print it shows nothing on my screen :( If I loop and use ptr it gives me each char by char. but i want the fully concat version for my compare function Here's a pic of what I see http://prntscr.com/8jgduj/direct see how it has those weird symbols at the end? is that normal? –  Sep 23 '15 at 03:31
  • How you are doing it. What are you getting? – Dominique McDonnell Sep 23 '15 at 03:32
  • Well unless you have 'Preceding with zeros: ' in your student.ID it won't be zero. Don't know why it's not printing anything. There is nothing wrong with your print code. – Dominique McDonnell Sep 23 '15 at 03:39
  • @aaaa, you could try `printf ("Preceding with zeros: %08d \n", xID);` as it takes the same format. – Dominique McDonnell Sep 23 '15 at 03:41
  • @DominicMcDonell but i wish to print the resulting string. like, im trying to make sure when i do the compare that they are the same -- say my ID is 00000007 and I pass 7 as xID, will it be the same? according to my code. i dont quite get stest yet. i wasnt sure how you got 35 either –  Sep 23 '15 at 03:43
  • Then you want: `sprintf (stringID, "%08d", xID); cout << "Preceding with zeros: " << stringID << endl;` – Dominique McDonnell Sep 23 '15 at 03:48
  • @DominicMcDonell I am able to print using http://prntscr.com/8jgh1j/direct but I don't understand why it prints the second time and not the first iteration –  Sep 23 '15 at 03:50
  • @aaaa, you might need to flush cout: http://stackoverflow.com/questions/5437643/strange-behaviour-of-stdcout-in-linux – Dominique McDonnell Sep 23 '15 at 03:53
  • Using endl will flush. – Dominique McDonnell Sep 23 '15 at 03:54
  • I finally got it; thank you! thats actually what i did. i inserted an endl before and suddenly it showed lol –  Sep 23 '15 at 03:54