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.
#include <iostream>
#include <string>
usingnamespace 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;
}
}
#include <iostream>
#include <string>
usingnamespace 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.
#include <iostream>
#include <string>
usingnamespace 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;}
}