Hi there, Was wondering if somebody could help me. I'm trying to learn C++ and i've decided on a (very) simple project to try and expand my knowledge of the syntax/language. I have got this code so far: Code: // Simple add/subtract CLI program #include <iostream> using namespace std; int subtraction (int a, int b) // Subtraction instruction set { int r; r=a-b; return (r); } int addition (int a, int b) // Addition instuction set { int r; r=a+b; return (r); } int main () // Main instruction set { int z; int y; int a; int b; z = addition (a,b); y = subtraction (a,b); cout << "Please enter an integer: "; cin >> a ; cout << " .\n" <<"Add or subtract?(z/y): "; cin >> ; // Cin above needs a pointer to choose z/y add or subtract, help?? cout << "Choose a number to add or subtract: "; cin >> b; cout << "The result is " << ; //Needs to use (r) for return==result from add or subtract } I was hoping somebody would be able to help me with making this code do what i want, which is enter value a choose add or subtract (z/y) then choose another number to add/sub and display the result with cout. Thanks in advance
So let me get this straight: enter a number -> select whether to add or subtract -> enter the number to subtract -> output result If so then you could use a case statement for each possibility to access the different subroutines. I'd program it for you, but it sounds like homework and you'll learn more if you program it yourself...
That's right. Would you mind explaining what a case statement is please? p.s thanks for replying and editing my post =D +rep
There are a couple of problems with your code. First of all, you have this at the start of your program: Code: z = addition (a,b); y = subtraction (a,b); This will give either zero or a weird number because you haven't initialised (i.e. set to a value) the variables a and b. You want to perform the subtraction after the user has supplied the values for these variables. Secondly, it would be more efficient to have a single variable to store the result, rather than using seperate variables for addition or subtraction. That saves you having to decide which to output. Something like this would do the trick (pseudocode - I'm not doing homework for you). Code: int a, b, result; char operation; // Get the input data get a and b from the user get the operation type (addition or subtraction) // Now do the addition/subtraction if the operation is addition result = addition(a, b); else if the operation is subtraction result = subtraction(a, b); // Output the results cout << "the result is " << result << endl; For switch/case statements, look at the C++ examples on Wikipedia (http://en.wikipedia.org/wiki/Switch_statement) and it should become fairly clear.
Cheers hitman that makes sense now, dunnno why i didn't think to use if p.s thanks for the speedy reply +rep
Apologies for double post, but i got it using your code hitman with my code(for declaration of add/sub actions) it works. Code: // Simple add/subtract CLI program #include <iostream> using namespace std; int subtraction (int a, int b) // Subtraction instruction set { int r; r=a-b; return (r); } int addition (int a, int b) // Addition instuction set { int r; r=a+b; return (r); } int main () { int a, b, result; char operation; // Get the input data cout << "Enter an integer:" << endl; cin >> a; cout << "Which operation do you wish to perform? (a = add, s = subtract)" << endl; cin >> operation; cout << "Enter another integer:" << endl; cin >> b; // Now do the addition/subtraction if(operation == 'a') result = addition(a, b); else if(operation == 's') result = subtraction(a, b); // Output the results cout << "The result is " << result << endl; } Thanks alot guys, will check out that wiki, but i also just found this guide:http://www.cplusplus.com/doc/tutorial/ which looks pretty good.
You should always initlialise all variables when declaring them otherwise they are undefined which can cause problems. e.g. int x = 0;
Your subroutines could be optimised a little. At the moment you are creating an additional variable where you don't need one. Eg: Code: int addition(int a, int b) { return a+b; } I must admit, I haven't played with C++ much, it does look a lot nicer than C to work with though. Laz
It's loads better than C to work with tbh - we started with C-only commands in C++ and worked up to more advanced stuff, and it's so much easier to get anything done in ++.
tbh it's better practice to introduce the concept of a return value asap. it's a lot nicer when you work with a result value and return that at the end of your code with a single return statement; that way you always have a single entry and exit point, which helps with debugging when your apps get complex.
+1. I still have a module on C++, even in year 3 (named Software Development III). By now we're just sick looking at it. My degree is called Electronics, Communications & Software engineering, but none of us thought there would be quite so much software development involved. By the way what compiler are you all using? And does anyone code for any hardware applications? Our teaching focuses on programming PIC Microprocessors.
I'm using Xcode (OS X), i tried using Visual C++ and i really didn't know how to use it properly, for instance the errors in my code found in debugging weren't actually anounced(it just said there are errors). As well as the fact i didn't reall understand how there could be errors in the code that worked in Xcode when tried in Visual.
I have to say the whole compiler thing is a dark art to me - I've never really used anything but the most common program for learning with... Usually a full IDE like Visual Studio or the equivalent (i.e. Borland's Delphi). Has anyone heard of the programming language that's basically made up entirely of simple syntaxes? We learnt a bit with it at A-level and I can't remember what it is for the life of me.