Thread: What am I doing wrong?

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    5

    Question What am I doing wrong?

    I am new to C++. I have CodeWarrior, if that makes a difference. I can get this project to compile and receive input. But when it is suppose to output the weekday at the end of "entered is:", I only get "." What am I doing wrong? I have done the formulas manually, they all seem to work. Any help will be greatly appreciated.



    //Project 4 - receives the number of a month, the day
    //of a month, and a year, and returns the name of the
    //day of the week on which that date fell or will fall

    #include <iostream> // cin, cout
    #include <iomanip> // math library
    #include <string> // string library

    using namespace std; // introduces namespace std

    string DayoftheWeek(string, int, int, int, int, int, int, int, int, int, int, int, int);
    //prototype

    int main(void)
    {
    int mon, day, year;
    char YorN;
    string WeekDay;
    do
    {
    cout << "Enter the number of the month:";
    cin >> mon; //month of year
    cout << "Enter day of the month:";
    cin >> day; //day of month
    cout << "Enter the year:";
    cin >> year; //year number

    cout << "The day of the week for the date you have entered is: ";

    string DayoftheWeek(WeekDay); //function call
    cout << WeekDay;
    cout << ".\n";
    cout << "\n\n\nEnter Y to try another, N to stop.\n\t\t";
    cin >> YorN;
    }
    while(YorN=='Y'||YorN=='y');

    return 0;
    }

    string DayoftheWeek(string WeekDay, int mon, int a, int b, int c, int d, int w, int x, int y, int z, int r, int day, int year)
    //definition of function to find day of week
    {
    a = mon;
    b = day;
    c = year % 100;
    d = year / 100;

    if(a <= 2)
    {
    a += 10;
    }
    else
    {
    a -= 2;
    }
    {
    w = (13 * a - 1) / a;
    x = c / 4;
    y = d / 4;
    z = w + x + y + b + c - 2 * d;
    r = z % 7;
    }
    switch(r)//for remainder
    {
    case 0: WeekDay = "Sunday";

    case 1: WeekDay = "Monday";

    case 2: WeekDay = "Tuesday";

    case 3: WeekDay = "Wednesday";

    case 4: WeekDay = "Thursday";

    case 5: WeekDay = "Friday";

    default: WeekDay = "Saturday";

    }return WeekDay;

    }

  2. #2
    you only have to pass the variables you'll need in the function. not the ones you use to calculate stuff(like x,w,y,...)
    and when you return a value from your function you don't have to put it in your arg list, do it like this: return_value=function(arg1,arg2);
    and when u use switch(), make sure you add break; after every case. otherwise he will always take the default value.

    Are you sure the formula for calculating the day is right? Because I entered my birthday and it came up with sunday, and I was born on a wednesday. I could be wrong, it was so long ago, and I was still very young. Or it could be the millenium bug

    well here's the code:
    Code:
    #include <iostream> // cin, cout 
    #include <iomanip> // math library 
    #include <string> // string library 
    
    using namespace std; // introduces namespace std 
    
    string DayoftheWeek(int , int , int ); 
    //prototype 
    
    int main(void) 
    { 
    int mon, day, year; 
    char YorN; 
    string WeekDay; 
    do 
    { 
    cout << "Enter the number of the month:"; 
    cin >> mon; //month of year 
    cout << "Enter day of the month:"; 
    cin >> day; //day of month 
    cout << "Enter the year:"; 
    cin >> year; //year number 
    
    cout << "The day of the week for the date you have entered is: "; 
    
    WeekDay = DayoftheWeek(mon,day,year); //function call 
    cout << WeekDay; 
    cout << ".\n"; 
    cout << "\n\n\nEnter Y to try another, N to stop.\n\t\t"; 
    cin >> YorN; 
    } 
    while(YorN=='Y'||YorN=='y'); 
    
    return 0; 
    } 
    
    string DayoftheWeek(int a,int b, int year) 
    //definition of function to find day of week 
    { 
    string WeekDay;
    int c,d,w,x,y,z,r;
    c = year % 100; 
    d = year / 100; 
    
    if(a <= 2) 
    { 
    a += 10; 
    } 
    else 
    { 
    a -= 2; 
    } 
    { 
    w = (13 * a - 1) / a; 
    x = c / 4; 
    y = d / 4; 
    z = w + x + y + b + c - 2 * d; 
    r = z % 7; 
    } 
    switch(r)//for remainder 
    { 
    case 0: WeekDay = "Sunday"; 
    	break;
    case 1: WeekDay = "Monday"; 
    	break;
    case 2: WeekDay = "Tuesday"; 
    	break;
    case 3: WeekDay = "Wednesday"; 
    	break;
    case 4: WeekDay = "Thursday"; 
    	break;
    case 5: WeekDay = "Friday"; 
    	break;
    default: WeekDay = "Saturday"; 
    	break;
    }return WeekDay; 
    }

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    That function to me looks like a poor attempt at zellers algorithm. Here is how to write that function properly..... adapt it yourself if you want it to return a string instead of an int....
    Code:
    int calender::whatdayisfirstofmonth()
    {
    	int c=year/100; // # of centuries
    	int d=year%100; // # of years through century
    	int m=(month+10)%12; // # of month march is 1,feb is 12
    	int k=1; // set the day part to 1 so we get back the day for first of month
    	if ((month==1)||(month==2))// treat jan and feb as if they were in previous year
    	{
    		if (d==0) // if d is 0 then to go back a year d becomes 99 and c become c-1
    		{
    			d=99;
    			c-=1;
    		}
    		else
    		{
    		d-=1; // jan and feb are treated as previous year
    		}
    	}
    	 float g=(k + (floor(((13*m)-1)/5)) + d + (floor(d/4)) + (floor(c/4)) - (2*c));
    	 int f=static_cast<int>(g)%7; // cast result of algorithm to int to take modulus
    	 if (f<0) // if negative add 7
    
    	 {
    		 f+=7;
    	 }
    	 return f; // returns 0 to 6 corresponding to sunday to saturday
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM