You have a lot of issues in your code.
First, as Ivan points out, you are trying to define a function inside of a function (ask()
inside main()
). That isn't valid.
Second, you have a goto
(why?!) attempting to jump to a label in another function. I doubt your compiler will even allow that, but how would you expect that to work? You are attempting to use variables passed to your function addition
that don't exist as you never call the function and the stack has never been setup for it. This is bad, don't do it, just call the function properly.
Third, the #include
preprocessor directive is terminated with a newline, not a semicolon. That could cause some (relatively) hard to track down compilation errors.
Fourth, you are mistakenly attempting to assign the const char*
"Addition"
to operation
when what you meant to use was the equality operator ==
. That won't work ether though because operation
is an r-value and cannot be assigned to like that. If you want to modify it you will need to declare it as a pointer, but once again, that's not what you are going for semantically...
If you want to compare strings and (for whatever reason...) are intent on using pointers to char then you should be using strcmp. That said, you are in C++ land, so just use std:string
instead.
Try something like this. I haven't enhanced your code in anyway, just made it something that will compile and run. I have made a few changes.
Aside from getting rid of a few syntax errors, your original Add
function took the result as a float
argument. Assigning to that from within the function would only modify a copy. You would need to take a pointer or reference if you want the caller to see the modified value, but you don't need that at all as you can simply return the result.
The string comparison is case sensitive, so you would probably want to change it to be case insensitive. I'm assuming no localization here :). I'm not performing error checking on the input either, so be aware that it may fail if the user enters something other than a valid floating point number.
#include <iostream>
#include <string>
using namespace std;
void Ask();
float Add( float, float );
int main( size_t argc, char* argv[] )
{
Ask();
return 0;
}
void Ask()
{
cout << "Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:\n";
string operation;
cin >> operation;
if( operation == "Addition" )
{
float first = 0, second = 0;
cout << "enter first operand";
cin >> first;
cout << "enter second operand";
cin >> second;
cout << "The result is: " << Add( first, second );
}
}
float Add( float first, float second )
{
return first + second;
}