A Function for Input/Output

I am trying to make an Input function and an output function for this:
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
#include <iostream>
#include <string>
#include <vector>

using namespace std;
int main()
{
	vector<string> games;
	string faves;
	string game;
	cout << "How Many Favorite Games Do You Have?\n";
	cin >> faves;
	int i = atoi(faves.c_str());
	
	while(i>0)
	{

		cout << "\nPlease Enter game names " << i << " :";
		cin >> game;
		games.push_back(game);

		i--;
	}

	cout << "Your Favorite Games Are: " << endl;

	int g = 0;
	while(games.size() > g)
	{
		cout <<g+1<<" "<< games[g]<< endl;
		g++;
}
	cout << endl;
}


I looked at the tutorials and I'm still having trouble figuring it out.Appreciate any help.

thanks
What are you having trouble understanding?
i just dont know how to use the functions, the tutorials are all for numbers...I just need input/output for my code there using functions
I guess what I am saying is that I just dont understand how use use functions at all...
Let's take one small piece of your program, lines 15-23. Here is how a function for that might look:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
vector<string> getGames(int numGames)
{
	vector<string> games;
	string game;

	while(numGames>0)
	{

		cout << "\nPlease Enter game names " << numGames << " :";
		cin >> game;
		games.push_back(game);

		numGames--;
	}

	return games;
}


In main(), you would just need to do something like this at line 15:

games = getGames(i);

Does that make sense?
kind of yes. to me it looks almost the same as the vectors one i did before but different words.
That's really the point. It is the same. It is just in a function instead of inline.

The point is now it is easier to reason about this section of the code. It has 1 parameter and two variables. It does one thing. You can test it and verify that it works. And then you can forget about it. You just know that if you need the user to enter in a certain number of games, you can call this function.

You have three pieces that could be moved into functions. If you did that, your main() might look something like this:

1
2
3
4
5
6
7
int main()
{
    int numGames = getNumGames();
    vector<string> games = getGames(numGames);
    printGames(games);
    return 0;
}


It is immediately clear what the program is doing. You can work on each function in isolation. And because main() is small, you are unlikely to forget that main() returns a value.
It is possible to ask the user to input game names without specifically asking for how many. (That way the user doesn't have to think them all through in his head first.)

The link is to read x, y pairs into a point structure, but the principle is the same: read until you get a blank (or empty) line. See line 15.
http://www.cplusplus.com/forum/beginner/2409/#msg9254

Err.. well, here you go anyway:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
vector <string> getGames()
  {
  vector <string> games;
  cout << "Please enter the names of your favorite games, one per line.\n"
          "Press ENTER twice to finish.\n> "
       << flush;
  string game;
  while (getline( game ) && !game.empty())
    {
    games.push_back( game );
    cout << "> " << flush;
    }
  return games;
  }

Hope this helps.
Topic archived. No new replies allowed.