473,388 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Code using C++

16
Create the equivalents of a four-function calculator. The program should request the user to enter a number, an operator, and another number. (Use floating point). It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. Use switch case statement to select the operation. Finally display the result.
When it finishes the calculation, the program should ask if the user wants to do another calculation. The response can be ‘y’ or ‘n’.

Please help me in writing the code.
Oct 16 '06 #1
6 3103
tyreld
144 100+
Generally, nobody is going to just write code for something that looks like homework or a learning exercise. Try it yourself. This is the best way to learn. When you get stuck post your code and ask specific questions about what is posing a problem. Once you do this people will be more likely to give you help and feedback. This applies to your other 2 posts.
Oct 16 '06 #2
Saba
16
i have tried but i got struck at one place.....plz help me if u can....
i am writting the code.....when i compile.....it does not switch again when i enter "y".....plz help me as soon as possible.
#include <iostream.h>

main()
{
int choice;
float firstNum, secondNum;
char oper, y, Y, n, N;

do
{
cout<< "Please enter the first number: " << endl;
cin >> firstNum;

cout << "Please enter the second number: " << endl;
cin >> secondNum;

cout << "Please enter the operator: "<<endl;
cin>> oper;

switch(oper)
{
case '+':
cout<< "Sum = " << firstNum + secondNum << endl;
break;

case '-':
cout<< "Difference = " << firstNum - secondNum << endl;
break;

case '*':
cout<< "Product = " << firstNum * secondNum << endl;
break;

case '/':
cout<< "Divsion = " << firstNum / secondNum << endl;
break;

}

cout<< "Do you want to do more calculations(y/n)?";
cin>> choice;
}
while ( (choice==y) && ( choice==Y) );

}
Oct 16 '06 #3
tyreld
144 100+
There are 3 things wrong.

First, the variable "choice" should be of type "char".

Expand|Select|Wrap|Line Numbers
  1. char choice;
  2.  
Second, the comparison should be against char literals not undefined variables as you have it now.

Expand|Select|Wrap|Line Numbers
  1. (choice == 'y')
  2.  
Finally, the while condition should be OR'ed not AND'ed. You want to check that the input to choice was either 'y' ***OR*** 'Y'. The AND operation would imply that choice have both 'y' and 'Y' as a value. Which isn't possible.

Expand|Select|Wrap|Line Numbers
  1. while ((choice == 'y') || (choice == 'Y'));
  2.  
Oct 16 '06 #4
tyreld
144 100+
On a side note. Your main method should have one of the following forms:
int main(void) { ... }
or
int main(int argc, char **argv) { ... }
Your main shold always return a value. You either need to return 0 for success or one of the macros defined in <stdlib.h> or <cstdlib>. Namely "EXIT_SUCCESS" and "EXIT_FAILURE."

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2.  
  3. int main(void)
  4. {
  5.    // do a bunch of stuff
  6.  
  7.    return EXIT_SUCCESS;
  8. }
  9.  
Oct 16 '06 #5
#include <iostream>
using namespace std;
int main()
{
char op; // ** must be outside loop
do // ** start loop
{
int number1, number2;
cout << " Enter a number: ";
cin >> number1;
cout << "Enter another number: ";
cin >> number2;
cout << "Enter a valid operator: ";

cin >> op;
switch (op)
{
case '+':
cout << "The result is: " << number1 + number2 << endl;
break;
case '-':
cout << "The result is: " << number1 - number2 << endl;
break;
case '*':
cout << "The result is: " << number1 * number2 << endl;
break;
case '/':
cout << "The result is: " << number1 / number2 << endl;
break;
default: cout << "You Have entered an invalid operator." << endl;
}
cout<< "do you want to another calculation? (y or n)";
cin >> op;
}
while (op == 'y'); // ** loop while y is entered
return 0;
}
Sep 23 '16 #6
/* Source code to create a simple calculator for addition, subtraction, multiplication and division using switch...case statement in C++ programming. */

# include <iostream>
using namespace std;
int main()
{
char o;
float num1,num2;
cout << "Enter operator either + or - or * or /: ";
cin >> o;
cout << "Enter two operands: ";
cin >> num1 >> num2;
switch(o) {
case '+':
cout << num1+num2;
break;
case '-':
cout << num1-num2;
break;
case '*':
cout << num1*num2;
break;
case '/':
cout << num1/num2;
break;
default:
/* If operator is other than +, -, * or /, error message is shown */
cout << "Error! operator is not correct";
break;
}
return 0;
}
Sep 23 '16 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
1
by: Novice | last post by:
Hi all, I'm afraid this is the second posting of this information as I didn't get a response on the previous post. I will try to shorten my message (i.e. be more concise) in the hopes that it will...
0
by: Colin | last post by:
Hi there, I really need your help on this. I'm trying to learn to using the VS.2003 to create a User Control. In my aspx code has no problem to use the property "grossWaye" that has "register" in...
2
by: bob | last post by:
Hello, I want to show progress to the user while some method is running so I thought I'd use a progress bar. But I don't see how I can do this without writing GUI code in the model? In...
192
by: Vortex Soft | last post by:
http://www.junglecreatures.com/ Try it and tell me what's happenning in the Microsoft Corporation. Notes: VB, C# are CLS compliant
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
4
by: DELESTRE Christophe | last post by:
I’m sorry to disturb you but I have a problem on .NET development, and I’m need some help to resolve it if it’s possible. I have an aspx page with src property (no dll for my web...
2
by: lewisms | last post by:
Hello all, I am quite new to c++/. Net so please don't shoot me down for being a newbie. Any way I am trying to make a simple multithreading program that is just to learn the ideas behind it...
1
by: cnixuser | last post by:
Hello, I am having a problem that I believe is related to the way a stream reader object looks for a text file by default. What I am doing is using a StreamReader object to read the text of a text...
4
by: =?Utf-8?B?dmlwZXJ4MTk2Nw==?= | last post by:
We are having an issue with an application we are developing. We have a Legacy COM DLL in C++ that we have converted to Visual Studio 2008. This COM DLL has methods that are calling Managed C#...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.