473,749 Members | 2,402 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 3129
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
13410
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
3917
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
2766
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
9495
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
2710
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
1600
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
4408
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
2896
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
2232
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
8996
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
9388
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
9333
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,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
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
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.