Re: Critique What I've Relearned?
Quote:
Originally Posted by Shanked
I figured \n and endl were interchangeable so I used them as such. You're suggesting that one or the other is better in another situation?
In short,
Code:
std::cout << "HelloWorld" << std::endl;
is effectively the equivalent of writing
Code:
std::cout << "HelloWorld" << "\n" << std::flush;
Hence, std::endl is a more expensive operation in terms of performance than "\n". Therefore, if you only want to get a new line, then using "\n" is more efficient than using std::endl.
In general, if you want a new line but don't need to flush the buffer, use "\n". If you don't want a new line but need to flush the buffer then use std::flush. If you need a new line and flush the buffer, then use std::endl.
I hope that helps. :)
Re: Critique What I've Relearned?
Well, here's the new and improved counter:
Code:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
void instructions (char response_1, bool& repeat_instructions)
{
string response_2;
bool instructions = true;
if (response_1 == 'y')
{
while (instructions == true )
{
cout << "The instructions are simple:\n\n"
<< "When the program prompts you, follow its instructions...\n"
<< "Choosing up (u) will make the count increase.\n"
<< "Choosing down (d) will make the count decrease.\n"
<< "When Counter reaches 0, it will terminate.\n\n";
cout << "Type \"ready\" to continue\n";
cin >> response_2;
if (response_2=="ready")
{
instructions = false;
cout << "Have fun!\n";
}
else
{
cout << "Let's try again...\n\n";
}
}
repeat_instructions = false;
}
else if (response_1 == 'n')
{
cout << "On with Counter then!\n\n";
instructions = false;
repeat_instructions = false;
}
else
{
cout << "Not an acceptable response, try again.\n(y/n)";
cin >> response_1;
repeat_instructions = true;
}
}
void IncrementCounter(bool& program, int& counter)
{
string response_3;
bool quit = false;
char response_3a;
cout << "Counter is: "<< counter << "\n"
<< "Up or down? (u/d)" << "\n";
getline(cin,response_3);
if (response_3 == "u")
{
counter++;
}
else if (response_3 == "d")
{
counter--;
}
else if (response_3 == "quit")
{
program = false;
quit = true;
counter = 0;
}
else
{
cout << "Incorrect response, try again...\n";
}
if (counter <= 0)
{
program = false;
if (quit == true)
{
cout << "Oh well... thanks for playing!\n";
}
else
{
cout << "Counter has reached 0, Thank you for playing!\n";
}
system ("PAUSE");
}
else
{
program = true;
}
}
int main ()
{
bool program = true, repeat_instructions = true;
char response_1;
int counter = 1;
string response_2;
cout << "Welcome to Counter!\n"
<< "Would you like to read the instructions?(y/n)\n";
cin >> response_1;
while (repeat_instructions == true)
{
instructions(response_1, repeat_instructions);
}
while (program == true)
{
IncrementCounter(program, counter);
}
return 0;
}
Re: Critique What I've Relearned?
That's not improved. Passing something in as an argument when it can be used as a return value is a step in the wrong direction. You had it right the first time when it was returning a bool.
Re: Critique What I've Relearned?
The purpose of functions is to encapsulate functionality in a modular way----so you can call them at any point in a program, with some inputs, and get some output(s). That's one reason why global vars are frowned upon; if you use them, you can have functions giving different results for the same input depending on when they're called, which is bad.
There's no harm in designating some parameters as outputs; however, they should be clearly marked as such. Also, in most cases it's best to avoid inout parameters----those which both hold an output *and* may change behavior depending on their initial values.
Re: Critique What I've Relearned?
Quote:
Originally Posted by GCDEF
That's not improved. Passing something in as an argument when it can be used as a return value is a step in the wrong direction. You had it right the first time when it was returning a bool.
I was under the impression that it didn't matter so much. Easy enough to fix.
Re: Critique What I've Relearned?
Alright... new and improved!
Code:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
bool instructions (char response_1, bool repeat_instructions)
{
string response_2;
bool instructions = true;
if (response_1 == 'y')
{
while (instructions == true )
{
cout << "The instructions are simple:\n\n"
<< "When the program prompts you, follow its instructions...\n"
<< "Choosing up (u) will make the count increase.\n"
<< "Choosing down (d) will make the count decrease.\n"
<< "When Counter reaches 0, it will terminate.\n\n";
cout << "Type \"ready\" to continue\n";
cin >> response_2;
if (response_2=="ready")
{
instructions = false;
cout << "Have fun!\n";
}
else
{
cout << "Let's try again...\n\n";
}
}
repeat_instructions = false;
}
else if (response_1 == 'n')
{
cout << "On with Counter then!\n\n";
instructions = false;
repeat_instructions = false;
}
else
{
cout << "Not an acceptable response, try again.\n(y/n)";
cin >> response_1;
repeat_instructions = true;
}
return repeat_instructions;
}
bool IncrementCounter(bool program, int& counter)
{
string response_3;
bool quit = false;
char response_3a;
cout << "Counter is: "<< counter << "\n"
<< "Up or down? (u/d)" << "\n";
cin.sync();
getline(cin,response_3);
if (response_3 == "u")
{
counter++;
}
else if (response_3 == "d")
{
counter--;
}
else if (response_3 == "quit")
{
program = false;
quit = true;
counter = 0;
}
else
{
cout << "Incorrect response, try again...\n";
}
if (counter <= 0)
{
program = false;
if (quit == true)
{
cout << "Oh well... thanks for playing!\n";
}
else
{
cout << "Counter has reached 0, Thank you for playing!\n";
}
system ("PAUSE");
}
else
{
program = true;
}
return program;
}
int main ()
{
bool program = true, repeat_instructions = true;
char response_1;
int counter = 1;
string response_2;
cout << "Welcome to Counter!\n"
<< "Would you like to read the instructions?(y/n)\n";
cin >> response_1;
while (repeat_instructions == true)
{
repeat_instructions = instructions(response_1, repeat_instructions);
}
while (program == true)
{
program = IncrementCounter(program, counter);
}
return 0;
}
Re: Critique What I've Relearned?
-There is no reason to pass 'repeat_instructions' to instructions(), as you are not using that value in that function. The 'repeat_instructions' in the instructions() should be a local variable. Same with 'program' in IncrementCounter()
-As for your main loop, i would probably write something like:
Code:
while( instructions(response_1) )
{;}
while( IncrementCounter(program, counter) )
{;}
but people will probably say that this is less readable, but you can see how return values can be used
Re: Critique What I've Relearned?
Quote:
Originally Posted by Shanked
I was under the impression that it didn't matter so much. Easy enough to fix.
No, and I gave you an example why. Functions are often used as part of expressions. For example, if you had a function that did some kind of calculation and gave you a number, you could write it and use it as
Code:
int GetSomeNumber()
{
// do some calclations
return number;
}
or
Code:
void GetSomeNumber(int& number)
{
// do some calculations and set number's value
}
To use the first function you could simply use it in an expression as
Code:
int newNumber = GetSomeNumber() * 2;
The second form would be
Code:
int newNumber;
GetSomeNumber(newNumber);
newNumber *= 2;
Three lines of code instead of one, and not the way C and C++ coders are used to thinking.