// Use a global variable.
#include <iostream>
usingnamespace std;
void func1();
void func2();
int count; // This is a global variable.
int main()
{
int i; // This is a local variable
for(i=0; i<10; i++) {
count = i * 2;
func1();
}
return 0;
}
void func1()
{
cout << "count: " << count; // access global count
cout << '\n'; // output a newline
func2();
}
void func2()
{
int count; // this is a local variable
for(count=0; count<3; count++) cout << '.';
}
This is quite odd considering it's from a book i'm studying, with a known author...
single quotations are for single characters 'a' 'b' 'c', double quotations are for strings and have a hidden character, the null character. so "a" is actually 'a' and '\0'.
tl;dr 'x' is for characters
"string" is for strings (anything more than one letter)
Because you are using cout to output a string of characters, I believe that double quotes are more commonly used but single quotes can be used because \n is a character.
Better yet, don't ever write this into your cpp files. usingnamespace std;
Think of all the other names that are now going to cause a problem. Is it so terrible to just write std::cout and std::cin and std::endl? Keeping the "std::" with the usage of those std names results in clearer code and you can have a variable called count if you'd like because there won't be a naming conflict at all.
I find it a hassle to write "std::" before cout. Another approach, and better than using namespace std;, is to use a more specific using declaration. For example, "using std::cout;" will allow you to use the unqualified "cout" and will not bring anything else from namespace std into scope.
I agree with seymore but it is mostly a matter of preference. I personally prefer using std::cout to writing std:: before each line partially because of the chance that I would slip and only put one : or some other syntax error that I could have prevented myself from having to debug later but that is just me.
seymore you said about not wanting to bring anything else from the namespace std into scope... what other things are there? apart from the given cout, cin, endl ^^