1 Replies - 28520 Views - Last Post: 01 October 2006 - 05:28 PM Rate Topic: -----

#1 hmapes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-September 06

day of week program

Posted 01 October 2006 - 04:41 PM

I am trying to get the following program to run and can't figure out what my problem is. I copied the code example in my textbook, but there is obviously something I'm missing. Here's the assignment - Use functional decomposition to write a C++ program that takes a number in the range of 0 to 6 and a second number in the range of 1 to 366 as input. The first number represents the day of the week on which the year begings, where 0 is Sunday, and so on. The second number indicates the day of the year. The program then outputs the name of the day of the week corresponding to the day of the year. The number of the day of the week can be compared as follows:

(starts day + day of year - 1)%7

The program should output an error message if the numbers entered are not in the required ranges. The prompt and the error message should make it clear to the user the numbers must be entered.




#include < iostream >
#include < cmath >

using namespace std;

int main ()

{
	float dwi;		// Input data for day of week year began
	float dyi;		// Input data for the day of the year
	double dayOfYear;	// Output data for day of year
	bool numbersAreOK;	// True if data is within the range of 1-366

	
	// Prompt for what day of the week the year begins on
	cout << "To represent what day the year began on, please enter a whole number from 0-6." " " "0 representing Sunday, 6 representing Saturday. " << endl;
	cin >> dwi;

	// Prompt for the day of the year
	cout <<  " Please enter a whole number from 1-366 to represent the day of the year you wish to display." << endl;
	cin >> dyi;

	// Test Data
	if ( dwi < 0 || dwi > 6)  
		numbersAreOK = false;
		cout << "Invalid data, please try again." << endl;

	if ( dyi < 1 || dyi > 366)
		numbersAreOK = false;
		cout << "Invalid Data, Please try again." << endl;

	if ( numbersAreOK = true)
	{

	// Calculate day of year
	double dayOfYear = (dwi + dyi--1)%7;

	// Print Day of Year
	if (dayOfYear == 0)
		cout << "Sunday" << endl;

		else if (dayOfYear ==  1)
			cout << "Monday" << endl;

		else if (dayOfYear ==  2)
			cout << "Tuesday" << endl;

		else if (dayOfYear == 3)
			cout << "Wednesday" << endl;

		else if (dayOfYear ==  4)
			cout << "Thursday" << endl;

		else if (dayOfYear ==  5)
			cout << "Friday" << endl;

		else if (dayOfYear == 6)
			cout << "Saturday" << endl;
	}
		cin.get();
		cin.get();

	return 0;


edit: added [code] tags ~ jayman9

Is This A Good Question/Topic? 0
  • +

Replies To: day of week program

#2 Vextor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 313
  • Joined: 22-May 02

Re: day of week program

Posted 01 October 2006 - 05:28 PM

There is quite a few things that need to be changed and adjusted. A couple of em are just nit-picky, but the others are where you're getting your errors. Don't feel bad though, we have all made these mistakes.

First and foremost is your use of the modulus operator (%) and the values it's being used with. Because you are dealing with days of the year and not more explicit values, you should instead be using integers. Days of the year come in whole numbers. So first I would change all of your numerical types to integers. On that same note you 're defined' dayOfyear where you do the calculation. This is bad. Remember once you declare your variables you don't have to again. Finally you have to minus (-) signs in the calculation. You only need one.

// Calculate day of year
	double dayOfYear = (dwi + dyi--1)%7;

// change it to

dayOfYear = (dwi + dyi-1)%7; 
//if all of your numerical types are integers this should work fine.



Second is your use of a boolean value to handle erroeanous input. This is fine if you need it but in this case you don't. (unless your instructor requested it). Instead you can simply use your logical operators (||) to tie both of the if statments together and use an else instead. If you have to use it make sure you instanciate it to true.

/* this is perfectly legal */

				if (( dwi < 0 || dwi > 6) || ( dyi < 1 || dyi > 366))
	{
						cout << "Invalid data, please try again." << endl;
	}
				else
				{
					 //if-else if code here.
				 }



or if you have to use it make sure you do this.
bool numbersAreOK = true;



Finally when you use cin>> by itself like you are for input you should also use what's called cin.ignore(). cin takes only one character of information and leaves the 'enter key' in the input queue. This can cause problems. So after each of your cin>> statements try putting
cin.ignore();
. You should do this only after you run it for the first time so you can see the difference.

These won't hurt anything but you dont' need 2 cin.get() statements at the bottom and for the last option in the if-elseif to decide which day, you can just use an 'else'.


beyond that it looks pretty good! Keep up the good work and if you need anything else just drop in and ask.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1