Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C
#include <iostream>using namespace std;

int main()
{
    cout <<"S = Sales"<< endl;
    cout <<"A = Accounting"<< endl;
    cout <<"E = Engineering!"<< endl;
    cout <<"H = Human Resouces"<< endl;
    cout <<"F = Finance!"<< endl;
    
    char dept;
    cout << "Enter your selection: ";
    cin >> dept;
switch (dept)
    {
        cout<<dept;
       case S:  cout << "Sales\n";  // fall through
       case A:  cout << "Accounting\n"; // fall through
       case E:  cout << "Engineering!\n"; // fall through
       case H:  cout << "Human Resouces\n"; // fall through
       case F:  cout << "Finance!\n";
              break;
     }
   cout << "\n\n";
    return 0;
}


please can someone help me out with this

Write a C++ program that receives input for 5 children and prints the names of their teacher inside a loop. (Teachers names are fictitious. So give your own name).

10. Write a program to display the following menu:

• S – Sales
• A – Accounting
• E – Engineering
• H – Human Resource
• F – Finance
1. Based on the user’s selection, display the department name.
• Hint : use Switch…case.

[edit]unnecesary code block removed[/edit]
Posted
Updated 26-Nov-12 22:39pm
v4

You should do it like this:
C++
#include <iostream>
using namespace std;

int main()
{
    cout <<"S = Sales"<< endl;
    cout <<"A = Accounting"<< endl;
    cout <<"E = Engineering!"<< endl;
    cout <<"H = Human Resouces"<< endl;
    cout <<"F = Finance!"<< endl;
    
    char dept;
    cout << "Enter your selection: ";
    cin >> dept;
	switch (dept)
    {
        cout<<dept;
	case 'S':  cout << "Sales\n"; break; // fall through
	case 'A':  cout << "Accounting\n";break; // fall through
	case 'E':  cout << "Engineering!\n";break; // fall through
	case 'H':  cout << "Human Resouces\n"; break;// fall through
	case 'F':  cout << "Finance!\n";break;
	default:cout<<"input error!!!"<<endl;
		break;
	}
	cout << "\n\n";
    return 0;
}
Because the dept is a char type, so the behind the case should add ' '.
 
Share this answer
 
v3
C++
#include <iostream>

using namespace std;

void Sales()
{
    cout << "Sales";
}
void Accounting()
{
    cout << "Accounting";
}
void Engineering()
{
    cout << "Engineering";
}
void HumanResouces()
{
    cout << "Human Resouces";
}
void Finance()
{
    cout << "Finance";
}
int main()
{
  int dept;

  cout<<"1. Sales\n";
  cout<<"2. Accounting\n";
  cout<<"3. Engineering\n";
  cout<<"4. Human Resouces\n";
  cout<<"5. Finance\n";
  cout<<"6. Exit\n";
  cout<<"Selection: ";
  cin>> dept;
  switch ( dept ) {
  case 1:            // Note the colon, not a semicolon
    Sales();
    break;
  case 2:            // Note the colon, not a semicolon
    Accounting();
    break;
  case 3:            // Note the colon, not a semicolon
    Engineering();
    break;
  case 4:            // Note the colon, not a semicolon
    HumanResouces();
    break;
  case 5:            // Note the colon, not a semicolon
    Finance();
    break;
  case 6:            // Note the colon, not a semicolon
    cout<<"Thank you for your Selecting!\n";
    break;
  default:            // Note the colon, not a semicolon
    cout<<"Error or  bad input, quitting\n";
    break;
  }
  cin.get();
}
 
Share this answer
 
v2
should use break for each case statement.
consider the switch statement format.
C++
switch (expression)
{
case valueOne: statement;
                   break;
case valueTwo: statement;
                   break;
....
case valueN:   statement;
                   break;
default:       statement;
}


NOTE: It is almost always a good idea to have a default case in switch statements. If you have no other need for the default, use it to test for the supposedly impossible case, and print out an error message; this can be a tremendous aid in debugging.


so modify your switch statement like this
C++
switch (dept)
{
    cout<<dept;
   case S:  cout<< "Sales\n";  // fall through
            break;
   case A:  cout<< "Accounting\n"; // fall through
            break;
   case E:  cout<< "Engineering\n"; // fall through
            break;
   case H:  cout<< "Human Resouces\n"; // fall through
            break;
   case F:  cout<< "Finance!\n";
            break;
   default: cout<<"Enter a valied character"<<endl;
 }
 
Share this answer
 
Comments
Shyne.my 25-Nov-12 7:43am    
Please i have done it like this but Please can you help me if i want to use Char for the variables instead of int data type,i tried using it but not work
please i have tried and tried Permalink can you help me out ,how do i figure it out
 
Share this answer
 
Comments
Nelek 25-Nov-12 7:15am    
Please don't post solutions to add information, to ask something or to comment another user.
- To add information to your message, you can use the widget "Improve question" / "Improve solution" at the bottom of your text.
- To ask/answer a user, you can use the widget "Have a question or comment?" (as I am doing right now with you) or the widget "reply" in another comment.
Review your switch statement[^].

For one each case should have it own break; and also consider adding a default: case for when incorrect input is supplied.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900