Julian Date - help with ifs, elses, and more

Hello!
I've improved my C++ skills since my last topic, could you please help me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  #include <stdio.h>
#include <stdlib.h>

int main () {
   int JD, m, d, dAno;
   char noon;
   printf ("This program gives the Julian Date for Jan-Feb 2014\nEnter the month number: \n");
   scanf("%d", &m);
   while ((m>2)||(m<1)) {printf ("Error; this program only applies\nto January or February \n");
   printf ("Enter the month number: \n");
   scanf("%d", &m);}
   printf ("Now enter the day of the month:\n");
   scanf("%d", &d);
   while ((d>31)||(d<1)) {printf ("Error; you must enter a valid date \n");
   printf ("Now enter the day of the month:\n");
   scanf("%d", &d);}
   printf ("Do you want the Julian Date after or before noon in Greenwich?\nType A or B:");
   scanf("%d", &noon);
   /*My first difficulty here: how can I evaluate a char? How can I assure that
   if the user types a different letter, signal or number the program says he's
   wrong, like above? Like, if I type a decimal or a char for the month, it enters in
   infinite looping, can I prevent that happening?*/
   if (m=1){
   dAno=d;
   JD=dAno+2456658;
   printf ("The Julian Date is %d\n", JD);}
   else{
   if (m=2){
        dAno=d+31;
        JD=dAno+2456658;
        printf ("A Data Juliana eh %d\n", JD);}}
        /*Here I have the main problem: the program is working well for January,
        but if I want to calculate for February, it simply ignores and calculates
        like if it was still January: Feb 1st returns 2456659, just like
        it was Jan 1st. I can't handle with these "ifs" and "elses" and also I
        can't make the proper use of the "noon" flag - if noon = B, the Julian Date shall
        be decremented by 1. At least the program compiles, that's already a deed for me.
        I'm using only the 1st two months for testing, I want to make a complete
        script for all the 12 months, but I need some tip about how to do that
        in a more efficient way*/
        system ("PAUSE");
        return 0;

}
if (m=1) should be if (m==1)
Thanks, JockX! Could you be even kinder and help with my other doubts? Or will the use of (m==1) solve virtually all of them?
Last edited on
Something that is confusing me.

if you are using C++ why are you using C I/O:

1
2
3
printf("text");    //C

cout << "text";   //C++ 
closed account (iAk3T05o)
Printf is C? Great, i won't have to use it
Printf is C? Great, i won't have to use it


your whole program is c. there is NO c++ in there.

and neither should you be using scanf either.
http://stackoverflow.com/questions/3456106/problem-using-scanf

use cin and cout for basic input/output
http://www.cplusplus.com/doc/tutorial/basic_io/

Last edited on
Hi Cronnoc.
I didn't know that I should use cout instead of printf. I've learned with printf, and apparently it's working, I fixed the program and now it works perfectly.
just out of curiosity how have you been learning
Topic archived. No new replies allowed.