Question #15438

Write a C++ program that would accept two numbers and will accept only characters based on the selection below, otherwise display “invalid input” and ask the user to try again. After accepting those characters, the program will perform the specific operation and the answer will be displayed.
Sample Output:
First Number : 7 Second Number : 4
< + > Addition < * > Multiplication
< – > Subtraction < / > Division
Enter choice: * <enter>
The answer is: 28

Expert's answer

#include <iostream>

using namespace std;

int main()

{

&

float n,k,a,z;

char q;

z=0;

a=0;

cout<<"First Number : ";

cin>>n;

cout<<"Second Number& : ";

cin>>k;&

while(z<1){

cout<<"Enter choice: ";

cin>>q;

switch(q)

{

& case '-' : {

a=n-k;

z=3;

break;

}

case '/' : {

a=n/k;

z=3;

break;

}

case '*' : {

a=n*k;

z=3;

break;

}

case '+' : {

a=n+k;

z=3;

break;

}

&

& default : cout<<"invalid input\n";

}

}

cout<<"The answer is: "<<a<<"\n\n";

system("PAUSE");

}

using namespace std;

int main()

{

&

float n,k,a,z;

char q;

z=0;

a=0;

cout<<"First Number : ";

cin>>n;

cout<<"Second Number& : ";

cin>>k;&

while(z<1){

cout<<"Enter choice: ";

cin>>q;

switch(q)

{

& case '-' : {

a=n-k;

z=3;

break;

}

case '/' : {

a=n/k;

z=3;

break;

}

case '*' : {

a=n*k;

z=3;

break;

}

case '+' : {

a=n+k;

z=3;

break;

}

&

& default : cout<<"invalid input\n";

}

}

cout<<"The answer is: "<<a<<"\n\n";

system("PAUSE");

}

## Comments

## Leave a comment