how can I correct this code that proves whether a number is prime or not?

Hi,
I want to write a code which defines whether a number is prime or not. Also, ask me if I want to continue for entering next number. Below is my code. I run my program, but it is clear that there are some problems that I cannot find them.

#include "stdafx.h"
#include <iostream>
using namespace std;
void T (int);
int main()
{
int x;
char ch;
while(true){
cout<<'x=';
cin>>x;
T(x);
cout<<"do you have desire to continue(y/else)";
cin>>ch;
if(ch!='y')
break;

}
cin.get();
cin.get();
}
void T (int x){
int i;
for(i=1;i<=(x/2);i++){
if(x%i==0)
cout<<"no prime";
else
cout<<"prime";}
}

Please help me in this regard. What is the mistake?
Many Thanks,
closed account (48bpfSEw)
This is not a string:

'x='


use instead

"x="


And use "<< endl" to break the line.

To give you an idea:

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
#include <iostream>

//Function Prototypes.
void inputNumber(int& num);
void isPrime(const int num);

int main()
{
    int number{ 0 };
    bool keepLooping{ true };
    char tryAgain{ 'Y' };

    while (keepLooping)
    {
	   inputNumber(number);
	   isPrime(number);

	   std::cout << "Do you want to try again?(Y/N):";
	   std::cin >> tryAgain;

	   switch (tryAgain)
	   {
		  case 'y':
		  case 'Y': keepLooping = true;
			 break;

		  case 'n':
		  case 'N': keepLooping = false;
			 break;

		  default:
		  {
			 std::cout << "Your choice " << tryAgain <<
			 " is invalid.\n";
			 std::cout << "The program will close...\n";
			 keepLooping = false;
		  }			 
	   }
    }
    return 0;
}

void inputNumber(int& num)
{
    std::cout << "Enter a number to determine if it is a prime or not: ";
    std::cin >> num;
}

void isPrime(const int num)
{
    if (num % 2 == 0)
	   std::cout << num << " is prime.\n";

    else
	   std::cout << num << " is not a prime.\n";
}
Last edited on
Topic archived. No new replies allowed.