So I am new here, from what I have seen people here know what they're doing (not that my question is hard at all). So I am taking a programming class at my school. I have dabbled around with programming for a while, but never got very far. My teacher has given us some very simple programs to write, but I am a bit confused with this one. Here are the instructions to the assignment:
------------------
Write a C++ program that does the following:
Prompt the user to enter a day of the week as M (or m), T, W, R, F, S, and U for Monday through Sunday respectively. The user may enter an upper or lower case letter.
When the user enters a character, the program will echo the letter and output the name of the day of the week.
Provide an error trap that reads something like "you have entered an invalid letter; program aborting." Suggestion: use a switch statement with the error trap as the default condition. it is not necessary to prompt for multiple inputs.
-----------------
So I know how to get the program to echo back the letter and everything. What I am a little confused about is: will I have to define all the letters as their respective day? eg. make M== Monday. And if I do have to do that how would I get it to accept Upper and Lower case letters and recognize that that letter is == monday ect. ect.
Also my main problem is the switch statement as the error trap. I have never used the switch statement, but I know what they do. I just don't really understand how I would use it for an error trap. Am I suppose to just make a case for every other letter in the alphabet other then M T W R F S and U? Even if I do that then what if the user enters a number instead of a letter?
I am sorry if I am not making much sense... It makes sense in my head, hopefully its not too jumbled..
Am I suppose to just make a case for every other letter in the alphabet other then M T W R F S and U?
No. Recall that a switch has an optional default case.
Originally Posted by Shinisama
Even if I do that then what if the user enters a number instead of a letter?
If you are reading a character and ignorning whatever comes after that character, then if a number is entered, the character read won't be one of the valid inputs.
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
Alright, after a little bit more research I figured it out (at least I thought.) This is what I came up with:
#include <iostream>
using namespace std;
int main ()
{
int day;
cout << "Enter a letter that represents a day of the week: " << endl;
cin >> day;
cout << "The letter you entered is ." << day << endl;
switch (day)
{
case 'm':
case 'M':
cout << "The day of the week is Monday." << endl;
break;
case 't':
case 'T':
cout << "The day of the week is Tuesday." << endl;
break;
case 'w':
case 'W':
cout << "The day of the week is Wednesday." << endl;
break;
case 'r':
case 'R':
cout << "The day of the week is Thursday." << endl;
break;
case 'f':
case 'F':
cout << "The day of the week is Friday." << endl;
break;
case 's':
case 'S':
cout << "The day of the week is Friday." << endl;
break;
case 'u':
case 'U':
cout << "The day of the week is Sunday." << endl;
break;
default:
cout << "You have entered an invalid letter program aborting." << endl;
}
return 0;
}
When I ran the program I hit the letter 'm' and I got 'the letter you have entered is .0' and then it also stated the default error catching statement 'you have entered and invalid letter, program aborting'
Bookmarks