Call a void function in a cout statement
Nov 3, 2012 at 9:59pm UTC
Hi! So in the main function, I want to call the void function Test(), but it seems like i"m unable to call it. Is there another way to do this and keep the function the same line. I know I can call it if I just end my cout statement and call it on the next line.
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
#include <iostream>
#include <string>
using namespace std;
void YesBrian()
{
cout << "You are Brian Dang" << endl;
};
void NoBrian()
{
cout << "You are NOT Brian Dang" << endl;
};
void Test(string CorrectName) {
{
if (CorrectName == "Brian Dang" )
YesBrian();
else
NoBrian();
}
}
//Test function
int main() {
string CorrectName;
{
CorrectName = "Caroline" ;
cout << "Test case: " << "" << CorrectName << endl;
cout << "RESULT:" << "" << Test(CorrectName) << endl;
cout << endl;
CorrectName = "Brian Dang" ;
cout << "Test case: " << "" << CorrectName << endl;
cout << "RESULT:" << "" << Test(CorrectName) << endl;
cout << endl;
CorrectName = "DANG Brian!" ;
cout << "Test case: " << "" << CorrectName << endl;
cout << "RESULT:" << "" << Test(CorrectName) << endl;
cout << endl;
CorrectName = "Baby Dang" ;
cout << "Test case: " << "" << CorrectName << endl;
cout << "RESULT:" << "" << Test(CorrectName) << endl;
cout << endl;
};
return 0;
};
Nov 3, 2012 at 10:01pm UTC
can't cout without a return from the function, ie no Void. Need to just call the function, not put it in the cout.
Last edited on Nov 3, 2012 at 10:03pm UTC
Nov 3, 2012 at 10:21pm UTC
@se67en
Made a few small changes to your program. Made the void functions into string functions, and passed the variables. Hope this is something you had in mind.
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
#include <iostream>
#include <string>
using namespace std;
string YesBrian(string CorrectName)
{
CorrectName= "You are Brian Dang" ;
return CorrectName;
}
string NoBrian(string CorrectName)
{
CorrectName = "You are NOT Brian Dang" ;
return CorrectName;
}
string Test(string CorrectName)
{
string Name;
if (CorrectName == "Brian Dang" )
Name = YesBrian(CorrectName);
else
Name = NoBrian(CorrectName);
return Name;
}
//Test function
int main() {
string CorrectName;
CorrectName = "Caroline" ;
cout << "Test case: " << "" << CorrectName << endl;
cout << "RESULT:" << Test(CorrectName) << endl;
cout << endl;
CorrectName = "Brian Dang" ;
cout << "Test case: " << "" << CorrectName << endl;
cout << "RESULT:" << Test(CorrectName) << endl;
cout << endl;
CorrectName = "DANG Brian!" ;
cout << "Test case: " << "" << CorrectName << endl;
cout << "RESULT:" << Test(CorrectName) << endl;
cout << endl;
CorrectName = "Baby Dang" ;
cout << "Test case: " << " " << CorrectName << endl;
cout << "RESULT:" << Test(CorrectName) << endl;
cout << endl;
return 0;
}
Nov 3, 2012 at 10:22pm UTC
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
#include <iostream>
#include <string>
using namespace std;
string Test(string CorrectName)
{
const string YesBrian = "You are Brian Dang" ;
const string NoBrian = "You are NOT Brian Dang" ;
if (CorrectName == "Brian Dang" )
return YesBrian;
else
return NoBrian;
}
//Test function
int main() {
string CorrectName;
{
CorrectName = "Caroline" ;
cout << "Test case: " << CorrectName << endl;
cout << "RESULT:" << Test(CorrectName) << endl;
cout << endl;
CorrectName = "Brian Dang" ;
cout << "Test case: " << CorrectName << endl;
cout << "RESULT:" << Test(CorrectName) << endl;
cout << endl;
CorrectName = "DANG Brian!" ;
cout << "Test case: " << CorrectName << endl;
cout << "RESULT:" << Test(CorrectName) << endl;
cout << endl;
CorrectName = "Baby Dang" ;
cout << "Test case: " << CorrectName << endl;
cout << "RESULT:" << Test(CorrectName) << endl;
cout << endl;
};
return 0;
};
This is one way to do it. Note that the function returns a string, as opposed to nothing (void), so cout knows how to handle it and output the result of the function already. Also, outputting "" to cout does absolutely nothing for the record, so I removed those.
Edit: Haha, I got beaten to it this time. But oh well, both are similar ways of doing the same thing, so feel free to look at/use either or both.
Last edited on Nov 3, 2012 at 10:32pm UTC
Nov 4, 2012 at 12:26am UTC
Thank you everybody! This is really helpful!
Topic archived. No new replies allowed.