Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<iostream>
using namespace std;
int main()
{
float a,b;
char ch,ch1;
int n;
	cout<<"Calculator"<<endl;
	cout<<"Please select below"<<endl;
	cout<<"1.Addition"<<endl;
	cout<<"2.Subtration"<<endl;
	cout<<"3.Multiplication"<<endl;
	cout<<"4.Division"<<endl;
	cout<<"5.Exit";
	cin>>ch;
	do
	
	{
			
		
	switch(ch)
	
	{
		case 1:
		cout<<"Enter the nos for addition = "<<endl;
		cin>>a>>b;
		cout<<"The result is = "<<a+b<<endl;
		break;
		case 2:
		cout<<"Enter the nos for subtration = "<<endl;
		cin>>a>>b;
		cout<<"The result is = "<<a-b<<endl;
		case 3:
		cout<<"Enter the nos for Multiplication = "<<endl;
		cin>>a>>b;
		cout<<"The result is = "<<a*b<<endl;
		case 4:
		cout<<"Enter the nos for Division = "<<endl;
		cin>>a>>b;
		cout<<"The result is = "<<a/b<<endl;
		case 5:
		cout<<" "<<endl;
		exit(0);
		cout<<"Do you want to continue(Y/N)"<<endl;
		default:
			cout<<"Wrong";
	}

}
	while(ch1=='y' || ch1=='Y');
}


What I have tried:

i have tried some but they are not working.
Posted
Updated 19-Jul-17 9:36am
v2
Comments
ZurdoDev 19-Jul-17 13:58pm    
What do you mean "it's not working?"
jeron1 19-Jul-17 14:00pm    
try
case '1':
case '2':
.
.

The number 1 is not the same as the character '1' - so when your switch compares them, it never finds a match.
Try this:
C++
switch(ch)
    {
    case '1':
    cout<<"Enter the nos for addition = "<<endl;
    cin>>a>>b;
    cout<<"The result is = "<<a+b<<endl;
    break;
And so forth for the other cases - it should work then.
 
Share this answer
 
Advice: Learn to indent your code, it helps reading.
C++
#include<iostream>
using namespace std;
int main()
{
	float a,b;
	char ch,ch1;
	int n;
	cout<<"Calculator"<<endl;
	cout<<"Please select below"<<endl;
	cout<<"1.Addition"<<endl;
	cout<<"2.Subtration"<<endl;
	cout<<"3.Multiplication"<<endl;
	cout<<"4.Division"<<endl;
	cout<<"5.Exit";
	cin>>ch;
	do
	{
		switch(ch)
		{
		case 1:
			cout<<"Enter the nos for addition = "<<endl;
			cin>>a>>b;
			cout<<"The result is = "<<a+b<<endl;
			break;
		case 2:
			cout<<"Enter the nos for subtration = "<<endl;
			cin>>a>>b;
			cout<<"The result is = "<<a-b<<endl;
		case 3:
			cout<<"Enter the nos for Multiplication = "<<endl;
			cin>>a>>b;
			cout<<"The result is = "<<a*b<<endl;
		case 4:
			cout<<"Enter the nos for Division = "<<endl;
			cin>>a>>b;
			cout<<"The result is = "<<a/b<<endl;
		case 5:
			cout<<" "<<endl;
			exit(0);
			cout<<"Do you want to continue(Y/N)"<<endl;
		default:
			cout<<"Wrong";
		}
	}
	while(ch1=='y' || ch1=='Y');
}

-----
After you applied the correction of solution 1, you will see that your code have other problems. Use the debugger to see what your code is really doing, you should understand pretty quickly why your code fail.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

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