Aaaaaah, I see. Maybe I should implement a pointer into my calculator... not that it would affect the output much, but since I'm a newbie at C++, it's good to experiment.
Aaaaaah, I see. Maybe I should implement a pointer into my calculator... not that it would affect the output much, but since I'm a newbie at C++, it's good to experiment.
There is no real need to add a pointer in this context. In particular, it is not a particularly good design choice, in my opinion. There is no reason not to simply return the value.
Going back to your original question for a moment, before you start playing around with pointers:
However, when dealing with parameters, remember that they are copies of the data you send to them, and hence, the variables themselves are local to the function. Even pointers and references are copies (understanding this is an important point).
For example:
Notice that when you change the value of x, you are changing what the temporary (pointer) variable is assigned to (changing what address it points at), so there is no effect on the passed parameter. However, assuming you leave x pointing to the same address passed to the function, it will change your variable, but you are passing an address-of-an-int (which is a value), not an int.Code:#include <iostream> void foo(int* x) { int a; x = &a; *x = 5; } void bar(int* x) { *x = 5; } int main() { int y = 1; std::cout << y <<std::endl; foo(&y); std::cout << y <<std::endl; bar(&y); std::cout << y <<std::endl; }
References work the same way in principle, but they have special syntax to make them a bit more transparent. You cannot reassign what a reference points to, and after its initializations, assigning anything to it is really making an assignment to the space in memory that it points to (as I said, special syntax). A consequence of this is, if it would ever be valid to pass a NULL argument, use a pointer... always. In general, it is good practice to prefer passing references when possible as it will make shooting yourself in the foot harder.
As a little example, if you wanted to reassign a pointer (e.g. what foo looks like it does, but doesn't), then you can pass (for example) a reference to a pointer (you could, of course pass a pointer to a pointer... though follow the same guidelines outlined above):
CheersCode:#include <iostream> void foo(int*& ptr) { ptr = new int; *ptr = 7; } int main() { int* p = 0; foo(p); if(!foo) std::cout << "Hmm... something is amiss here..." << std::endl; else std::cout << "*p = " << *p << std::endl; }
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
Ok, I'm kind of stumped again as to what the compiler's complaining about. Here's the fuction with the problem, the compiler errors are "'plus' undeclared (first use this function)" and "'minus' undeclared (first use this function)".
Code:int calc() { string cal; cout << "Do you want to add, subtract, multiply, or divide?\n"; cin >> cal; if (cal == "add") { plus(); } else if (cal == "Add") { plus(); } else if (cal == "subtract") { minus(); } else if (cal == "Subtract") { minus(); } else if (cal == "multiply") { multi(); } else if (cal == "Multiply") { multi(); } else if (cal == "divide") { divi(); } else if (cal == "Divide") { divi(); } }
Make sure plus and minus are defined before this function.
"If you tell the truth, you don't have to remember anything"
-Mark Twain
Plus() and minus() are the first two defined. Here, I'll post my whole code...
Code:#include <iostream> using namespace std; int plus() { float a; float b; float c; cout << "Input a number: "; cin >> a; cout << "Input a second number: "; cin >> b; c = a + b; cout << a << " plus " << b << " equals " << c << "."; cin.ignore(); cin.get(); } int minus() { float a; float b; float c; cout << "Input the number you want to subtract from: "; cin >> a; cout << "Input the amout you with to subtract: "; cin >> b; c = a - b; cout << a << " minus " << b << " equals " << c << "."; cin.ignore(); cin.get(); } int multi() { float a; float b; float c; cout << "Input a number: "; cin >> a; cout << "Input a second number: "; cin >> b; c = a * b; cout << a << " times " << b << " equals " << c << "."; cin.ignore(); cin.get(); } int divi() { float a; float b; float c; cout << "Input a number: "; cin >> a; cout << "Input a second number: "; cin >> b; c = a / b; cout << a << " divided by " << b << " equals " << c << "."; cin.ignore(); cin.get(); } int calc() { string cal; cout << "Do you want to add, subtract, multiply, or divide?\n"; cin >> cal; if (cal == "add" || cal == "Add") { plus(); } else if (cal == "subtract" || cal == "subtract") { minus(); } else if (cal == "multiply" || cal == "Multiply") { multi(); } else if (cal == "divide" || "Divide") { divi(); } } void again() { string d; cin >> d; if (d == "Y" || d == "y") { calc(); } else { } } int main() { string a; cout << "Do you wish to use the Calculator? Y/N: "; cin >> a; if (a == "Y" || a == "y") { calc(); cout << "Would you like to use the calculator again? Y/N: "; cin >> a; while (a == "Y" || a == "y") { again(); cout << "Would you like to use the calculator again? Y/N: "; cin >> a; } else { cin.ignore(); } }
I don't immediately see why you are having trouble with plus() and minus(), but I do notice that although you say that those first four functions return an int, you have no return statements in them, and the return values (if there were any) are never used. They should have a 'void' return type.
They needn't be defined before, just declared (although a definition is more than sufficient).Make sure plus and minus are defined before this function.
Code:void foo( ); // <--- declaration void foo( ) { } // <--- definition
Last edited by Zach L.; 08-26-2005 at 07:40 PM.
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
Well, the error seems to be in the calc() function, from what the compiler is telling me. Something about the lines "plus();" and "minus();" are bugging it, but what's odd is that multi() and divi() are fine. I copied and pasted all four from the original plus(), modifying it to fit what they're supposed to do, but you'd think that if I copied and pasted all of them, they'd all have the problem...
Usually compilers just give warnings on no returns maybe you have an option set that makes all warnings errors... try this code. I have also added 80,'\n' to the cin.ignore()'s. That will allow you to enter up to 80 chars or untill you press enter.Code:#include <iostream> using namespace std; void plus() { float a; float b; float c; cout << "Input a number: "; cin >> a; cout << "Input a second number: "; cin >> b; c = a + b; cout << a << " plus " << b << " equals " << c << "."; cin.ignore(80,'\n'); cin.get(); } void minus() { float a; float b; float c; cout << "Input the number you want to subtract from: "; cin >> a; cout << "Input the amout you with to subtract: "; cin >> b; c = a - b; cout << a << " minus " << b << " equals " << c << "."; cin.ignore(80,'\n'); cin.get(); } void multi() { float a; float b; float c; cout << "Input a number: "; cin >> a; cout << "Input a second number: "; cin >> b; c = a * b; cout << a << " times " << b << " equals " << c << "."; cin.ignore(80,'\n'); cin.get(); } void divi() { float a; float b; float c; cout << "Input a number: "; cin >> a; cout << "Input a second number: "; cin >> b; c = a / b; cout << a << " divided by " << b << " equals " << c << "."; cin.ignore(80,'\n'); cin.get(); } void calc() { string cal; cout << "Do you want to add, subtract, multiply, or divide?\n"; cin.ignore(80,'\n'); cin >> cal; if (cal == "add" || cal == "Add") { plus(); } else if (cal == "subtract" || cal == "subtract") { minus(); } else if (cal == "multiply" || cal == "Multiply") { multi(); } else if (cal == "divide" || "Divide") { divi(); } } void again() { string d; cin >> d; if (d == "Y" || d == "y") { calc(); } else { } } int main() { string a; cout << "Do you wish to use the Calculator? Y/N: "; cin >> a; if (a == "Y" || a == "y") { calc(); cout << "Would you like to use the calculator again? Y/N: "; cin >> a; while (a == "Y" || a == "y") { again(); cout << "Would you like to use the calculator again? Y/N: "; cin >> a; } else { cin.ignore(80,'\n'); } return 0; }
No, that still gives the same errors... and warnings are still warnings by my settinngs.
Broke out the compiler... this compiles for me. You had an if{ with no '}' and a couple other errors.
Code:#include <iostream> #include <string> using namespace std; void plus() { float a; float b; float c; cout << "Input a number: "; cin >> a; cout << "Input a second number: "; cin >> b; c = a + b; cout << a << " plus " << b << " equals " << c << "."; cin.ignore(80,'\n'); cin.get(); } void minus() { float a; float b; float c; cout << "Input the number you want to subtract from: "; cin >> a; cout << "Input the amout you with to subtract: "; cin >> b; c = a - b; cout << a << " minus " << b << " equals " << c << "."; cin.ignore(80,'\n'); cin.get(); } void multi() { float a; float b; float c; cout << "Input a number: "; cin >> a; cout << "Input a second number: "; cin >> b; c = a * b; cout << a << " times " << b << " equals " << c << "."; cin.ignore(80,'\n'); cin.get(); } void divi() { float a; float b; float c; cout << "Input a number: "; cin >> a; cout << "Input a second number: "; cin >> b; c = a / b; cout << a << " divided by " << b << " equals " << c << "."; cin.ignore(80,'\n'); cin.get(); } void calc() { string cal; cout << "Do you want to add, subtract, multiply, or divide?\n"; cin.ignore(80,'\n'); cin >> cal; if (cal == "add" || cal == "Add") { plus(); } else if (cal == "subtract" || cal == "subtract") { minus(); } else if (cal == "multiply" || cal == "Multiply") { multi(); } else if (cal == "divide" || "Divide") { divi(); } } void again() { string d; cin >> d; if (d == "Y" || d == "y") { calc(); } else { } } int main() { string a; cout << "Do you wish to use the Calculator? Y/N: "; cin >> a; if (a == "Y" || a == "y") { calc(); cout << "Would you like to use the calculator again? Y/N: "; cin >> a; while (a == "Y" || a == "y") { again(); cout << "Would you like to use the calculator again? Y/N: "; cin >> a; } } else { cin.ignore(80,'\n'); } return 0; }
What's the <string> header file do?
Lets you use string.... which is what you are doing... it does alot more than that too lets you compare etc add.
I thought you were able to use the string type without that header file? And I still have the same exact errors.
I got no errors.... and I needed to include it to get rid of the errors about the comparisons and such... no idea why it won't work for you I am using Microsoft's compiler 7.1 maybe that has something to do with it post your errors?
I'm using Bloodshed Dev-C++ as an IDE / compiler-linker. The errors, exactly as the compile shows them, are...
In function `void calc()':
`plus' undeclared (first use this function)
`minus' undeclared (first use this function)
I'm stumped.