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.
#include <iostream>
usingnamespace 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;
}
#include <iostream>
#include <string>
usingnamespace 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;
}
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?
#include <iostream>
#include <string>
usingnamespace 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.
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.
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.
#include <iostream>
#include <string>
usingnamespace 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;
}
#include <iostream>
usingnamespace 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;
}
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.