473,699 Members | 2,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Code using C++

16 New Member
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 3126
tyreld
144 New Member
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 New Member
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 New Member
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 New Member
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_SUCCE SS" and "EXIT_FAILU RE."

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
AR JAlbani
2 New Member
#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
AR JAlbani
2 New Member
/* 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
13362
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 comments on past experience, research articles, comments on the matter would be much appreciated. I suspect something like C would be the best based on comments I received from the VB news group. Thanks for the help in advance James Cameron
1
3914
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 make it easier for someone (i.e. a Microsoft person) to digest the information and respond to it. I am a C++ and Java developer with over 3 years of industry experience. I've written low level C++ code, in addition to web clients that use web...
0
622
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 the aspx code if I don't use "Codebehind" directive. If I try to move the <script></script> coding the Codebehind of the VS2003. I don't know how to get the property "grossWaye". I post both User Control and .aspx codes for your help. Thanks in...
2
2761
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 Smalltalk I would fire of notification exceptions that a progress bar would catch, increment progress, and then allow to continue (or no
192
9467
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
2701
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 know nothing about ASP. He can build the design of the pages in HTML with tables, labels, textboxes etc. But then I would need to change them to ASP.net objects and write the code to make the page work (normally I do this as I go - can't do this...
4
1597
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 application, all pages are JIT compiled on request), and I want link a assembly with the “src” attribute, like <%@ Assembly Src=”/MyWebApplication/Common/MyAssemblyClass.cs” %>
2
4405
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 (before I incorporate them in another program). I just can’t seem to get a non-static call to work in my thread that has access to the Form1 variables and controls I need. I can call a non-static function using another class but then I can seem to get in...
1
2893
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 file which includes some html code to populate html formatted content as the text of an asp:label (<asp:label>). The reading of the text file itself goes just fine ;however, this only occurs when I use an absolute file path which will not work of...
4
2228
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# assemblies as pass thru to support legacy applications in an effort to move our code to the new Code base. Our COM Object can be instantiated on Windows XP in any COM supported environment using Visual C++, Visual Basic, ASP.NET or ASP and works...
0
8705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9197
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9054
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8941
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6549
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3071
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.