Difference between cout and return!!

Pages: 12
My instructor posted this problem that's due for an assignment. It seems so simple, yet he purposely makes things annoying

Anyways, here's the problem:
[IMG]http://i25.tinypic.com/30v0gb8.jpg[/IMG]

I've tried looking online to help me, but I can't seem to understand it.

Does he want me to assign variables to firstname and lastname to display? If I were to do that, I can't figure out how I would do so since his examples are only with integers.


This assignment is to test your understanding of functions.

Your program will, of course, have a cout statement -- just not in your firstPresident() function.

If you are still lost, take a read through the site tutorial on functions
http://www.cplusplus.com/doc/tutorial/functions/

Really BIG hint: your function's return type should be one of the following:

  1. const char*

  2. std::string

Hope this helps.
Ok this is making a little more sense.

If the return type is going to be a string, why won't it let me use strings to make a function?

cout is an object of class ostream that represents the standard output stream and is used to display stuff to the console window.

return is used to return a value from a method/function.

1
2
3
4
std::string someFunction()
{
  return "Some String";
}
Well done, bob. You get an A plus! :)
Blah, this isn't making any sense to me. :/ I'm sorry guys, I'm really trying hard to understand why or how this works, but the setup of these problems makes no sense.
I understand the concept that the Function is basically a template that will transform the data that is entered into main(). For example, if we define string::std firstPresident(first, last) as a function, I can enter a name in the main(), like string firstPresident (George, Washington) or if I wanted to change it up, (Jimmy, Carter)

I cant' figure out how to format this though.
Last edited on
As bob very kindly demonstrated, you can have functions that take no arguments. That's what you are asked to do here.
I seem to be missing a huge concept here.. Am I makings things way complicated?

I keep getting errors that George and Washington aren't defined.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <ctime>

using namespace std;

std::string firstPresident(string first, string last)
{
  return "first, last";
}

int main()
{
	string first, last;
	firstPresident( "George", "Washington" );
	cout << first << " " << last << endl;
	return 0;

}


}


>____________________> gives me blank input.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <ctime>

using namespace std;






std::string firstPresident(string first, string last)
{
  return "George Washington";
}

int main()
{
	string first, last;
	firstPresident( "David", "Bowie" );
	cout << first << " " << last << endl;
	
}


Why is this giving me nothing?! ugh. so frustrated.
1)
you can have functions that take no arguments


2) You don't seem to understand how functions work. See: http://www.cplusplus.com/doc/tutorial/functions/
string first, last in main() is not the same thing as string first, last in firstPresident(). They're staying local to the function they've been defined in.
What do you mean by this?
you can have functions that take no arguments


Does that mean my function can just cout the George Washington? My project says not to.

First question, why do I have to use std::string and why can't I just use string?

Second of all, what does the return; initially do?

I feel so stupid.
Ok, now a normal person would do this, assuming they could cout, by doing this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

void firstPresident ()
{
  cout << "George Washington";
}

int main ()
{
  firstPresident ();
  cout << " " << endl;
  return 0;
}

This I understand. What I don't understand I how I can do this without cout.
Can somebody please tell me how to do this before I seriously go insane?
Have you read the function tutorial completely? It tells you everything you need to know.
1
2
3
4
5
6
7
8
9
10
11
std::string HelloWorld( )
{
	return "Hello world!";
}

int main( )
{
	cout << HelloWorld( );

	return 0;
}


The HelloWorld( ) function returns "Hello world!". So, when you call HelloWorld( ), it's essentially the same thing as just writing out "Hello world!" instead.

Return values can be any type. They can be int, void, bool, std::string, whatever you want. In this case, HelloWorld( ) returns std::string, which is a type "cout" understands.

Anyways, when you run the program, this is what happens:

1
2
3
4
5
6
7
int main( )
{
	// cout << HelloWorld( ); <-- The compiler resolves the function return, and you get this:
	cout << "Hello world!";

	return 0;
}


Hope this helps..
First off, I'm wondering why you're including the ctime header file.

secondly, you need to construct a function that accepts two strings, combines them with a special string function, and then returns the result

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

using namespace std;

std::string stringFunction(string first, string last)
{
   //initialize any needed variables here

   //construct your function to combine (concatenate) the strings here

   //return the result here

}

int main()
{
	string first, last, result;
	result = stringFunction( first, last );
	cout << result;
}


Hope that helps.
There is no part of the assignment requiring him to do string manipulations. Where do you assume that he must use arguments or combine string values?

His function type should look like either:

  1. const char* firstPresident()

  2. string firstPresident()

Pages: 12