Trouble with Continue and Break statements

Pages: 12
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.

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>
#include <cmath>
using namespace 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;
}
Last edited on
Your choice is a char, not a bool. It stores what the user did input.

"flag" is a name for variables that are usually of type bool and are used to deliver on/off messages.

1
2
3
4
5
bool next {true};
for ( int count = 0; next && (count <= 10); ++count )
{
  // code
}
Last edited on
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.

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
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	int sales = 5;
	int QUOTA_AMOUNT = 4;
	int salesQuotaMet;
	
	if (sales >= QUOTA_AMOUNT)
	{
		salesQuotaMet = true;
	}
	else
	{
		salesQuotaMet = false;
	}
	
	if (salesQuotaMet == true)
	{
		cout << "You have met your sales quota!\n";
	}

	return 0;
}


Second Code:

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
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	int sales = 5;
	int QUOTA_AMOUNT = 4;
	int salesQuotaMet;
	
	if (sales >= QUOTA_AMOUNT)
	{
		salesQuotaMet = 1;
	}
	else
	{
		salesQuotaMet = 0;
	}
	
	if (salesQuotaMet == 1)
	{
		cout << "You have met your sales quota!\n";
	}

	return 0;
}
Last edited on
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

do-while
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>

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)
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>

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
    }
}

closed account (48T7M4Gy)
The idea of getting at least the first answer without being interrogated is sensible, so:
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
37
#include <iostream>
#include <cmath>

using namespace 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?

Thanks again.
closed account (48T7M4Gy)
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.
Last edited on
Topic archived. No new replies allowed.
Pages: 12