-3

I am trying to have the user enter two numbers and then repeat their sum. Right now VS 2015 is not recognizing the parameters when I try to call the function in main()

I am trying to understand the breakdown in my conceptualization of basic functions and parameters. I'm getting there but I need to see where my thought process is breaking down.

#include <iostream>

using namespace std;


int addNumbers(int FirstNumber, int SecondNumber) {
    cout << "enter first number: " << endl;
    cin >> FirstNumber;
    cout << "enter second number: " << endl;
    cin >> SecondNumber;
    int answer = FirstNumber + SecondNumber; 
    cout << answer;
    return answer; 

}

int main() {

    cout << "Test\n";
    cout << addNumbers(FirstNumber, SecondNumber);
    return 0;
}
  • Please learn how to write [mcve] – Inline Jan 14 '17 at 20:19
  • Welcome on stackoverflow. Unfortunately, it's not really clear what you're asking and what kind of problem you're having. Please read our help section to see how to write a good question to get most out of SO. – Walter Jan 14 '17 at 20:20
  • 1
    `addNumbers()` takes two arguments but you're providing only one in the all in `main()`. So that should not even compile. Moreover, `First` is not defined in `main()`. – Walter Jan 14 '17 at 20:20
  • Thanks for the prompt response; I'm tryingto find a place to edit it, is that not possible? – Blake McCorkle Jan 14 '17 at 20:21
  • Click [edit]. Also note that `First` is not defined anywhere. – Paul R Jan 14 '17 at 20:21
  • 1
    `addNumbers(FirstNumber, SecondNumber)` - neither of these two variables are defined in `main`. What do you expect to pass here? – UnholySheep Jan 14 '17 at 20:23
  • I thought I could define it in where I declared the function - int addNumbers(int FirstNumber, int SecondNumber) {}; – Blake McCorkle Jan 14 '17 at 20:23

1 Answers1

1

You need to define variables that you're using. Moreover, you only need to define them where you need them. For example

#include <iostream>
using namespace std;                      // this is not recommended but I won't 
                                          // change your code completely
int addNumbers()
{
    int FirstNumber, SecondNumber;        // local variables
    cout << "enter first number: " << endl;
    cin >> FirstNumber;
    cout << "enter second number: " << endl;
    cin >> SecondNumber;
    return FirstNumber + SecondNumber;    // compute and return result
}

int main() {    
    cout << "Test\n";
    cout << addNumbers();
    // note: return 0; is not needed.
}
Walter
  • 44,150
  • 20
  • 113
  • 196
  • @LightnessRacesinOrbit you race faster than I can edit. ... It is now (Gimme at least a few seconds to remove those pesty typos and bugs). – Walter Jan 14 '17 at 20:25
  • It ran for for me. Thanks Walter, I will compare your code to mine and understand what I was doing wrong. – Blake McCorkle Jan 14 '17 at 20:27
  • @Walter: You could have removed them before submitting :) – Lightness Races in Orbit Jan 14 '17 at 20:27
  • @LightnessRacesinOrbit Obviously, I overlook them in the first instance, but I saw them before I saw your comment. – Walter Jan 14 '17 at 20:28
  • It appears I do not understand when to place parameters in a function like - function (parameters here) vs. in the body of the function. Anyone have a good explanation or recommended reading? – Blake McCorkle Jan 14 '17 at 20:29
  • 1
    @BlakeMcCorkle: You should be learning C++ from a good book. Here are some: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Lightness Races in Orbit Jan 14 '17 at 20:30
  • You should not remove `return 0;` and you should modify `\n` to `<< endl;`, also recommend using `using std::endl; using std::cout; using std::cin;` instead of `using namespace std;` – cpatricio Jan 14 '17 at 20:31
  • 1
    @cpatricio "You should modify \n to << endl;" Why? There is no need to flush at that point. – user4581301 Jan 14 '17 at 20:40
  • @cpatricio `return 0` is implicit from `int main()` (according to the C++ standard) and hence not required (unless you want to return early, but that was not the case here). – Walter Jan 14 '17 at 20:45
  • @user4581301 unless there is a performence issue, `<< endl` will be easier to read the code. @Walter that's compiler depedent, if you can avoid problems, why not? – cpatricio Jan 14 '17 at 20:48
  • Both are issues of taste, I guess, but if `return 0` was ever a requirement in C++ it was decades ago. C hasn't even required it for almost 20 years. – user4581301 Jan 14 '17 at 20:53
  • @cpatricio *that's compiler depedent* Really? I never knew about such a badly non-compliant compiler. Which one are you thinking about? – Walter Jan 16 '17 at 08:51