Monday, May 5, 2008

Using Case Statement in C++, menu driven program in C++

Write a program to display the following menu and accept a choice number. If an invalid choice is entered, appropriate error message must be displayed. Otherwise the choice number entered must be displayed.

MENU

1. Create a data file

2. Display a data file

3. Edit a data file

4. Exit

Choice :


PROGRAM:

#include <iostream.h>

#include <conio.h>


void main() {

int choice;

clrscr();

cout<<"MENU";

cout<<"1. Create a data file\n";

cout<<"2. Display a data file\n";

cout<<"3. Edit a data file\n";

cout<<"4. Exit\n\n";

cout<<"\nEnter your choice: ";

cin>>choice;

switch(choice) {

case 1: cout<<"you entered choice 1\n";

cout<<"Create a data file\n";


            break;

case 2: cout<<"you entered choice 2\n";

cout<<"Display a data file\n";

break;

case 3: cout<<"you entered choice 3\n";

cout<<"3. Edit a data file\n";

break;

case 4: cout<<"you entered choice 4\n";

cout<<"Exit";

break;

default: cout<<"Please enter a valid choice";

}

getch();


}//end of program

No comments: