I want to display the number of negative numbers entered using functions

2
#include <iostream>
#include <locale.h>

using namespace std;

int getnegativo(int a, int b);

int main()
{
    setlocale(LC_ALL, "portuguese");
    int cont, cot=0;

    do {
        cout << "digite um valor: ";
        cin >> cont;

      cot = getnegativo(cont, cot);

    }while (cont != -1);

    cout << "número de valores negativos digitados: " << cot;

    cout << "\n\n" << endl;
    return 0;
}


int getnegativo(int a, int b){
if (a<0)
    b++;
}
    
asked by anonymous 01.11.2015 / 19:00

1 answer

1

I'll help you on the way you're going, but there are several things you'd better do differently. One of the changes is that I would probably pass the argument by reference instead of making a return , but probably not yet learned to use it, so it goes in the simplest way.

#include <iostream>
#include <locale.h>

using namespace std;

int getnegativo(int a, int b);

int main() {
    setlocale(LC_ALL, "portuguese");
    int cont, cot = 0;

    do {
        cout << "digite um valor: ";
        cin >> cont;

      cot = getnegativo(cont, cot);

    } while (cont != -1);

    cout << "número de valores negativos digitados: " << cot;

    cout << endl << endl;
    return 0;
}

int getnegativo(int a, int b){
    if (a < 0)
        b++;
    return b;
}

See running on ideone .

I would start thinking about what you are using. Why use locale in every application, if it has no function in the code? It's still worse because it's something of C and not native to C ++.

    
01.11.2015 / 19:15