I am a bit confused on what you mean about using a boolean flag. Could you give me an example on how to do this? I looked in my book and it really does not talk about flags that much. I decided to give it a try but I know it's totally wrong logic wise.
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
double value;
char choice;
cout << "Enter a number: ";
cin >> value;
cout << "This program will raise " << value << " to the power of 0 through 10.\n";
for (int count = 0; count <= 10; count++)
{
cout << value << " raised to the power of " << count << " is " << pow(value, count) << endl;
cout << "Enter Q to quit or any other key ";
cin >> choice;
if (choice == 'Q' || choice == 'q')
{
choice = false;
}
else
{
choice = true;
}
}
return 0;
}
How can it be done by what @MikeyBoy suggested? All I really know from flags is that if it's set to true, it means it has to exist and if it's false, it will not exist. My book explains two types of flags with two sets of examples:
Also I am not familiar with the term "next". I understand these codes but I don't know how to implement the logic with my original program.
Assuming that:
a. we should ask the user whether to quit now only after the first result has been printed
b. we should not ask the user whether to quit now after the last result has been printed
c. we do not want to use jump statements
#include <iostream>
bool quit()
{
std::cout << "Enter Q to quit or any other character to continue: " ;
char choice ;
std::cin >> choice;
return choice == 'Q' || choice == 'q' ;
}
int main()
{
double value;
std::cout << "Enter a number: ";
std::cin >> value;
std::cout << "This program will raise " << value << " to the power of 0 through 10.\n";
double pow = 1.0 ;
int count = 0 ;
std::cout << std::fixed ;
do
{
std::cout << '\n' << value << " ^ " << count << " == " << pow << '\n' ;
pow *= value ; // compute the next power
++count ;
}
while( count<11 && !quit() ) ; // check count first, ask the user
// only if all powers have not yet been printed
}
for (has an extra check for count within the loop body)
#include <iostream>
bool quit()
{
std::cout << "Enter Q to quit or any other character to continue: " ;
char choice ;
std::cin >> choice;
return choice == 'Q' || choice == 'q' ;
}
int main()
{
double value;
std::cout << "Enter a number: ";
std::cin >> value;
std::cout << "This program will raise " << value << " to the power of 0 through 10.\n";
double pow = 1.0 ;
std::cout << std::fixed ;
for( int count = 0 ; count < 11 ; ++count )
{
std::cout << '\n' << value << " ^ " << count << " == " << pow << '\n' ;
// check count first, ask the user
// only if all powers have not yet been printed
if( count < 10 && quit() ) count = 11 ; // setting count to 11 exits the loop
else pow *= value ; // compute the next power
}
}
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
double value;
char choice = '0';
cout << "Enter a number: ";
cin >> value;
cout
<< "This program will raise " << value
<< " to the power of 0 through 10.\n";
for (
int count = 0;
count <= 10 &&
toupper(choice) != 'Q';
count++
)
{
cout
<< value << " raised to the power of "
<< count << " is " << pow(value, count) << endl;
cout << "Enter Q to quit or any other key ";
cin >> choice;
}
cout << "Finished\n";
return 0;
}
@kemort this is a pretty cool with of doing it, never thought about it. I have never seen the term "toupper" so I have a few questions about your program.
First, why are you defining choice as "0"?
Second, why is toupper inside the for loop parenthesis? (for (int count = 0; count <= 10 && toupper(choice) != 'Q'; count++) Can it be anywhere else, or it has to be there and is the most logical choice
I just don't get how it's cancelling the program once the person hits either Q or q mainly because all of the stuff in the loop is doing a repetition of 10 times, but entering Q will cancel it is my problem. Is it because of the logic that "toupper(choice) can not be equel to either Q or q" otherwise it will cancel the whole loop?
a) choice needs to be initialized to some (arbitrary) controllable value so I picked any character other than 'Q' or 'q' because that would mean when it comes to be tested in the for loop you would be dumped out before you got going. Note also the single quotes not double quotes.
b) check out toupper in the reference section. What the toupper() function does is convert the input character to upper case so you don't need the 'Q' || 'q' test, just test for 'Q'.
c) the test has to be where it is in the for(... ; iteration test ; ... ){ ... } loop, ie the middle section. Note that the iteration test can be as complex as you need hence the && in this case. I separated the loop into separate lines to keep it manageable/readable but you can have a single line a mile long if you want. The blank lines and separate line structures are ignored by the compiler. The test is such that the count must be less than or equal to 10 AND Q/q has not been answered. ie if you answer the question with any other character and the count hasn't reached 10 then the calculation proceeds, followed by the same quit question.
d) I have shown you two versions but this last one incorporates the comment by JLBorges that you would want to run the loop at least oncewhich makes sense - why run it if you want to quit. So I took the quit question out of the loop and put it after the first calculation inside the loop :)
http://www.cplusplus.com/reference/cctype/toupper/?kw=toupper
http://en.cppreference.com/w/cpp/language/for (looks a bit heavy but worth reading)
BTW using functions with bool return is a good move for re-uasabilty if the same question arises for other parts of a more complex program. Write once-use many.