# If statements not working

This is a discussion on If statements not working within the C Programming forums, part of the General Programming Boards category; Hi all.. I've got a problem with some code I'm writing and I don't know how to make it do ...

1. ## If statements not working

Hi all.. I've got a problem with some code I'm writing and I don't know how to make it do what I want. Every time I debug it, it gives me a 0 value. The goal is to write a program that given the date as input, it returns the day number. I'm sure you've all seen this somewhere before.

Code:
```// day-date.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <stdafx.h>
#include <conio.h>
#include <math.h>

int
main ()

{
int number;
double x,y;

printf("Enter month \n");
scanf("%d",&x);

printf("Enter day \n");
scanf("%d", &y);
{
if (x == 1){
number = y;
}
else if (x == 2){
number = 31 + y;
}
else if (x == 3){
number = 31 + 28 + y;
}
else if (x == 4){
number = 31 + 28 + 31 + y;
}
else if (x == 5){
number = 31 + 28 + 31 + 30 + y;
}
else if (x == 6){
number = 31 + 28 + 31 + 30 + 31 + y;
}
else if (x == 7){
number = 31 + 28 + 31 + 30 + 31 + 30 + y;
}
else if (x == 8){
number = 31 + 28 + 31 + 30 + 31 + 30 + 31 + y;
}
else if (x == 9){
number = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + y;
}
else if (x == 10){
number = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + y;
}
else if (x == 11){
number = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + y;
}
else if (x == 12){
number = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + y;
}
else
printf("invalid");
}
printf("The number for the date is %lf", &number);

getch();

return(number);

}```
Any help is appreciated.

2. I suspect you do not want x and y to be "double". If you use printf("x=%f, y=%f\n", x, y); after you've read the values with scanf, I'm pretty sure it will NOT show the value you expect.

--
Mats

3. One problem, you declare x and y as doubles but try to read them in as integers. Change them to ints.

4. Ok. I changed x and y to ints, but still getting 0.

5. Seeing as you print it using
Code:
`printf("The number for the date is %lf", &number);`
format for "long double" (or undefined format), I'm not entirely surprised it goes wrong.

--
Mats

6. Can you show me how to fix it?

I honesly have no idea.

7. Originally Posted by leroyjenkins
Can you show me how to fix it?

I honesly have no idea.
How do you THINK you should print an integer?

--
Mats

8. Code:
`printf("The number for the date is %c", &number);`

like so?

Edit:
Code:
`printf("The number for the date is %d", &number);`
Tried this and it gave me an oddly huge number.

9. http://faq.cprogramming.com/cgi-bin/...&id=1043284385

The sooner you memorize the components of a format string, the better.

10. I tried this

Code:
`printf ("%5d\n", The date number is %5d);`
and nothing but a thousand errors.

11. How did reading about %d lead you to conclude that most of the string should be outside the "" marks?
printf ("%5d\n", The date number is %5d);

Your post #8 was nearly there apart from a stray &

Code:
`printf ( "%5d\n", 123 );      /* Prints "  123" */`
This is what I have in there now.
Code:
`printf ("%5d\n, The date number is %5d");`

13. Well i'll be dang. It works now. It was just that last statement.

Code:
`printf("The number for the date is %d", number);`

14. Thanks everyone. Thank you all very much.