cout doens't work after operation break

Hello guys. I've got a question. I need to program with C++ language and have the following output:

Enter an integer greater than 1 to check if it is a prime number: 3

The number 3 is a prime number.

Do you want to enter another number? (y or n): y

Enter an integer greater than 1 to check if it is a prime number: 4

The number 4 is NOT a prime number.

Do you want to enter another number? (y or n): n

Exiting program ...


But I found that the "Exiting program ..." cout doesn't work after the "break". Please assist me.


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
38
39
40
  #include <iostream>
using namespace std;
int main()
{
   int n,flag,number;
   char c;
   while(true)
   {  
cout << "Enter an integer greater than 1 to check if it is a prime number: ";
cin>>n;
flag=0;
for(int i=2;i*i<=n;i++)
{
   if(n%i==0)
   {
       flag=1;
       break;
   }
}
number = n;
   if(flag==0)
{ cout << "The number " << number << " is a prime number." << endl;}
else
{cout << "The number " << number << " is NOT a prime number." << endl;}
  
cout << "Do you want to enter another number? (y or n): ";
   cin>>c;
   if(c=='y')
   {
       continue;
   }
   else
   {
       break;
   }
   cout << "Exiting program ..." << endl;
}
  
return 0;
}
Last edited on
Your code is fairly hard to read because of the use of inconsistent indentation. I suggest you try to improve that aspect of the program to make your program easier to read.

Now to the problem. I would recommend changing your while() loop condition to
 
while(c == 'y' || c == 'Y')

And then you won't need the continue, break, and the if() statement at the end of your loop. Be sure to initialize c to some value before the loop. And then move your "Exiting program ..." statement to after the while() loop.


Your program is very inconsistently indented and, hence, incredibly difficult to read.

I think the break in line 34 would exit the while loop whose opening brace is line 8 and closing brace is line 37 (but it's very hard to see!). The program then ends.

Perhaps you want to put your "Exiting program ..." output BEFORE the break statement on line 34. Otherwise it will never run.
Your program with consistent indentation:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>

using namespace std;

int main()
{
    int n, flag, number;
    char c;

    while(true)
    {
        cout << "Enter an integer greater than 1 to check if it is a prime number: ";
        cin >> n;
        flag = 0;

        for(int i = 2; i * i <= n; i++)
        {
            if(n % i == 0)
            {
                flag = 1;
                break;
            }
        }

        number = n;

        if(flag == 0)
        {
            cout << "The number " << number << " is a prime number." << endl;
        }
        else
        {
            cout << "The number " << number << " is NOT a prime number." << endl;
        }

        cout << "Do you want to enter another number? (y or n): ";
        cin >> c;

        if(c == 'y')
        {
            continue;
        }
        else
        {
            break;
        }

        cout << "Exiting program ..." << endl;
    }

    return 0;
}


Do you see how much easier the above is to read and follow the program logic?

Also you should start using meaningful variable names. Your use of the single letter variable names is not a good practice and as your programs grow their use will tend to make your programs unreadable as well.
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>

using namespace std;

int main()
{
  
	int n, flag, number;
   	char c;
  
   	while(true)
   	{
      
		cout << "Enter an integer greater than 1 to check if it is a prime number: ";
		cin >> n;
      
		flag=0;
   
		for(int i=2;i*i<=n;i++)
		{
          
   			if(n%i==0)
  		 	{
              
       			flag=1;
       			break;
              
   			}
          
		}
      
		number = n;
      
   		if(flag==0)
		{ 
          
          	cout << "The number " << number << " is a prime number." << endl;}
		
      	  	else
		  	{
          
          	  	cout << "The number " << number << " is NOT a prime number." << endl;
        
         	}
  
		cout << "Do you want to enter another number? (y or n): ";
  		cin >> c;
      
   		if( c == 'y' )
   		{
     
      		continue;
          
   		}
      
  	 	else
   		{
          
          	cout << "Exiting program ..." << endl;
          
       		break;
          
   		}
      
	}
  
return 0;
}


I improved the indentation inconsistencies. And I replace the cout "Exiting program ..." before break and it solved this problem.
Last edited on
Thank you guys. I really appreciate your assistance.

jlb, please teach me the ways to use more meaningful and understandable letters.
jlb, please teach me the ways to use more meaningful and understandable letters.

No, don't use single letters use meaningful variable names. For example in your code use something like choice instead of c.

Topic archived. No new replies allowed.