Takaflaka
There are a lot of coding and logic problems with your code.
if(a==M)
a isn't defined, but you probably meant
ascii.
As it stands,
M is an undeclared variable. Probably you mean the character
'M'.
Apart from other similar flaws with the other choices, your answer would be wrong anyway.
1 2 3 4
|
if(a==S)
answer=5;
if(a==S)
answer=6;
|
For the same input 'S', answer would be set to 5, then changed to 6. Similar things will happen with 'T' - Tuesday or Thursday.
You have no nesting - there is no consideration of the second letter.
I don't think nested switches are very good here. The first program below uses nested ifs (and requires C++11 for the range-based for loop).
The second program (rather messily) uses nested switches. It was news to me that you could switch on anything other than an integer. However, it seems to be OK if both parts of the comparison can be converted to integers, so characters work.
I expect that other forum users can improve these two.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string dayNames[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
char firstLetter, secondLetter;
cout << "Input first character: ";
cin >> firstLetter;
firstLetter = toupper( firstLetter ); // easier comparison if you ensure it is upper case
bool needTwo = ( firstLetter == 'S' || firstLetter == 'T' ); // do you need to consider the second letter as well?
if ( needTwo )
{
cout << "Input second character: ";
cin >> secondLetter;
secondLetter = tolower( secondLetter );
}
string day = ""; // default if nothing matches
for ( string s : dayNames ) // needs C++11 for the range-based loop
{
if ( s[0] == firstLetter ) // compare first letter
{
if ( needTwo )
{
if ( s[1] == secondLetter ) day = s; // compare second letter if necessary
}
else day = s;
}
}
if ( day != "" ) cout << "Day is " << day << endl;
else cout << "Day not found" << endl;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
char firstLetter, secondLetter;
cout << "Input first character: ";
cin >> firstLetter;
firstLetter = toupper( firstLetter ); // comparison later made in upper case
bool needTwo = ( firstLetter == 'S' || firstLetter == 'T' ); // do you need to consider the second letter as well?
if ( needTwo )
{
cout << "Input second character: ";
cin >> secondLetter;
secondLetter = tolower( secondLetter ); // comparison later made in lower case
}
string day = ""; // default if nothing matches
switch( firstLetter )
{
case 'S': switch( secondLetter )
{
case 'u': day = "Sunday" ; break;
case 'a': day = "Saturday"; break;
}
break;
case 'T': switch( secondLetter )
{
case 'u': day = "Tuesday" ; break;
case 'h': day = "Thursday"; break;
}
break;
case 'M': day = "Monday" ; break;
case 'W': day = "Wednesday"; break;
case 'F': day = "Friday" ; break;
}
if ( day != "" ) cout << "Day is " << day << endl;
else cout << "Day not found" << endl;
}
|