HELP! Can't correctly call my function

Hi! So I tested the function BrianTest below without the test function, and the output works. However, when I try to call the function below with the test function, the output does not provide the correct parameters of my BrianTest function. What am I doing wrong? The first test case does not return the if cout statement, but returns the else cout statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>

using namespace std;

//Callback function BrianTest created
void BrianTest()

{
    //Declared string

    string Brian;

    //"If" statement to print "You are Brian Dang!"

    {
        if(Brian == "BrianDang")
        cout<< "You are Brian Dang!" << endl;



    //"Else" statement to print "You are NOT Brian Dang"
        else
        cout << "You are NOT Brian Dang" <<endl;
    };

}

//Main Function to test callback function

int main()

{
    string Brian;

//Test cases

    {

        cout << "My name is Brian Dang" << endl;
        BrianTest();
        Brian="BrianDang";
        cout << endl;

        cout << "My name is BrianZang" << endl;
        Brian = "BrianZang";
        BrianTest();
        cout << endl;

        cout << "My name is Caroline Doan" << endl;
        Brian = "Caroline Doan";
        BrianTest();
        cout << endl;

        cout << "My name is Brian Rawr" << endl;
        Brian = "Brian Rawr";
        BrianTest();
        cout << endl;

        return 0;

    }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>

using namespace std;

//Callback function BrianTest created
void BrianTest(string Name)

{

    //"If" statement to print "You are Brian Dang!"

    {
        if (Name == "Brian Dang")
        cout<< "You are Brian Dang!" << endl;



    //"Else" statement to print "You are NOT Brian Dang"
        else
        cout << "You are NOT Brian Dang" <<endl;
    };

}

//Main Function to test callback function

int main()

{
    string Name;

//Test cases

    {

        cout << "My name is Brian Dang" << endl;
        Name = "Brian Dang";
        BrianTest(Name);
        cout << endl;

        cout << "My name is Brian Zang" << endl;
        Name = "Brian Zang";
        BrianTest(Name);
        cout << endl;

        cout << "My name is Caroline Doan" << endl;
        Name = "Caroline Doan";
        BrianTest(Name);
        cout << endl;

        cout << "My name is Brian Rawr" << endl;
        Name = "Brian Rawr";
        BrianTest(Name);
        cout << endl;

        return 0;

    }
}


I rewrote this in a more correct way, passing an argument to your function. This is the correct way to do it. Don't get confused by "Name" being in multiple places -- There are two entirely different variables called Name. One is in the main() function and just temporarily storing the name before it's passed to your function. The other is in the arguments for the test function. It would also be perfectly valid to call the function like this: BrianTest("Brian Rawr"); or any other name you could think of.
Last edited on
Wow! Thanks! That worked!
Moeljbcp beat me ...I wrote it below and it does work too..., guess a bit late now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <string>
using namespace std;

void BrianTest();

int main()

{
    string Brian;

//Test cases


        cout << "My name is Brian Dang" << endl;
        BrianTest();
        Brian = "BrianDang";
        cout << endl;

        cout << "My name is BrianZang" << endl;
        Brian = "BrianZang";
        BrianTest();
        cout << endl;

        cout << "My name is Caroline Doan" << endl;
        Brian = "Caroline Doan";
        BrianTest();
        cout << endl;

        cout << "My name is Brian Rawr" << endl;
        Brian = "Brian Rawr";
        BrianTest();
        cout << endl;

        return 0;

}

void BrianTest()

{
    //Declared string

    string Brian;

    //"If" statement to print "You are Brian Dang!"

    
        if(Brian == "BrianDang")
        {cout<< "You are Brian Dang!" << endl;}



    //"Else" statement to print "You are NOT Brian Dang"
        else
        {cout << "You are NOT Brian Dang" <<endl;}
}
Topic archived. No new replies allowed.