CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2011
    Posts
    2

    Beginners question about methods

    Hi all! I am just taking my first steps into learning c++ for fun, but I can't seem to get some code working here...

    a simple method I am using returns the error:

    error C2601: 'Addition' : local function definitions are illegal
    1> getline.cpp(11): this line contains a '{' which has not yet been matched

    it seems to be refering to :

    int Addition(int x, int y){
    int useranswer;
    useranswer = x + y;
    return useranswer;
    }

    and as far as I can see the { is matched with } at the end, is it not?

    can anyone help me with this, please?

    the entire (unfinished) code is:

    #include <iostream>
    #include "conio.h"
    #include <string>
    using namespace std;

    int Addition(int x, int y);
    int Subtraction(int x, int y);
    int Multiplication(int x, int y);


    void main(){
    int num1;
    int num2;
    string mathtype;
    cout << "What do you want to do? add, subtract, or multiply? ";
    getline(cin, mathtype);
    cin.clear ();
    cout << "You picked " << mathtype << endl;
    cout << "Enter a number: ";
    cin >> num1;
    cout << "Enter another number: ";
    cin >> num2;
    if(mathtype == "add"){
    cout << "The numbers added equal " << Addition(num1, num2) << endl;

    }

    int Addition(int x, int y){
    int useranswer;
    useranswer = x + y;
    return useranswer;
    }



    I have tried adding the other Subtraction and Multiplication methods, but they don't work either. Where am I missing the }?

    Thanks in advance!
    Attached Images Attached Images  

  2. #2
    Join Date
    May 2011
    Posts
    2

    Re: Beginners question about methods

    found the answer: the if brace was not closed.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Beginners question about methods

    This illustrates why I find that style of indentation hard to read. If you line them up and indent properly, it's easy to see what's missing.
    Code:
    void SomeFunction(){
    ...
    }
    
    vs.
    
    void SomeFunction()
    {
    ...
    }

  4. #4
    Join Date
    Jul 2010
    Posts
    37

    Re: Beginners question about methods

    Quote Originally Posted by GCDEF View Post
    This illustrates why I find that style of indentation hard to read. If you line them up and indent properly, it's easy to see what's missing.
    I agree wholeheartedy.
    I know coding preference ultimately comes down to what you're used to, but I've never seen any benefit in keeping the braces misaligned.
    Last edited by petszk; May 16th, 2011 at 08:16 PM.

  5. #5
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Beginners question about methods

    Quote Originally Posted by petszk View Post
    ...I've never seen any benefit in keeping the braces misaligned.
    It lets you put more code on the screen.
    Was probably important when people used 24-lines text terminals.
    And no, I don't like that style.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Beginners question about methods

    Looking at the code posted here, I was not able to spot the mistake myself at a glance. Looking at the image file, I was able to guess the problem immediately, and my guess was correct.

    Considering that I prefer the Allman style (the latter of GCDEF's examples) myself, I'd say that the problem really is (lack of) indentation and having an inkling of what to look for when faced with that error message.

    Oh, and speaking of indentation: you clearly have good indentation, so post your code here in [code][/code] bbcode tags to preserve it.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured