#include <iostream> #include<conio.h> using namespace std; //function prototypes
ID: 3726221 • Letter: #
Question
#include <iostream>
#include<conio.h>
using namespace std;
//function prototypes
void showMenu();
void help();
void arithmetic();
void relational();
void logical();
//main method
int main()
{
char choice; //variable to store user choice from menu
do
{
showMenu(); //function call to showMenu(), which displays menu for user
cin >> choice;
switch (choice) // switch statement for choice
{
case 'H':
help(); //call help() if user choice is H
break;
case 'A':
arithmetic(); // call arithemtic() if user choice is A
break;
case 'R':
relational(); // call relational() if user choice is R
break;
case 'L':
logical(); //call logical() if user choice is L
break;
case 'Q':
case 'q':
exit(0); //call exit(0) to Quit, if user choice is Q or q
default:
cout << "Invalid Choice. Try Again!" << endl; // print Invalid choice for other choices
}
}while (1);
return 0;
}
void showMenu() //function to display MENU
{
cout << "--------------------------------------------------------------------"<<endl;
cout << "MENU" << endl;
cout << "H: Help ";
cout << "A: Arithmetic ";
cout << "R: Relational ";
cout << "L: Logical ";
cout << "Q\q: Exit " << endl;
cout << "Enter your choice :";
}
void help() //function to display HELP
{
cout << "--------------------------------------------------------------------"<<endl;
cout << "Welcome to Help" << endl;
cout << "This program demonstrates the use of arithmetic, relational and logical operators." << endl;
cout << "select option 2 to perform arithmetic operation" << endl;
cout << "select option 3 to use realtional operator" << endl;
cout << "select option 4 to use logical operator" << endl;
cout << "Press any key to continue...."<<endl;
char wait = getch();
}
void arithmetic() //function to perform ARithmetic operations
{
int x, y,ch;
cout << "--------------------------------------------------------------------"<<endl;
cout <<"Enter two integers:"<<endl; //ask two integers
cin >> x >> y;
cout <<"select operation: 1. Addition"<<endl;
cin >> ch;
if(ch==1){ //if ch==1, add two numbers and display the result
cout<<"Addition of two numbers is: "<< (x+y)<<endl;
cout << "Press any key to continue...."<<endl;
char wait = getch();
}
else{ //else display Invalid operation.
cout<<"Invalid Operation"<<endl;
cout << "Press any key to continue...."<<endl;
char wait = getch(); //waiting for key stroke
system("cls"); //clearing the screen
}
}
void relational() //function to perform Realational operations
{
int x, y,ch;
cout << "--------------------------------------------------------------------"<<endl;
cout <<"Enter two integers(X and Y):"<<endl; //ask two integers
cin >> x >> y;
cout <<"select operation: 1. check less than(<)"<<endl;
cin >> ch;
if(ch==1){ //if ch==1, check X < Y and display the result
if(x<y)
cout<< "X is lesser than Y"<<endl;
else
cout<< "Y is lesser than X"<<endl;
cout << "Press any key to continue...."<<endl;
char wait = getch();
}
else{ //else display Invalid operation.
cout<<"Invalid Operation"<<endl;
cout << "Press any key to continue...."<<endl;
char wait = getch(); //waiting for key stroke
system("cls"); //clearing the screen
}
}
void logical() //function to perform Logical operations
{
int x, y,ch;
cout << "--------------------------------------------------------------------"<<endl;
cout <<"Enter two integers(X and Y):"<<endl; //ask two integers
cin >> x >> y;
cout <<"select operation: 1. Logical AND (&)"<<endl;
cin >> ch;
if(ch==1){ //if ch==1, perform Logical AND and display the result
cout<<"Result of Logical AND of two numbers is: "<< (x&y)<<endl;
cout << "Press any key to continue...."<<endl;
char wait = getch();
}
else{ //else display Invalid operation.
cout<<"Invalid Operation"<<endl;
cout << "Press any key to continue...."<<endl;
char wait = getch(); //waiting for key stroke
system("cls"); //clearing the screen
}
}
This code isn't workinf for me can someone identify the problem and fix it for me, Thanks!!!
Explanation / Answer
/*Ran the code in linux it is runnning fine with few chages that we need to do as conio.h is not available in linux and added <cstdlib> as the definition of exit and system were present here,also all cls were changed to clear as I ran it in linux, only mistake I found was that a method was named logical operations and bitwise AND operation was being carried out, still this is not creating any errors or problems, please mention the problem you are facing , below is the code wich I tested in linux*/
#include <iostream>
#include <cstdlib>//For exit and system to work
//#include<conio.h> //it isn't available in linux
using namespace std;
//function prototypes
void showMenu();
void help();
void arithmetic();
void relational();
void logical();
//main method
int main()
{
char choice; //variable to store user choice from menu
do
{
showMenu(); //function call to showMenu(), which displays menu for user
cin >> choice;
switch (choice) // switch statement for choice
{
case 'H':
help(); //call help() if user choice is H
break;
case 'A':
arithmetic(); // call arithemtic() if user choice is A
break;
case 'R':
relational(); // call relational() if user choice is R
break;
case 'L':
logical(); //call logical() if user choice is L
break;
case 'Q':
case 'q':
exit(0); //call exit(0) to Quit, if user choice is Q or q
default:
cout << "Invalid Choice. Try Again!" << endl; // print Invalid choice for other choices
}
}while (1);
return 0;
}
void showMenu() //function to display MENU
{
cout << "--------------------------------------------------------------------"<<endl;
cout << "MENU" << endl;
cout << "H: Help ";
cout << "A: Arithmetic ";
cout << "R: Relational ";
cout << "L: Logical ";
cout << "Q\q: Exit " << endl;
cout << "Enter your choice :";
}
void help() //function to display HELP
{
cout << "--------------------------------------------------------------------"<<endl;
cout << "Welcome to Help" << endl;
cout << "This program demonstrates the use of arithmetic, relational and logical operators." << endl;
cout << "select option 2 to perform arithmetic operation" << endl;
cout << "select option 3 to use realtional operator" << endl;
cout << "select option 4 to use logical operator" << endl;
cout << "Press any key to continue...."<<endl;
cin.get();
}
void arithmetic() //function to perform ARithmetic operations
{
int x, y,ch;
cout << "--------------------------------------------------------------------"<<endl;
cout <<"Enter two integers:"<<endl; //ask two integers
cin >> x >> y;
cout <<"select operation: 1. Addition"<<endl;
cin >> ch;
if(ch==1){ //if ch==1, add two numbers and display the result
cout<<"Addition of two numbers is: "<< (x+y)<<endl;
cout << "Press any key to continue...."<<endl;
cin.get();
}
else{ //else display Invalid operation.
cout<<"Invalid Operation"<<endl;
cout << "Press any key to continue...."<<endl;
cin.get(); //waiting for key stroke
system("clear"); //clearing the screen in linux
}
}
void relational() //function to perform Realational operations
{
int x, y,ch;
cout << "--------------------------------------------------------------------"<<endl;
cout <<"Enter two integers(X and Y):"<<endl; //ask two integers
cin >> x >> y;
cout <<"select operation: 1. check less than(<)"<<endl;
cin >> ch;
if(ch==1){ //if ch==1, check X < Y and display the result
if(x<y)
cout<< "X is lesser than Y"<<endl;
else
cout<< "Y is lesser than X"<<endl;
cout << "Press any key to continue...."<<endl;
cin.get();
}
else{ //else display Invalid operation.
cout<<"Invalid Operation"<<endl;
cout << "Press any key to continue...."<<endl;
cin.get(); //waiting for key stroke
system("clear"); //clearing the screen in linux
}
}
void logical() //function to perform Logical operations
{
int x, y,ch;
cout << "--------------------------------------------------------------------"<<endl;
cout <<"Enter two integers(X and Y):"<<endl; //ask two integers
cin >> x >> y;
cout <<"select operation: 1. Logical AND (&)"<<endl;
cin >> ch;
if(ch==1){ //if ch==1, perform Logical AND and display the result
cout<<"Result of Logical AND of two numbers is: "<< (x&y)<<endl;/*Mistake (not causing any problem though) here & means bitwise AND operation not logical AND logical AND is to be done using &&*/
cout << "Press any key to continue...."<<endl;
cin.get();
}
else{ //else display Invalid operation.
cout<<"Invalid Operation"<<endl;
cout << "Press any key to continue...."<<endl;
cin.get(); //waiting for key stroke
system("clear"); //clearing the screen in linux
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.