anonymous
  • anonymous
Plss sum1 help me to construct a C++ program for the following question. Construct a program in C++ to calculate the value when you perform the following operations using functions and keyboard input. Addition Subtraction Multiplication Integer division Real division
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
schrodinger
  • schrodinger
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
first, you'll need a way to let the user enter values and commands using the keyboard. Do you know how to do that?
anonymous
  • anonymous
It will b cout<<"Enter the values"; Isnt t?
anonymous
  • anonymous
No, cout is for output, not input. Please find out how to do keyboard input in C++, then come back.

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
cout<<"Enter the values"; cin>>values;
anonymous
  • anonymous
Please construct a complete program that will let the user enter a value, and then echo back that value.
anonymous
  • anonymous
Suppose f the ques is to construct a C++ program to display the registration number #include using namespace std; int main() { int regno; cout<<"Enter the Registration Number"<>regno; cout<
anonymous
  • anonymous
looks good so far (you can determine yourself whether it is correct by running it). Since the user is meant to be able to provide commands (as well as numbers), an int input will not suffice. Change your program to allow the user to enter something like "+ 3 4". You can change the prompt to say something like "Please enter a command" or whatever you like.
anonymous
  • anonymous
Hi @future_engineer can u help me vth this?
anonymous
  • anonymous
@atjari , hii bro...I am new into CSomputer course...haven't learnt much abt C yet...:(
anonymous
  • anonymous
It's k. Thanx a lot.
anonymous
  • anonymous
:)
anonymous
  • anonymous
The approach would be, initiate a switch case for all those operations after declaring them as functions, then declare a function for the user to input data. Then define each function. I know its an old approach, i used it in C for some ADTs.
anonymous
  • anonymous
can u jst show me hw that program vl luk lyk f u dnt mind? I stil am unabl to get a clear f the suitabl pro for my ques.
anonymous
  • anonymous
I can show you a similar program, but it is in C, http://www.w3professors.com/Data/Courses/Data-Structure/Programs/Stack/stack-using-array.pdf Look closely how the functions are used.
anonymous
  • anonymous
which part of my last answer did you not understand? Yes, at some point you'll need to create functions that actually execute the operations, but they won't help you if the user cannot type in which operations he wants to choose.
anonymous
  • anonymous
Quick google gives me this, http://cpp-programs.blogspot.in/2011/03/c-program-to-do-additionsubtraction-and.html Its not the solution, but some base to start on, atleast thats how i start.
anonymous
  • anonymous
Jst a moment let me write and c.
anonymous
  • anonymous
BTW, "real division" strictly speaking cannot be done on a computer, because irrational numbers cannot be represented.
anonymous
  • anonymous
if v r to use the case statement where do v insert t? in the main functu=ion or sub func?
anonymous
  • anonymous
You need to decide how the user will decide on the operations. The question leaves out quite a few important specifications: how does the user choose the operations? (what is the syntax, i. e. "+ 3 4" =prefix, "3 + 4" =infix; the first one is easier to program but harder for to use. Are combinations allowed? "4 + 3 * 5" only pairs of operands, or would add(6,9,7) be allowed?
anonymous
  • anonymous
t?
anonymous
  • anonymous
do you mean "where do we insert it?"
anonymous
  • anonymous
ya. coz we need to ask the user to insert the choice number of the operation.
anonymous
  • anonymous
@nczempin why induce complications for multiple operations? Lets get over with a simple program and then analyse these infix, prefix, postfix tweaks eh?
anonymous
  • anonymous
@atjari the switch cases take care of that.
anonymous
  • anonymous
That's the problem I hav nw. I dnt knw wher to insert it @arcticf0x .
anonymous
  • anonymous
I am extremely sorry @nczempin . V havnt learnt infix, postfix etc.
anonymous
  • anonymous
i mean we havnt learnt them yet.
anonymous
  • anonymous
nobody's introducing any complications; atjari will have to decide what to use, since the question doesn't specify it. If we are free to choose (no idea if we are) then the easiest to program would assume 2 operands, no combinations. In that case it would be easiest to choose the operation from 5 options (and we can just use numbers" and the prompt for the 2 operands. Then he wouldn't need to parse the string.
anonymous
  • anonymous
Well, by now I have suspected this, so let's just assume the simplest possible way, in which you let the user input 3 numbers: operation operand1 operand2
anonymous
  • anonymous
and then you can do a switch on the operation, and in the case you do the calculation
anonymous
  • anonymous
or, rather, in the case section you call the appropriate function, since the question asks you to use functions.
anonymous
  • anonymous
Have you learnt about enumerations?
anonymous
  • anonymous
No
anonymous
  • anonymous
something like this: switch (operation){ case 1: result =add(operand1,operand2); break; case 2: result = subtract(operand1, operand2); break; ... default: // some kind of error handling.
anonymous
  • anonymous
ok then just use ints, like I did in the example
anonymous
  • anonymous
you get the idea?
anonymous
  • anonymous
Ya a little.
anonymous
  • anonymous
well, see how far you get with this rough outline; you'll have to declare the variables, the functions, the text printed out etc.
anonymous
  • anonymous
can u check the attachment and say if I hav started correctly?
1 Attachment
anonymous
  • anonymous
the functions need parameters, e. g. "int sum(int a, int b);" And of course you'll need to define them too at some point, but just declaring works for now (you'll get link errors)
anonymous
  • anonymous
and since you're supposed to support "real division" (by which I'm assuming they mean "floating point division", your you may want to allow floats or doubles to be used. definitely in the realdiv function, and once you have that, it's up to you to decide whether you want to allow only integers as operands, or if floats/doubles are also allowed. Since you'll have to allow float or double for realdiv, this may make sense (although a program that only allows integers should also be fine; integerdiv(5, 2) = 2, realdiv(5,2) = 2.5
anonymous
  • anonymous
If u dnt mind cn u do me one more favour?
anonymous
  • anonymous
It's past midnite here. If u can write the whole program and show me I vl me much more grateful to u.
anonymous
  • anonymous
Only if you dnt mind plssssssss. It's 1a.m in my place.
anonymous
  • anonymous
@nczempin
anonymous
  • anonymous
sorry mate, I won't just program it for you. I have other things to do. Just go to bed and do it tomorrow
anonymous
  • anonymous
It's k. Anyways thanx. I need to submit t in the morning.
anonymous
  • anonymous
Thanx a lot for helping me.
anonymous
  • anonymous
just put the switch in the main method, do the functions like this int add(int a, int b); int subtract(int a, int b); ... float realdiv(int a, int b); int main(){ int operation; int operand1; int operand2; // here do the inputting, like you previously did for just 1 variable, but here you collect all three values // here you insert my switch example } int add(int a, int b){
anonymous
  • anonymous
int add(int a, int b){ return a+ b; } // and the others int integerdiv(int a, int b){ return a/b; } float realdiv(int a, int b){ return (float) a /(float) b; }
anonymous
  • anonymous
now just put all those pieces together and correct the errors.
anonymous
  • anonymous
So kind of u mate. Vl never forget this help of urs. Thanx a lot.
anonymous
  • anonymous
@nczempin
anonymous
  • anonymous
When I compile I get operation as undeclared.
anonymous
  • anonymous
I marked the place where the cout and cin goes in the comment // here do the inputting,
anonymous
  • anonymous
well, in the code I gave you it's declared.
anonymous
  • anonymous
if you put the switch in the main function (where I put the comment that says // here you insert my switch example) and also the "int operation;" it should be declared.
anonymous
  • anonymous
if i compile nw it says case label not within switch statement
anonymous
  • anonymous
please try to figure out these compile errors on your own. The error message should help you.
anonymous
  • anonymous
here result is int or float?
anonymous
  • anonymous
please show some effort, don't just keep asking others for the answer. What do you think? int or float?
anonymous
  • anonymous
Extremely sorry for troubling u a lot. it's float.
anonymous
  • anonymous
If I compile I dnt get the correct answer is not displayed.
anonymous
  • anonymous
1 Attachment
anonymous
  • anonymous
yes, because there is an error in your code.
anonymous
  • anonymous
bt the error is nt displayed.
anonymous
  • anonymous
a programming error can still occur even if the program compiles correctly. debug the code, see what happens. What makes you think that your code would display the correct answer?
anonymous
  • anonymous
I havnt learnt about debugging except its definition.
anonymous
  • anonymous
@nczempin
anonymous
  • anonymous
well, before your next programming assignment you should learn how to set breakpoints and step through your code in whatever IDE you are using. In the meantime, I'll just give you the answer: You are displaying the result before you are computing it.
anonymous
  • anonymous
I assure u that I vl definitely learn t bt for the moment vl u pls tell me where to insert t?
anonymous
  • anonymous
oh come on. where do you compute the result?
anonymous
  • anonymous
when I compile it performs only subtraction for any any choice given
anonymous
  • anonymous
@nczempin
anonymous
  • anonymous
you forgot some break statements. that would explain you getting division on choices 3 and 4, but not subtraction I gotta go now, sry.

Looking for something else?

Not the answer you are looking for? Search for more explanations.