Jump to content

My error-free code has an error. (Switches/Cases)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Sundosia

Sundosia

    Newbie

  • Members
  • Pip
  • 2 posts
At least it seems error free. But I guess I'm doing something wrong! =)


#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);

}



#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
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.
#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
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
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
Sundosia

Sundosia

    Newbie

  • Members
  • Pip
  • 2 posts
Thanks for the help guys! And for the more efficient version, it will help speed up my programming! =)