-2

I am trying to write a program that will let a user enter three numbers and the program will count how many positive number the user entered

#include <iostream>

int enterInteger()
{
    int a, b, c{};
    std::cout << "Enter your interger: ";
    std::cin >> a;

    std::cout << "Enter your interger: ";
    std::cin >> b;

    std::cout << "Enter your interger: ";
    std::cin >> c;

    return a, b, c;
}

void if_fun(int a, int b, int c, int counter)
{
    if (a > 0)
        std::cout << "InProgress " << counter + 1 << std::endl;

    if (b > 0)
        std::cout << "InProgress " << counter + 1 << std::endl;

    if (c > 0)
        std::cout << "InProgress " << counter + 1 << std::endl;

}

int main()
{
    int num{ enterInteger() };

    if_fun(num);
}

When I try to compile the code, I receive the following erros:

Error (active)  E0165   too few arguments in function call  Iflesson        
Error   C2660   'if_fun': function does not take 1 arguments    Iflesson    

I tried to initiate arguments in if_fun

int main()
{
    int num{ enterInteger() };

    if_fun(num1, num2, num3, 4);
}

As expected, there are erros about undefined arguments. So in function

if_fun() 

the programm add 1 integer to agrument counter and it should be the total amount of positive numbers

So, I am stuck with solution and can not recognize how I should change my code to work it as I thought

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
DCH
  • 3
  • 2
  • 2
    Your error messages refer to `Iflesson`, which does not appear in the posted code. – Scott Hunter Apr 14 '23 at 19:43
  • 3
    `return a, b, c;` - where did you learn this? What `int` do you think is being returned? – Drew Dormann Apr 14 '23 at 19:43
  • 5
    You're guessing, it's not a practical way to learn C++. – john Apr 14 '23 at 19:43
  • Maybe coming from another language. First start from the basics. For example have a counter, anytine an value is read, increment the counter if the value is positive. Finally print the counter. No return values. Later on learn how to return values, then you will afterwards learn how to use containers. eg the same way you use containers in the other languages, ie vectors, lists, dictionaries etc. – Onyambu Apr 14 '23 at 19:50
  • Fun fact: In `int a, b, c{};` only `c` is being initialized. With `int a{}, b{}, c{};` all three are initialized, but do yourself a favour and only do one thing per line. It won't run any slower and the other people reading your code, or debugging it, will thank you. – user4581301 Apr 14 '23 at 19:52
  • Your questions reveal you need a paused and slow learning of strong foundations on C++ (There are really good books that you can explore here: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list (i.e. the `return` misconception ("returning many things") and the need to pass the same number of arguments to a function as is defined, are logic, conventions and basics of the language you don't want to learn in this "trial&error" way, because that would be an infinite and frustrating path). – nostromo Apr 14 '23 at 20:07

3 Answers3

1

A function can only return one value, if you want to return three integers from a function then simplest way is to use three reference arguments

void enterInteger(int& a, int& b, int& c)
{
    std::cout << "Enter your interger: ";
    std::cin >> a;

    std::cout << "Enter your interger: ";
    std::cin >> b;

    std::cout << "Enter your interger: ";
    std::cin >> c;
}

To call this function you declare your three variables in main, and pass them as parameters.

int main()
{
    int num1, num2, num3;
    enterInteger(num1, num2, num3);
    ...
}

Now for some reason even though the if_fun function is only returning one value you made the function void. The simple thing is to make the function to return an int.

int if_fun(int a, int b, int c)
{
    int counter = 0;
    if (a > 0)
        std::cout << "InProgress " << counter + 1 << std::endl;

    if (b > 0)
        std::cout << "InProgress " << counter + 1 << std::endl;

    if (c > 0)
        std::cout << "InProgress " << counter + 1 << std::endl;

    return counter;
}

You call this function from main like this

int main()
{
    ...
    int count = if_fun(num1, num2, num3);
}

Now the if_fun function still needs work, but this is a start.

john
  • 85,011
  • 4
  • 57
  • 81
  • For me, the best answer. Clear, precise and to the point. Just i would add the C++ bibliography link: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list @DCH Take a look at this page, you will find useful references. Good luck. (And remember that if you want to tag anyone you have to use the (at-symbol)Name (just first letters and it will autocomplete)). – nostromo Apr 14 '23 at 20:37
1

A lot of (online) sources for learning C++ are out of date. Good sources to learn cpp from are : A recent C++ book or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date). For C++ reference material use : cppreference. And after you learned the C++ basics from those sources, look at the C++ coreguidelines regularely to keep up-to-date with the latest guidelines.

Here is an example of what you are trying to do in current C++ (I left out the use of https://en.cppreference.com/w/cpp/language/lambda in count_if and provided a free function predicate)

#include <algorithm>
#include <vector>
#include <iostream>

// predicate (a function returning a bool)
bool is_positive(const int value)
{
    return value > 0;
}

int main()
{
    std::vector<int> values(3); // 3 integers

    // for repeated things use loops
    // in this case a range based for loop (they cannot run out of bound
    // like regular for loops)
    // https://en.cppreference.com/w/cpp/language/range-for
    // auto : https://en.cppreference.com/w/cpp/language/auto compiler deduces type
    // & is a reference : https://isocpp.org/wiki/faq/references
    for (auto& value : values)
    {
        std::cout << "enter a number : ";
        std::cin >> value;
    }

    std::cout << "Number of positive numbers is : ";

    // from algorithm header : https://en.cppreference.com/w/cpp/algorithm/count
    std::cout << std::count_if(values.begin(), values.end(), is_positive);

    return 0;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19
0

You can't return more than one variable, but you can establish an aggregate type and return an instance of it as your one variable.

struct integers
{
    int a;
    int b;
    int c;
};
integers enterIntegers() // note the slight name change. With enterInteger I would
                         // expect only one integer to be input.
{
    integers values;
    std::cout << "Enter your integer: "; // note typo fix
    std::cin >> values.a;

    std::cout << "Enter your integer: ";
    std::cin >> values.b;

    std::cout << "Enter your integer: ";
    std::cin >> values.c;
    return values;
}

If your tools are up-to-date, you can also take advantage of Structured Bindings and sorta get what you want:

auto enterInteger()
{
    int a, b, c;
    std::cout << "Enter your interger: ";
    std::cin >> a;

    std::cout << "Enter your interger: ";
    std::cin >> b;

    std::cout << "Enter your interger: ";
    std::cin >> c;

    return std::tuple{a, b, c};
}

usage:

auto [a, b, c] = enterInteger();
user4581301
  • 33,082
  • 7
  • 33
  • 54