#include <iostream.h>
#include <ctype.h>
#define message = "Is a vowel."
#define message2 = "Is not a vowel."
int main()
{
char letter;
cout << "Type a letter : " << endl;
cin >> letter;
letter = toupper(letter);
switch (letter)
{
case "A" : cout<<message<<endl;
break;
case "E" : cout<<message<<endl;
break;
case "I" : cout<<message<<endl;
break;
case "O" : cout<<message<<endl;
break;
case "U" : cout<<message<<endl;
break;
default : cout<<message2<<endl;
break;
}
return(0);
}
My error-free code has an error. (Switches/Cases)
Started by Sundosia, Aug 05 2008 04:03 PM
3 replies to this topic
#1
Posted 05 August 2008 - 04:03 PM
At least it seems error free. But I guess I'm doing something wrong! =)
|
|
|
#2
Posted 05 August 2008 - 10:05 PM
1. The .h-suffix is not necessary in C++; use iostream and ctype.
2. When you are in C++ and want to use C-libraries, you shall always add a c- in front of the originally name. The library ctype is actually its originally name, also in C, so in C++ you must use it as cctype.
3. When you #define a value, you shall not use the equal-sign.
4. In order to use cout, cin and endl, you must let the computer know, that they're from the namespace, std.
5. A switch-statement's cases do only accept values that can be converted to numbers; thus you cannot use strings (double-quotes) and must specify them as characters (single-quotes).
6. return is not really a function, but a built-in construction, so the parentheses are optional, and most people choose not to use them. Just so that you know.
This is the corrected code, using the same formatting as you did.
2. When you are in C++ and want to use C-libraries, you shall always add a c- in front of the originally name. The library ctype is actually its originally name, also in C, so in C++ you must use it as cctype.
3. When you #define a value, you shall not use the equal-sign.
4. In order to use cout, cin and endl, you must let the computer know, that they're from the namespace, std.
5. A switch-statement's cases do only accept values that can be converted to numbers; thus you cannot use strings (double-quotes) and must specify them as characters (single-quotes).
6. return is not really a function, but a built-in construction, so the parentheses are optional, and most people choose not to use them. Just so that you know.
This is the corrected code, using the same formatting as you did.
#include <iostream>
#include <cctype>
#define message "Is a vowel."
#define message2 "Is not a vowel."
using namespace std;
int main()
{
char letter;
cout << "Type a letter : " << endl;
cin >> letter;
letter = toupper(letter);
switch (letter)
{
case 'A' : cout<<message<<endl;
break;
case 'E' : cout<<message<<endl;
break;
case 'I' : cout<<message<<endl;
break;
case 'O' : cout<<message<<endl;
break;
case 'U' : cout<<message<<endl;
break;
default : cout<<message2<<endl;
break;
}
return 0;
}
#3
Posted 06 August 2008 - 06:41 PM
Here's a somewhat cleaner and more efficient version. Notice that I allowed the cases to fall through, which is an advantage to using the switch statement over if-else.
#include <iostream>
#include <cctype>
#define message "Is a vowel."
#define message2 "Is not a vowel."
using namespace std;
int main()
{
char letter;
cout << "Type a letter : " << endl;
cin >> letter;
letter = toupper(letter);
switch (letter)
{
case 'A' :
case 'E' :
case 'I' :
case 'O' :
case 'U' :
cout<<message<<endl;
break;
default :
cout<<message2<<endl;
break;
}
return 0;
}
#4
Posted 07 August 2008 - 11:16 AM
Thanks for the help guys! And for the more efficient version, it will help speed up my programming! =)


Sign In
Create Account

Back to top









