0
#include < iostream >

#include "main.h"


int main() {

    std::cout << "x + y is: " << getInteger() << std::endl;
    return 0;
}


int getInteger(int x, int y){

    std::cout << "Enter an integer: ";
    std::cin >> x;
    std::cin >> y;
    return x + y;

}

The "main.h" file, included in the same source file as the function consists of the following code:

#ifndef MAIN_H_INCLUDED

#define MAIN_H_INCLUDED


int getInteger(int x, int y);


#endif // MAIN_H_INCLUDED

I don't know why the title of the file is followed by "included" in caps, this was just the default format that appeared once I created a header file through the IDE.

When I try to build this function I get the following error:

error: too few arguments to function 'int getInteger(int, int)'
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
  • 7
    The forward declaration works fine, but a function called `getInteger` that takes 2 parameters is a completely different function from one called `getInteger` that takes no parameters. Also, those funny `INCLUDED` lines are called [header guards](https://www.learncpp.com/cpp-tutorial/header-guards/). – BoBTFish Dec 17 '20 at 15:50
  • I've tried putting 2 parameters or zero, the error remains. Now that i've added two parameters, it has changed though, error:expected primary expression before 'int'. – bobthebuilder Dec 17 '20 at 15:51
  • 2
    "*I don't know why the title of the file is followed by "included" in caps*" - it is called a [header guard](https://stackoverflow.com/questions/2979384/). – Remy Lebeau Dec 17 '20 at 15:51
  • 3
    To me, it looks like an error for `getInteger` to have any arguments. The function doesn't require any external input, it gets its values from `std::cin`. – François Andrieux Dec 17 '20 at 15:52
  • The error message *"error: too few arguments to function 'int getInteger(int, int)'"* shows the compiler did find your forward declaration, otherwise it wouldn't know how many arguments `getInteger` should have. You are simply using `getInteger` incorrectly in `main`. – François Andrieux Dec 17 '20 at 15:53
  • Yes, It worked when I removed the parameters from getInteger. Thanks! – bobthebuilder Dec 17 '20 at 15:55

3 Answers3

2

It's pretty clear that you meant to write this function

int getInteger() {
    int x, y;
    std::cout << "Enter an integer: ";
    std::cin >> x;
    std::cin >> y;
    return x + y;
}

That's a function with zero arguments, which is what you give when you call the function from main.

The function you actually wrote has two arguments, and that is what the error message is telling you, when you called the function you didn't give enough arguments.

john
  • 85,011
  • 4
  • 57
  • 81
1

You call the function without arguments

getInteger()

However a function with the same name is declared with two parameters

int getInteger(int x, int y);

that moreover the values of which are not used within the function

int getInteger(int x, int y){

    std::cout << "Enter an integer: ";
    std::cin >> x;
    std::cin >> y;
    return x + y;

}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • One minute, doesn't the cin count as using those parameters? How are they not used? – bobthebuilder Dec 17 '20 at 15:57
  • the function uses the *variables*, but not the *values* that they would have to be set to initially just to make the function call, ie in `getInteger(1, 2)` the `1` and `2` would be ignored and overwritten by user input. If the function is going to read the user input, the parameters are useless and should be removed. Otherwise, the user input shoud be read in `main()` instead and then passed into the function as parameters. – Remy Lebeau Dec 17 '20 at 15:57
0

Quite a few problems here, as has been pointed out, you're asking for parameters in your getInteger() function then providing none in main.

#include <iostream>

int getInteger(){

    int x, y;
    std::cout << "Enter an integer: ";
    std::cin >> x;
    std::cout << "Enter another integer: ";
    std::cin >> y;
    return x + y;

}

    int main() {

    std::cout << "x + y is: " << getInteger() << std::endl;
    return 0;
}

This will compile, no header file required, I've modified your function so it makes works as I believe you intended.

  • Yes, I am only using this to test out header files and see how they work. – bobthebuilder Dec 17 '20 at 16:00
  • Cool I like this guys tutorials: https://www.youtube.com/watch?v=9RJTQmK0YPI this one is about header files, also this guy https://www.youtube.com/watch?v=Zbw58vTotok&t=618s this is about circular dependency which something that will probably mess you up soon enough :-) – Samuel Campbell Dec 17 '20 at 16:08
  • I'm using this https://www.learncpp.com/, started 2 days ago and finding C++ pretty fun. The language sure seems useful and rewarding, i'll be sure to check that guy out later on! – bobthebuilder Dec 17 '20 at 16:17
  • Cool, C++ is great, but pretty complex. I've been learning it for a while now and its horrible when things aren't going your way, but then soo good when you finally crack it. Keep it up! – Samuel Campbell Dec 17 '20 at 16:22