Arrays

Write a program that contains an array of strings. The array should contain the names of the days of the week. Input from the keyboard is the value 0 through 6, where 0 == Sunday. Display the name of the day of the week from the numeric value input.

<code>

int main()
{
char *days[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int num;
cout << "Enter a number from 0 to 6\n";
cin >> num;
if (num == 0)
{
cout << " 0 == "; cout <<days[0]; cout <<"\n";
}
else if (num == 1)
{
cout << " 1 == "; cout <<days[1]; cout <<"\n";
}
else if (num == 2)
{
cout << " 2 == "; cout <<days[2]; cout <<"\n";
}
else if (num == 3)
{
cout << " 3 == "; cout <<days[3]; cout <<"\n";
}
else if (num ==4)
{
cout << " 4 == "; cout <<days[4]; cout <<"\n";
}
else if (num == 5)
{
cout << " 5 == "; cout <<days[5]; cout <<"\n";
}
else
{
cout <<" 6 == "; cout <<days[6]; cout <<"\n";
}
system ("pause");
return 0;
}

</code>

That's the way I did I know I can make it shorter is it possible to use like a
2D array for this problem or how can I improve it? Thanks
Seeing as the number the user enters directly corresponds to the array element, you don't need a bunch of if statements to check each of them.

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
	char *days[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
	int num;
	cout << "Enter a number from 0 to 6\n";
	cin >> num;

	if(num >= 0 && num <= 6)
	{
		cout << days[num] << endl;
	}
}


This will basically do the same thing as you have.
I knew I could use the && I guess I was just using them the wrong way thanks so much for clarify that for me =)
I had this same assignment for a class, but I also have this added to it, and I've read all the corresponding material in the book and have no clue what to do. Any help would be appreciated.

Here are the instructions:

Rewrite the program so that the input is passed on the command line when the program is executed. Do the work in functions. Be sure to check that the proper number of parameters were passed. If the proper number of parameters were not passed: display a usage statement to the standard error device, and exit the program with a non-zero return code.

I don't even know what it means by passes and parameters.

Thanks.
Topic archived. No new replies allowed.