0

So, My problem here is that I am getting the wrong int from this array and I dont know why. Notice that: pacients.birth = 09/07/1997 in this case.

Using GDB, thats what I have:

Breakpoint 1, ident () at fm.c:72
72      int last = strlen(pacients.birth);
(gdb) n
74      int unidade = pacients.birth[last-1] - '0';
(gdb) n
75      int dezena = pacients.birth[last-2] - '0';
(gdb) p last
$1 = 10
(gdb) p unidade
$2 = 7
(gdb) p dezena
$3 = 1210253312
(gdb) 

Notice that $1 and $2 are correct, but $3 Im getting garbage here...

4
  • That's really weird! Can you please update your question with the source code? If it's a pset-related code, better send it to me! You can find my email in my profile!
    – kzidane
    Jun 26 '14 at 13:55
  • It is not, but it is 220 lines long, but here it is:pastie.org/private/fb678ykyuwjtmqnqehmejw Jun 26 '14 at 14:01
  • found anything wrong?? Jun 26 '14 at 14:44
  • Looking at your code, I couldn't find any problems. However, I couldn't compile and execute/debug it because I don't have some of the header files and the libraries included and linked to your program respectively.
    – kzidane
    Jun 26 '14 at 14:49
-1

Looks like I was able to fiz this problem by initiallizing the int`s before, just dont know why yet.

Like this:

int unidade = 0;
int dezena = 0;
5
  • That's not correct! You don't have to initialize ints with 0s before you initialize them with any expressions or any other literals.
    – kzidane
    Jun 26 '14 at 15:59
  • so why did it worked? Jun 26 '14 at 17:02
  • I don't really know! Unfortunately I don't have full control over formatting code in comments, but consider this program #include <stdio.h> int main(void) {char *str = "10"; int x = str[0] - '0'; int y = str[1] - '0'; printf("x: %d\ny: %d\n", x, y);} the output: x: 1 y: 0 I didn't have to initialize x and y to 0s before I use them in this program.
    – kzidane
    Jun 26 '14 at 17:09
  • @Kareem how can I ralk to you in private? Do you have an account in cs50 reddit? Jun 26 '14 at 17:21
  • Yes I do. We also may chat in the cs50 chat room here chat.stackexchange.com/rooms/14114/cs50
    – kzidane
    Jun 26 '14 at 17:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .