1 Attachment(s)
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!
Re: Beginners question about methods
found the answer: the if brace was not closed.
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()
{
...
}
Re: Beginners question about methods
Quote:
Originally Posted by
GCDEF
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.
Re: Beginners question about methods
Quote:
Originally Posted by
petszk
...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.
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.