yes or no loop

ok so there is a question that tells me to make user chooses whether he wants to try a program or not... and that program is to see whether the number he inserted is prime or not... that part is easy... but then it said..... after that luck up the user in a loop till he/she enters the right response as Y for yes and N for no. thats the hard part and here is the code but nothing happened
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
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    int number;
	char Answer;
	cout << "this program is to check if the number is prime or not... Do you want to start? (Y/N)";
	cin >> Answer;
	if (Answer == 'Y')
	{
   
  
   cout <<"Enter an integer:";
   cin>> number;
for(int i=2; i < number; i++)
{

 if (number%i==0)
 cout << "number is not prime" << endl;
 
 else
 if(number%i!=0)
  cout << "number is prime" << endl;
    
    return 0;
}

}
	else if (Answer == 'N')
		cout << "as you wish.. Have a nice day"<<endl;
	else
		cout << "please insert a valid choice"<<endl;
	cout << "do you want to try again?";
	do
	{
		   cout <<"Enter an integer:";
   cin>> number;
for(int i=2; i < number; i++)
{

 if (number%i==0)
 cout << "number is not prime" << endl;
 
 else
 if(number%i!=0)
  cout << "number is prime" << endl;
    
    return 0;
 
	}
	}while (Answer == 'Y');
}
Last edited on
Please could you enclose your code in code tags? That will make it much more readable, which means that people will be more likely to take the time to help you. Use the "<>" button to the right of the edit window to do this.
Firstly - my omnipresent request for code tags - the <> button on the right.


Have a look at this code I did to help someone else:

http://www.cplusplus.com/forum/beginner/84889/#msg455481


It shows an easy way to quit out of a loop.

Hope all goes well.
I've looked quickly your source.
Here's my remark :

1-

cin >> Answer;
if (Answer == 'Y'){[...]}

If I enter 'y' or 'n', then what do you think? You should add two comparisons to fit the checking input. It should look like :
if (Answer == 'Y' || Answer == 'y'){[...]}
else if (Answer == 'N' || Answer == 'n'){[...]}


2-

for(int i=2; i < number; i++)
{
if (number%i==0)
cout << "number is not prime" << endl;

else
if(number%i!=0)
cout << "number is prime" << endl;}

You should not copy a similar code unless you know what you have to do.
This makes your code longer and harder to read.

3- Capital letters.

4-
while (Answer == 'Y');

It seems redundant. Look at this :
...
cout << "number is prime" << endl;

return 0;

Your loop will be ended automatically & immediately after the program has finished its prime checking. Then what do you think about 'loop' ??
Last edited on
@Jackson Marie... well the problem is... I can't do the loop.... quiet confusing... and thank you for your remarks...... and thank you all for help.. I really appreciate it..
@Omar

I can't do the loop.... quiet confusing..

How did you get on with the code I gave you in the link?

Also, you do not have to test whether all the numbers give a remainder - you only have to go as high as sqrt(n).

You also need to make use of functions - the code you have from lines 8 to 30 is basically repeated on lines 32 to 54.

At the start, the question to start is rather irrelevant.

The use of the return statements causes the program to exit early.

Hope this all helps, looking forward to seeing your new code. :)
Topic archived. No new replies allowed.