C++ switch statement problem

Write a C++ program that prompts the user to input a letter (upper or

Page 1 of 1

2 Replies - 781 Views - Last Post: 26 October 2009 - 02:58 PM Rate Topic: -----

#1 J.holloway718  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 20-October 09

C++ switch statement problem

Posted 26 October 2009 - 02:09 PM

Problem*** I run the problem and doesnt run the question again.


Write a C++ program that prompts the user to input a letter (upper or lower case) and determines if that letter is a vowel or consonant. The program should have the following:

Main
Asks the user to input the letter
Calls the programmer-defined function
Prints to the screen the letter and vowel or the letter and consonant

Programmer-defined function
Accepts the letter from the main
Determines if the letter is a vowel or consonant
Returns to the main function the result

Note the program should begin by asking the user to enter a letter and should continue to run as long as a + is not entered by the user.



#include<fstream>
#include<iostream>
using namespace std;

char letter;



int main (void)
{
char letter='\0';

while(letter!='+')
{
cout<< "Please enter a letter (upper or lower case)"<<endl;
cin>> letter;
}



switch (tolower(letter))
{
case 'a':
cout<<" a is a vowel."<<endl;
break;

case 'A':
cout<<" A is a vowel."<<endl;
break;

case 'e':
cout<< "e is a vowel"<<endl;
break;

case 'E':
cout<<" E is a vowel."<<endl;
break;

case 'i':
cout<< "i is a vowel."<<endl;
break;
case 'I':
cout<< "I is a vowel."<<endl;
break;

case 'o':
cout<<"o is a vowel."<<endl;
break;

case 'O':
cout<< "O is a vowel."<<endl;
break;

case 'u':
cout<< "u is a vowel."<<endl;
break;

case 'U':
cout<< "U is a vowel."<<endl;
break;

return 0;

}

}

Is This A Good Question/Topic? 0
  • +

Replies To: C++ switch statement problem

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: C++ switch statement problem

Posted 26 October 2009 - 02:13 PM

Duplicate post http://www.dreaminco...topic134558.htm
Was This Post Helpful? 0
  • +
  • -

#3 Sn0wm4n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 08-July 08

Re: C++ switch statement problem

Posted 26 October 2009 - 02:58 PM

ok firstly I realized that tolower actually returns a number which I assume to be the Unicode identifier which is a large problem. I made a header file that actually did what you want and rewrote some of your code to work with it along with a few other things. Next time you post code, post in [*code] [*/code] without the *'s

CaseChange.h (HAS 2 FUNCTIONS toUpper() and toLower())
char toUpper(char inputLetter)
{
	if (inputLetter == 'a')
	{
		inputLetter = 'A';
	}
	if (inputLetter == 'b')
	{
		inputLetter = 'B';
	}
	if (inputLetter == 'c')
	{
		inputLetter = 'C';
	}
	if (inputLetter == 'd')
	{
		inputLetter = 'D';
	}
	if (inputLetter == 'e')
	{
		inputLetter = 'E';
	}
	if (inputLetter == 'f')
	{
		inputLetter = 'F';
	}
	if (inputLetter == 'g')
	{
		inputLetter = 'G';
	}
	if (inputLetter == 'h')
	{
		inputLetter = 'H';
	}
	if (inputLetter == 'i')
	{
		inputLetter = 'I';
	}
	if (inputLetter == 'j')
	{
		inputLetter = 'J';
	}
	if (inputLetter == 'k')
	{
		inputLetter = 'K';
	}
	if (inputLetter == 'l')
	{
		inputLetter = 'L';
	}
	if (inputLetter == 'm')
	{
		inputLetter = 'M';
	}
	if (inputLetter == 'n')
	{
		inputLetter = 'N';
	}
	if (inputLetter == 'o')
	{
		inputLetter = 'O';
	}
	if (inputLetter == 'p')
	{
		inputLetter = 'P';
	}
	if (inputLetter == 'q')
	{
		inputLetter = 'Q';
	}
	if (inputLetter == 'r')
	{
		inputLetter = 'R';
	}
	if (inputLetter == 's')
	{
		inputLetter = 'S';
	}
	if (inputLetter == 't')
	{
		inputLetter = 'T';
	}
	if (inputLetter == 'u')
	{
		inputLetter = 'U';
	}
	if (inputLetter == 'v')
	{
		inputLetter = 'V';
	}
	if (inputLetter == 'w')
	{
		inputLetter = 'W';
	}
	if (inputLetter == 'x')
	{
		inputLetter = 'X';
	}
	if (inputLetter == 'y')
	{
		inputLetter = 'Y';
	}
	if (inputLetter == 'z')
	{
		inputLetter = 'Z';
	}
	return inputLetter;
}

char toLower(char inputLetter)
{
	if (inputLetter == 'A')
	{
		inputLetter = 'a';
	}
	if (inputLetter == 'B')
	{
		inputLetter = 'b';
	}
	if (inputLetter == 'C')
	{
		inputLetter = 'c';
	}
	if (inputLetter == 'D')
	{
		inputLetter = 'd';
	}
	if (inputLetter == 'E')
	{
		inputLetter = 'e';
	}
	if (inputLetter == 'F')
	{
		inputLetter = 'f';
	}
	if (inputLetter == 'G')
	{
		inputLetter = 'g';
	}
	if (inputLetter == 'H')
	{
		inputLetter = 'h';
	}
	if (inputLetter == 'I')
	{
		inputLetter = 'i';
	}
	if (inputLetter == 'J')
	{
		inputLetter = 'j';
	}
	if (inputLetter == 'K')
	{
		inputLetter = 'k';
	}
	if (inputLetter == 'L')
	{
		inputLetter = 'l';
	}
	if (inputLetter == 'M')
	{
		inputLetter = 'm';
	}
	if (inputLetter == 'N')
	{
		inputLetter = 'n';
	}
	if (inputLetter == 'O')
	{
		inputLetter = 'o';
	}
	if (inputLetter == 'P')
	{
		inputLetter = 'p';
	}
	if (inputLetter == 'Q')
	{
		inputLetter = 'q';
	}
	if (inputLetter == 'R')
	{
		inputLetter = 'r';
	}
	if (inputLetter == 'S')
	{
		inputLetter = 's';
	}
	if (inputLetter == 'T')
	{
		inputLetter = 't';
	}
	if (inputLetter == 'U')
	{
		inputLetter = 'u';
	}
	if (inputLetter == 'V')
	{
		inputLetter = 'v';
	}
	if (inputLetter == 'W')
	{
		inputLetter = 'w';
	}
	if (inputLetter == 'X')
	{
		inputLetter = 'x';
	}
	if (inputLetter == 'Y')
	{
		inputLetter = 'y';
	}
	if (inputLetter == 'Z')
	{
		inputLetter = 'z';
	}
	return inputLetter;
}



Revised code
#include<fstream>
#include<iostream>
#include</DIRECTORY/TO/THIS/HEADER/CaseChange.h>

using namespace std;

char letter;

int main (void)
{
char letter='\0';

while(letter!='+')
{
//Prompt
cout<< "Please enter a letter (upper or lower case)"<<endl;

//Get the value of the letter
cin>> letter;

//Beginning of the switch statement that passes the user input letter
//run through the toLower function so that it limits the vowel possibilities
//to 5 instead of 10 
switch (toLower(letter))
{
//Match to the vowel 'a' and prints -> USERINPUT (the letter they input) " is a vowel"
case 'a':
cout<< letter << " is a vowel."<<endl <<endl;
break;

//Same theory as the first case
case 'e':
cout<< letter << " is a vowel."<<endl <<endl;
break;

//Same theory as the first case
case 'i':
cout<< letter << " is a vowel."<<endl <<endl;
break;

//Same theory as the first case
case 'o':
cout<< letter << " is a vowel."<<endl <<endl;
break;

//Same theory as the first case
case 'u':
cout<< letter << " is a vowel."<<endl <<endl;
break;

//A default that prints if the input is not a vowel rather than nothing
default:
cout<< letter << " is not a vowel."<<endl <<endl;
}//End of switch

}//End of while loop
return 0;

}//End of Main


This post has been edited by Sn0wm4n: 26 October 2009 - 03:00 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1