Passing/sharing strings between functions?

With my code below I'd like to not have to use "string name" as a global variable if I don't have to. I'd like to declare it in the getName() function but be able to call it in the guessNumber() function on where I have the welcome message. With my code, how would I go about that?

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
//Number guessing game by Calvin

#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <string>
#include "clrscr.h"

std::string name;

int getName()
{
	do {
			std::cout << "Please enter your Username: ";
			std::getline(std::cin, name);

			if (name.length() < 2)
			{
				ClearScreen();
				std::cout << "Name must have atleast 2 characters!" << std::endl;
			}
		} while (name.length() < 2);

	return 0;
}

int guessNumber()
{
	ClearScreen();
	std::string test;

	std::cout << "Welcome " << name << ". Let's get started." << std::endl;
	std::cin >> test;

	return 0;
}

int playAgain()
{

	return 0;
}

int main()
{
	getName();
	guessNumber();
	playAgain();

	return 0;
}
Last edited on
I don't see how you could use it in getName() and use it in guessNumber().
But what you could do is declare it in your main and pass it between your functions with references.
Could you show me how to do that with my current code?
Function parameters and return values:
1
2
3
4
5
6
7
8
9
std::string getName();
int guessNumber( const std::string & name );

int main() {
  const std::string foo( getName() );
  int num = guessNumber( foo );
  std::cout << num << '\n';
  return 0;
}
I'm not understanding that. So I make my whole function getName a string instead?
How's 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//Number guessing game by Calvin

#include <iostream>
#include <string>
#include <cstdio>

using std::cout;
using std::endl;
using std::cin;

int getName(std::string x)
{
	do {
			std::cout << "Please enter your Username: ";
			std::getline(std::cin, x);

			if (x.length() < 2)
			{
				cin.clear();
				std::cout << "Name must have atleast 2 characters!" << std::endl;
			}
		} while (x.length() < 2);

	return 0;
}

int guessNumber(std::string x)
{
	std::cout << "Welcome " << x << ". Let's get started." << std::endl;
	
	cin.clear();

	return 0;
}

int playAgain()
{

	return 0;
}

int main()
{
    std::string name;
    
	getName(name);
	guessNumber(name);
	playAgain();

	return 0;
}
@deathslice That code seems to compile fine and makes the example more clear, however when it's supposed to print out the Welcome << name <<..... it leaves name blank so the output is:

"Welcome , Let's get started" even when entering a name.

EDIT: Yes I replaced your "x" variable name with my original variable name "name" before testing.
Last edited on
The problem with deathslice's code is that at line 11, x is passed by value. getName() updates x, but x is never returned to the caller. You have two choices.
1) pass x by reference instead of by value.
2) Declare getName as returning a string, then return x.

For this type of thing would it be better to continue to just use std::string name as a global variable?

I am trying to use good practice with my code even with the smallest of projects and figured using as few global variables as possible would be best, though I could be wrong here. Am I making more work than I should, or am I right in that I shouldn't use "name" as a global variable?
am I right in that I shouldn't use "name" as a global variable?

Yes.

I'm not understanding that. So I make my whole function getName a string instead?

No. The function is not a string, it returns a string.

You have written functions like:
1
2
3
int foo() {
 return 0;
}

What do you think the int to mean on that first line? What about the return keyword?

In my previous post there was only the declaration of the getName. Here is (incomplete) implementation for it:
1
2
3
4
5
6
std::string getName()
{
  std::string result;
  // do something to change the result
  return result;
}
Alright that makes sense. I am now returning "name" but that's as far as I've got.
Last edited on
Here is my updated code. The error I am getting is in the main function. Says guessNumber(); does not take 0 arguments.

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
//Number guessing game by Calvin Gunn

#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <string>
#include "clrscr.h"

std::string getName()
{
	std::string name;
	do {
			std::cout << "Please enter your Username: ";
			std::getline(std::cin, name);

			if (name.length() < 2)
			{
				ClearScreen();
				std::cout << "Name must have atleast 2 characters!" << std::endl;
			}
		} while (name.length() < 2);

	return name;
}

int guessNumber(std::string name)
{
	ClearScreen();
	std::string test;

	std::cout << "Welcome " << name << ". Let's get started." << std::endl;
	std::cin >> test;

	return 0;
}

int playAgain()
{

	return 0;
}

int main()
{
	int guessNumber(const std::string & name);

		getName();
		guessNumber();
		playAgain();

		return 0;
}
How was my main() different?
Ah yes, there we go. All works great now. THANKS!

Now I need to figure out what exactly is going on here so I can actually learn something. My original code made sense when name was a global var, now it's a tad confusing.
Topic archived. No new replies allowed.