0

I am a newbie at C++, and I am trying to make a "calculator" which: adds two numbers, subtracts two numbers, multiplies two numbers, divides two numbers, takes the sine of a number, takes the cosine of a number, or takes the tangent of a number. Here is the code:

#include <iostream>;
#include <cmath>;
#include <string>
int main () 
{}
int ask(std::string operation);
    {
        std::cout<<"Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:\n";
        std::cin>>operation;
            if (operation="Addition") 
            {
                goto Add
                                }
    float Add(float addend1, float addend2, float answer) 
    {
    Add:
        std::cout<<"Insert the first number to be added:\n";
        std::cin>>addend1;
        std::cout << "Insert the second number to be added:\n";
        std::cin>>addend2;
        answer=addend1+addend2;
        std::cout<<addend1<<"+"<<addend2<<"="<<answer<<"\n";
        break
    }
}

There will be more functions later, but my problem is on line 7. There is an error that says: expected unqualified-id before "{" token. I know my indentation is horrible, but thanks!

smilinggoomba
  • 91
  • 1
  • 2
  • 11
  • 6
    Why... why... why `goto`?! Also, the label is in another function. Very, very bad idea. What compiler are you using? – Ed S. Jun 19 '11 at 19:13
  • 2
    Be careful of `=` vs. `==` in `if` conditions. – Wooble Jun 19 '11 at 19:15
  • @Woobie: The OP doesn't want `==` either, he wants `strcmp`. – Ed S. Jun 19 '11 at 19:31
  • 1
    Wow, this is some of the most broken code I've seen. And I've seen a lot of CS 101 code. I used to be sort of an unofficial TA years ago. – Omnifarious Jun 19 '11 at 19:32
  • 1
    @Omnifarious: Everyone starts somewhere. – Ed S. Jun 19 '11 at 19:34
  • @smilinggoomba: My tip would be to start even smaller than you have made due to the many errors I see in your code. Start with a program that adds two numbers and print that. Then move on to writing the Add into a *function*. After that try to subtract a number from another. Go slow though, add one line and compile and see if the compiler understands it! You'll get to a Calculator eventually – default Jun 19 '11 at 19:52
  • @Ed S.: This is true. I sounded discouraging, and I didn't really mean to be. More of an observation and a recommendation to learn more about how to do things. – Omnifarious Jun 19 '11 at 21:18
  • Actually, this code is broken in so many ways, it should be closed with a reference to http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list. – sbi Jun 19 '11 at 21:54

5 Answers5

8

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;
}
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • My code now looks like this: `#include ; #include ; #include ; int main () { int ask (){ char operation [20]; std::cout<<"Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:\n"; std:cin>>operation; if (operation="Addition"){ float addend1, float addend2, float answer std::cout<<"Insert the first number to be added:\n"; std::cin>>addend1; std::cout << "Insert the second number to be added:\n"; std::cin>>addend2; answer=addend1+addend2; std::cout< – smilinggoomba Jun 19 '11 at 19:48
  • Continued from last comment: Expected "}" at end of input. – smilinggoomba Jun 19 '11 at 19:50
  • @smilinggoomba: yeah I'm not parsing that out. I posted an example based off of yours. – Ed S. Jun 19 '11 at 19:52
  • @Ed S. Wow! You did an amazing work. I was too lazy to analyze/explain all of this so thorough. +1 – Ivan Danilov Jun 19 '11 at 20:05
  • @Ivan: Meh, I was waiting for my coffee to brew =D – Ed S. Jun 19 '11 at 20:12
6

С++ doesn't allow nested functions. You have function main() and trying to declare function ask() inside it. And compiler doesn't know what you want.

Ivan Danilov
  • 14,287
  • 6
  • 48
  • 66
  • @EdS: I use Xcode to compile, and I edited my code. But thanks for the help! – smilinggoomba Jun 19 '11 at 19:40
  • @smilinggoomba: Just to clarify. Xcode is the name of the IDE (Integrated Developer Environment). When you compile your program, Xcode will call Apple's version of the compiler called [gcc](http://en.wikipedia.org/wiki/GNU_Compiler_Collection). – Lucas Jun 19 '11 at 21:08
2

Let's try to break this down..
You shouldn't use ; on the precompiler directives.

#include <iostream>;
#include <cmath>;
#include <string>;

Should be

#include <iostream>
#include <cmath>
#include <string>

.

int main () {
    int ask (){

See Ivans answer for this

char operation [20];
std::cout<<"Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:\n";
std:cin>>operation;
if (operation="Addition"){

You can use std::string instead which is alot easier to deal with. Then you can write

#include <string>

...
std::cout<<"Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:\n";
std::string myString;
getline(cin, myString);
if (myString == "Addition"){

.

goto Addition;
}
}
    float addition(float addend1, float addend2, float answer)
{

Not sure what is going on here.. but let's break Addition to it's own function

void Addition(){
    // do addition here
}

.

Addition:
std::cout<<"Insert the first number to be added:\n";
std::cin>>addend1;
std::cout << "Insert the second number to be added:\n";
std::cin>>addend2;
answer=addend1+addend2;
std::cout<<addend1<<"+"<<addend2<<"="<<answer<<"\n";
}

Don't forget that you have to define the variables

int addend1;
int addend2;
int answer;

Hope this helps you along the way.

Community
  • 1
  • 1
default
  • 11,485
  • 9
  • 66
  • 102
  • That should not matter, because `;` means empty statement. Its completely valid code. http://www.ideone.com/yOEHz – Nawaz Jun 19 '11 at 19:22
  • 2
    It matters in Visual Studio: `warning C4067: unexpected tokens following preprocessor directive - expected a newline` – default Jun 19 '11 at 19:23
  • @Default: Even GCC gives warning. But its valid. Its not an error. The code is well-formed even if you write `;;;;;;;;;;;;`. – Nawaz Jun 19 '11 at 19:24
  • ok, so I extended the answer somewhat. Hope that removes the downvote? – default Jun 19 '11 at 19:47
  • @Nawaz: The semicolon is illegal in C and in C++. The preprocessor language is a language in and of itself. The syntax of that language is described in chapter 16 of the 2003 C++ standard, section 6.10 of the C99 standard. Preprocessor statements are terminated by the end of line character, not by a semicolon. – David Hammen Jun 19 '11 at 20:13
2

First int ask() what is that.Why do you start a block here. Second you have two {s and three }s that's because of the ask(). I think that c++ does not support anonymus functions. Third why do you use goto,when you have a function,just call the function. Fourh your addition func should either be void or remove it's last parameter. Also I think that you don't need string.h file unless you use some rather advanced funcs,the char array should be enough for your program.

2

I commented your code a little bit, maybe that gets you started:

#include <iostream>;
#include <cmath>;
#include <string>;
int main () {
    int ask (){         //you cannot nest functions in C++

    char operation [20];    //why not use the string class if you include it anyway 
std::cout<<"Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:\n";
    std:cin>>operation;
    if (operation="Addition"){ //you cannot compare char-strings in C++ like that
    goto Addition;      //don't use goto (I don't want to say "ever", but goto is only used in extremely rare cases) make a function call instead
}
}
    float addition(float addend1, float addend2, float answer)  //you probably want to declare the variables inside the function
{
Addition:           //don't use labels
std::cout<<"Insert the first number to be added:\n";
std::cin>>addend1;
std::cout << "Insert the second number to be added:\n";
std::cin>>addend2;
answer=addend1+addend2;
std::cout<<addend1<<"+"<<addend2<<"="<<answer<<"\n";
}
Lucas
  • 13,679
  • 13
  • 62
  • 94