Thread: Help in project!

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    14

    Help in project!

    Ok...i am doing a school project...basically you input a year and it will tell you what date easter falls on in that year. Believe it or not...there is a mathematical way to find it out. Check this out:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int year, b, c, a, s, r, t, h, g, j, k, m, n, q, p;
        char month;
        cout<< "This program designed by Shiv (in C++) will find the date of Easter in the year you choose... \n";
        cout<< "Please choose the year: ";
        cin>> year;
        cin.ignore();
        b = year / 100;
        c = year % 100;
        a = (5*b + c) % 19;
        r = (3*b + 75) / 4;
        s = (3*b + 75) % 4;
        t = (8*b + 11) / 25;
        h = (19*a + r - t) % 30;
        g = (a + 11*h) / 319;
        j = (300 - 60*s + c) / 4;
        k = (300 - 60*s + c) % 4;
        m = (2*j - k - h + g) % 7;
        n = (h - g + m + 110) / 30;
        q = (h - g + m + 110) % 30;
        p = (q + 5 - n) % 32;
        if (n = 4) {
              char month = "April";
              }
        if (n = 3) {
              char month = "March";
              }
        cout<< "In year "<< year<<", Easter will be on Sunday "<< p<<" of "<< month;
        cin.get();
        
    }
    In the code...the interger n is either 3 or 4, if it is 3 the month is march, if it is 4 the month is april...so im trying to make if statements to do the recognising...but its not working:

    Code:
    if (n = 4) {
              char month = "April";
              }
        if (n = 3) {
              char month = "March";
              }
    Any help to doing it would be greatly appreciated...thanks.

  2. #2
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Try using strings instead?

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        int year, b, c, a, s, r, t, h, g, j, k, m, n, q, p;
        string month;
        cout<< "This program designed by Shiv (in C++) will find the date of Easter in the year you choose... \n";
        cout<< "Please choose the year: ";
        cin>> year;
        cin.ignore();
        b = year / 100;
        c = year % 100;
        a = (5*b + c) % 19;
        r = (3*b + 75) / 4;
        s = (3*b + 75) % 4;
        t = (8*b + 11) / 25;
        h = (19*a + r - t) % 30;
        g = (a + 11*h) / 319;
        j = (300 - 60*s + c) / 4;
        k = (300 - 60*s + c) % 4;
        m = (2*j - k - h + g) % 7;
        n = (h - g + m + 110) / 30;
        q = (h - g + m + 110) % 30;
        p = (q + 5 - n) % 32;
        if (n = 4) {
              month = "April";
              }
        if (n = 3) {
               month = "March";
              }
        cout<< "In year "<< year<<", Easter will be on Sunday "<< p<<" of "<< month;
        cin.get();
        
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (n = 4)
    It's ==, not =

    Also, you need much better variable names than a,b,c,d,.....

  4. #4
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29
    i am quite new at c++, just a few points,
    Code:
    if (n = 4) {
              month = "April";
              }
        if (n = 3) {
               month = "March";
              }

    Should it not be like this

    Code:
    if (n = 4) {
              month = "April";
              }
        else if (n = 3) {
               month = "March";
              }
    ?

    And another this, when i compile the code i get the following message in my compiler (i use borland 5.5)
    Code:
    warning w8060 possibly incorrect assignment in function main
    At both the if lines.

    Can anybody explain?

    edit::: i found answer to 2nd question in previous reply
    Last edited by 74466; 01-31-2006 at 12:57 AM. Reason: i did not read the previous message

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> possibly incorrect assignment in function main

    This is probably due to use of = instead of == as Salem pointed out.

    The else if in this case is probably better, but either will work since n cannot == 4 and 3 at the same time.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    The problem you are experiencing trips up all beginning programmers. In C++ the equal sign is used to "assign" something a value, which means you are setting something equal to a value. For instance, this is an assignment using the equals sign:
    Code:
    int n = 4;
    It results in n being set equal to 4.

    Another thing you have to understand is that the condition in an if statement has to result in it either being true or false. So, let's see what happens when you write this:
    Code:
    if (n = 4)
    First, n is assigned or set equal to the value 4. After that, the if statement conditional becomes:
    Code:
    if(n)
    where n is 4. That means the if conditional becomes:
    Code:
    if(4)
    Not only is that not what you intended the conditional to be, it doesn't even look like something that is true or false. However, all numbers except 0 are considered to be true, and 0 is considered to be false, so the if statement does not cause an error. Try running this program:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	if(-1)
    	{
    		cout<<"-1 is true or else this wouldn't display."<<endl;
    	}
    
    	
    	if(0)
    	{
    		cout<<"If this displays, 0 is true."<<endl;
    	}
    	else
    	{
    		cout<<"If this displays, 0 is false."<<endl;
    	}
    
    	
    	if(1)
    	{
    		cout<<"1 is true or else this wouldn't display."<<endl;
    	}
    
    
    
    	return 0;
    }
    In C++, the double equals sign(==) is used to compare two things, and the mistake you are making is trying to use the single equals sign to compare things. You should not interpret this statement:
    Code:
    if(n=4)
    to mean: "if n is equal to 4, then execute the following statments." You should interpret it to mean: "assign 4 to n, and then since 4 is always true, execute the following statements." Just remember that whenever you see "=" you should say "assign" to yourself--not "equal to". That way, this statement:
    Code:
    if(n=4)
    would read: "if n assign 4" which doesn't make much sense, so you know it is an error.

    However, when you see "==", you should interpret that to mean "equal to". That way if you write this:
    Code:
    if(n==4)
    it will read: "if n is equal to 4", which is correct.
    Last edited by 7stud; 01-31-2006 at 02:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM