-3

I wrote a program to determine if a student passes for exams. So it calculates the average and compares it with a limit and return it to the main.

It works (Which is great) but i keep getting a 0 or a 1 after each output. Im guessing this has to do with the boolean function. But im honestly not sure. At this point it feels like ive dug a rabbithole and see no exit.

Any help would be appreciated.

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

bool heeftToegang(float lab1, float lab2, float lab3, float toets1,
                  float toets2)
{
    const int kleinste = 0;
    const int hoogste = 10;
    const int toetsWeging = 3;
    int grens = 6;

    int gemiddelde = (lab1 + lab2 + lab3 +
               toets1 * toetsWeging + toets2 * toetsWeging) /
              9;

    string geen = " geen deelname";
    string oke = " => deelname oke";

    cout << "Lab: " << lab1 << " ; " << lab2 << " ; " << lab3
         << " Toets: " << toets1 << " ; " << toets2;

    // if else statement om de cijfers te checken
    if (lab1 > 8)
    {
        grens -= 0.5;
    }
    else if (lab2 > 8)
    {
        grens -= 0.5;
    }
    else if (lab3 > 8)
    {
        grens -= 0.5;
    }

    // check if var invalid
    if (lab1 == kleinste || lab2 == kleinste || lab3 == kleinste ||
        lab1 > hoogste || lab2 > hoogste || lab3 > hoogste)
    {
        cout << geen << " (Sprake van fraude)";
        return false;
    }
        // hier wordt er gekeken of het cijfer kleiner is dan 4
    else if (toets1 < 4 || toets2 < 4)
    {
        cout << geen;
        return false;
    }
    // check gemiddelde < 6
    else if (gemiddelde < grens)
    {
        cout << geen;
        return false;
    }
    else
    {
        cout << oke;
        return true;
    }
}
int main()
{
    cout << heeftToegang(5, 5, 5, 6, 6) << endl;
    cout << heeftToegang(5, 5, 6, 6, 6) << endl;
    cout << heeftToegang(5, 6, 6, 6, 6) << endl;
    cout << heeftToegang(6, 6, 6, 6, 6) << endl;
    cout << heeftToegang(6, 6, 6, 5, 6) << endl;
    cout << heeftToegang(6, 6, 6, 6, 5) << endl;
    cout << heeftToegang(6, 6, 6, 7, 7) << endl;
    cout << heeftToegang(8, 8, 6, 5, 6) << endl;
    cout << heeftToegang(8, 8, 0, 7, 8) << endl;
    cout << heeftToegang(9, 9, 9, 3.9, 9) << endl;
    return 0;
}
Dedeu
  • 3
  • 1

2 Answers2

0
cout << heeftToegang(9, 9, 9, 3.9, 9) << endl;

Will execute the heeftToegang function and output what was returned. Booleans are displayed as '0' or '1' when outputted with cout (you can change this with boolalpha).

Your function does all the outputting that you want, so there's no need for you to output the boolean at the end. Change your function calls to just that--function calls!

heeftToegang(9, 9, 9, 3.9, 9);
scohe001
  • 15,110
  • 2
  • 31
  • 51
0

Use std::boolalpha if you want to see "true" or "false" printed for bools. Otherwise they wilł be converted to integers and printed as the values 0 (for false) and 1 (for true) - note: any non-zero value is considered "true" in C++.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70