Calling a function

I am calling functions and having a bit of an issue. I believe the code is correct but it will not allow me to enter a number to tell it how many time to print out a name.

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
#include <iostream>

using namespace std;

void printName();


int main()
{
char name;
int num;

	cout << "Please enter your name. ";
	cin >> name;

	cout << endl;
	
	cout << "How many times would you like to print your name? \n \n";
	cin >> num;

	for(int i = 0; i < num; i++)
	{
		printName();
		break;
	}
	return 0;
}

void printName()
{
	cout << "name" << endl;
}
closed account (NUj6URfi)
Try:

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
#include <iostream>

using namespace std;

void printName();


int main() {
string name;
int num;

	cout << "Please enter your name. ";
	getline(cin, name);

	cout << endl;
	
	cout << "How many times would you like to print your name? \n \n";
	cin >> num;

	for(int i = 0; i <= num; i++) {
		printName();
	}
               cin.get();
	return 0;
}

void printName() {
	cout << name << endl;
               return;
}
Last edited on
The use of breaks and returns is wrong for both of you. Also printName()
does not know what "name" is. You must pass it in.
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
#include <iostream>
#include <string>
using namespace std;

void printName(string);

int main()
{
    string name;
    int num;
    
    cout << "Please enter your name. ";
    getline(cin, name);

    cout << endl;

    cout << "How many times would you like to print your name? \n";
    cin >> num;

    for(int i = 0; i < num; i++) {
        printName(name);
    }
    return 0;
}

void printName(string name) {
    cout << name << endl;
}
Last edited on
Why over complicate things? The break and return were wrong, but what's the point of passing an argument to the function instead of just making name global? also why,
getline(cin, name);
when
cin >> name;
works fine?


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
#include <iostream>
#include <string>

using namespace std;

void printName();
string name;    // needs to be global for the function to see it,
                        // also needs to be string not char

int main()
{
        int num;

	cout << "Please enter your name. ";
	cin >> name; 

        // cout << endl;    - unless you want an extra blank line for some reason
        // add \n to the beginning next line if you must   "\nHow many..." 
	
	cout << "How many times would you like to print your name?";   // "\n\n"
	cin >> num;

	for(int i = 0; i < num; i++)
	{
		printName();
	}
        
        cout << endl;  // makes more sense here

	return 0;
}

void printName()
{
	cout << name << endl;  // not "name"
}


The formatting (endl) or lack of doesn't matter except in how the output looks so those comments don't really matter so much. the only real problems were giving name a type char and outputting "name" instead or the value of name.
The problem with global functions is manyfold. They corrupt your namespace, can get lost in files, you don't know what functions are calling it, you don't know what functions have been modifying it, and if you want to pass a different variable to the function, then you have to write a new one. Basically, unless you absolutely HAVE to use a global variable (normally due to badly coded external API's), you should try to pass by reference instead, as @vasilenko93 has done.
closed account (NUj6URfi)
The only problem with my code is that name is supposed to be global and I need to add in \n. getline(cin, name) gets multiple words while cin gets one.
It is not a good idea to promote the use of global variables on a beginners forum. Though there are always exceptions to any rule, the idea is to start out with good habits, rather than later having to unlearn bad ones.
closed account (NUj6URfi)
What is wrong with global variables? They are easier than private ones.
It is not a good idea to promote the use of global variables on a beginners forum.

I thought about that later for just the reasons you gave. With something as simple as this it was a quick easy fix though.

getline(cin, name) gets multiple words while cin gets one.

I've never had occasion to input more than one word before and I didn't realize that limitation until now. Always good to learn something new. Having return at the end of the function is pointless though since once name is output, the function ends.
closed account (NUj6URfi)
The function not the program. Than the program doesn't know what to do and does nothing instead of going back to the previous function.
closed account (NUj6URfi)
I wonder why the topic creator hasn't posted something else yet.
ballsdeepcoder69 is the best coder alive, swag.
ballsdeepcoder69 is the best coder alive, swag.

Your swag has no power in a C++ forum...or anywhere else in the world.
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
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>

using namespace std;

void printName(string name);


int main()
{
string name;
int num;

	cout << "Please enter your name. ";
	getline(cin, name);

	cout << endl;

	cout << "How many times would you like to print your name? \n \n";
	cin >> num;

	for(int i = 0; i < num; i++)
	{
		printName(name);
	}
	return 0;
}

void printName(string name)
{
	cout << name << endl;
}


done
Last edited on
closed account (NUj6URfi)
Try:

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
#include <iostream>

using namespace std;

void printName();


int main() {
string name;
int num;

	cout << "Please enter your name. ";
	getline(cin, name);

	cout << endl;
	
	cout << "How many times would you like to print your name?";
	cin >> num;
cout << "\n\n";

	for(int i = 0; i <= num; i++) {
		printName(name);
	}
               cin.get();
	return 0;
}

void printName(string name) {
	cout << name << endl;
               return;
}



You have to have the <= sign for this though.
Last edited on
You have to have the <= sign for this though.


So it will print num+1 times? Realise that the loop ends when the end condition becomes false.

Cronnoc's code has the normal idiom for doing something num times on line 22 .

There is no need for a return statement in a void function, unless you want the function to return early. Putting one at the end is pointless.

The use of getline is recommended because the '\n' one uses to enter a value when using std::cin is left in the stream, and is encountered in the next std::cin, causing problems.

Topic archived. No new replies allowed.