Thread: Please help me with my c++ homework!

  1. #31
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Comment out the line
    Code:
     // date=weekday(   );
    and your program should compile without any warnings or errors.

    Once you have your program compiling with warnings or errors you can tackle your function.

    Jim

  2. #32
    Registered User
    Join Date
    Oct 2010
    Location
    Cincinnati, Oh
    Posts
    25
    OK now i have absolutely no idea what to do. I feel like I'm really close, but there is a lot of little things that aren't right. This project was due today, but if I turn it in tomorrow working, ill get a better grade than if i turned it in today not working.

    Code:
    #include <iostream>
    using namespace std;
    
    
    //-----------------------------------------------------
    //Main Program
    //-----------------------------------------------------
    
    
    int main ( ) 
    {
      
        int year,month,weekday,day;  
    	char repeat;
    	int Monday=2, Tuesday=3, Wednesday=4, Thursday=5, Friday=6, Saturday=0, Sunday=1;
        int dayofweek;
    	int dayname;
    
    	cout<<"Input the month:";
    	cin>>month;
    		while(month!=0){
    			cin>>day;
    			cin>>year;
    			dayofweek=weekday(month,day,year);
    			dayname(dayofweek);
    	cout<<endl;
            cout<<"Input the day:";
    	cin>>day;
    	cout<<endl; 
    	cout<<"Input the year:";
    	cin>>year;
    	cout<<endl;
    
    
    
    	
      	cout<<"Month----"<<month<<endl;
    	cout<<"Day------"<<day<<endl;
    	cout<<"Year-----"<<year<<endl;
    	cout<<"Weekday--"<<weekday<<endl;
    	cout<<endl;
    	cout<<"Month----"<<0<<endl;
    	
    	
    	
    	cout<<"Would you like to compute another date? (Y/N):";
    	cin>>repeat;
    	cout<<endl;
    	cout<<endl;
    	cout<<"----------------------------------------------------";
    	cout<<endl;
    	cout<<endl;
    
    
    	while (repeat=='Y' || repeat== 'y');
    	cout<<endl;
    
      return 0;
    }
    
    
    
    	//------------------------------------------------------------------
    	//Weekday
    	//
    	//This function uses Zeller’s Congruence formula to calculate 
    	//the day of the week on which a chosen date falls.
    	//
    	//Input Parameters
    	//   Century---Two numbers to the left in the year
    	//   Year------Two numbers to the right of the century
    	//   Date-------The number of the day of the month
    	//
    	//Output Parameters
    	//   Day---The specific day of the week in which the date falls
    	//
    	//Return Value:
    	//   Day
    	//-------------------------------------------------------------------
    		
    	 weekday(int month,int day,int year);
    
    	{
    		int c,y,d,m;
    
    		d=day;
    		
    			if(month<=2){
    				m=month+12;
    				year--;
    			}
    			else 
    				m=month;
    		
    		c=year/100;
    		y=year%100;
    
    		weekday= ((day +((m+1)*26/10)) + (year +(y/4)) + ((c/4) - 2*c)) % 7);
    		
    		return weekday;
    	}
    	
    	//-------------------------------------------------------------------
    	//displaydayname 
    	//
    	//This function displays the day of the week using numbers associated 
    	//with the days.
    	//
    	//Input Parameters
    	//	Day Number
    	//
    	//Output Parameters
    	//  Day Name
    	//
    	//Return Value
    	//	None
    	//--------------------------------------------------------------------
    
    		void displaydayname(int daynumber)
    		{
    			if (daynumber==0)
    				cout<<"Saturday";
    			else 
    				(daynumber==1)
    				cout<<"Sunday";
    			else 
    				(daynumber==2)
    				cout<<"Monday";
    			else 
    				(daynumber==3)
    				cout<<"Tuesday";
    			else
    				(daynumber==4)
    				cout<<"Wednesday";
    			else
    				(daynumber==5)
    				cout<<"Thursday";
    			else
    				(daynumber==6)
    				cout<<"Friday";
    
    			return;
    		
    		}

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You made a commendable effort to format your code, but it is still not good enough. When I tried to format it better, I quickly found that you did not place your closing brace for a while loop in the global main function, thus causing the main function to be far longer than you expect.

    Other problems spotted at a glance include missing return types for your functions, and missing if keyword for your if-else chain in your displaydayname function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #34
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You are trying to add too much to your program at one time.

    Why are you adding the while statement?

    You first need to get the program to work for a single set of entries.

    Why are you adding another function when you have not figured out how to use the first function?

    You need to start out slowly. Add one small item. Compile, fix any errors. Add another item. Compile, continue until complete.


    Jim

  5. #35
    Registered User
    Join Date
    Oct 2010
    Location
    Cincinnati, Oh
    Posts
    25
    Alright guys this is it. I almost got it but i can't get it to compile. It says there is an illegal if-else statement, and term does not evaluate to a function taking 3 arguments.

    Code:
    #include <iostream>
    using namespace std;
    double weekday(int month,int day,int year);
    void displaydayname(int daynumber);
    //-----------------------------------------------------
    //Main Program
    //-----------------------------------------------------
    
    
    int main ( ) 
    {
      
        int year,month,weekday,day;  
        int daynumber;
    	int dayname;
    
    	cout<<"Input the month:";
    	cin>>month;
    	do{
    		
    		cout<<"Day------";
    		cin>> day;
    		cout<<"Year-----";
    		cin>> year;
    
    		cout<<endl;
    	
    		daynumber = weekday(month, day, year);
    		
    		cout<<"Weekday--";
    		cout<< endl;
    
    		displaydayname (daynumber);
    	
    		cout<<endl;
    		cout<<endl;
    		cout<<"Month----";
    		cin>>month;
    		}while (month > 0); 
    		return 0;
    		}
    
    
    
    	//------------------------------------------------------------------
    	//Weekday
    	//
    	//This function uses Zeller’s Congruence formula to calculate 
    	//the day of the week on which a chosen date falls.
    	//
    	//Input Parameters
    	//   Century---Two numbers to the left in the year
    	//   Year------Two numbers to the right of the century
    	//   Date-------The number of the day of the month
    	//
    	//Output Parameters
    	//   Day---The specific day of the week in which the date falls
    	//
    	//Return Value:
    	//   Day
    	//-------------------------------------------------------------------
    		
    	 double weekday(int month,int day,int year)
    	 {
    		int c,y,d,m,z;
    
    		d=day;
    		
    			if(month<=2){
    				m=month+12;
    				year--;
    			else
    				m=month;
    			}
    		c=year/100;
    		y=year%100;
    
    		z = ((day +((m+1)*26/10)) + (year +(y/4)) + ((c/4) - 2*c)) % 7;
    		
    		return z;
    	    }
    	
    	//-------------------------------------------------------------------
    	//displaydayname 
    	//
    	//This function displays the day of the week using numbers associated 
    	//with the days.
    	//
    	//Input Parameters
    	//	Day Number
    	//
    	//Output Parameters
    	//  Day Name
    	//
    	//Return Value
    	//	None
    	//--------------------------------------------------------------------
    
    		void displaydayname(int daynumber)
    		{
    			if (daynumber==0)
    				cout<<"Saturday";
    			else if (daynumber==1)
    				cout<<"Sunday";
    			else if (daynumber==2)
    				cout<<"Monday";
    			else if (daynumber==3)
    				cout<<"Tuesday";
    			else if (daynumber==4)
    				cout<<"Wednesday";
    			else if (daynumber==5)
    				cout<<"Thursday";
    			else if (daynumber==6)
    				cout<<"Friday";
    
    			return;
    		
    		}

  6. #36
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    if-else statements go like this:
    Code:
    if (...) {
        //as much or little code as you want
    } else { 
        //as much or little code as you want
    }
    or
    Code:
    if (...) 
        //one line of code max
    else
        //one line of code max
    or some combination of the two
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  3. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM

Tags for this Thread