I’m wanting to display the amount of negative numbers typed using functions

Asked

Viewed 433 times

3

#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++;
}

1 answer

2


I’ll help you along the way you’re going, but there’s a lot of things you’d better do differently. One of the changes is that I would probably pass the argument by reference rather than making a return, but you probably haven’t learned to use it yet, so it goes the simple way.

#include <iostream>
#include <locale.h>
using namespace std;

int getnegativo(int a, int b){
    if (a < 0)  b++;
    return 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;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I’d start thinking about what you’re wearing. Why use locale in every application, if it has no function in the code? It is even worse because it is something of C and not native to C++.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.