Question Programming a prime number detector

Ah Zong

New member
Joined
Aug 21, 2018
Messages
2
Programming Experience
Beginner
I was trying to program a prime number detector, pretty close to success.
Here is my code, along with the error by the compiler, which I can't figure what it means.

C#:
using System;					
public class Program
{
	public static void Main()
	{
		string Input(){
			string input = Console.ReadLine();
			return input;
		}
		
		while(true){
			bool isPrime = true;
			Input();
			
			int value;
			if(int.TryParse(Input(), out value)){
				long x = Convert.ToInt64(Input());
			
				if(x == 2 ){
					isPrime = true;
				}
				else if(x % 2 == 0){
					isPrime = false;
				}
				else{
					for(int k = 3; k < x; k += 2){
						if(x % k == 0){
							isPrime = false;
						}
					}
				}
					
				if(isPrime == true){
					Console.WriteLine(Input() + " is prime");
				}
				else{
					Console.WriteLine(Input() + " is not prime");
				}
			}
			else{
				Console.WriteLine("Please enter a valid integer");
			}
		}
	}	
}

Compilation error (line 6, col 3): } expected
Compilation error (line 12, col 3): Invalid token 'while' in class, struct, or interface member declaration
Compilation error (line 14, col 4): Method must have a return type
Compilation error (line 17, col 4): Invalid token 'if' in class, struct, or interface member declaration
Compilation error (line 17, col 10): Invalid token '.' in class, struct, or interface member declaration
Compilation error (line 17, col 11): Method must have a return type
Compilation error (line 17, col 25): Identifier expected
Compilation error (line 17, col 38): Invalid token ')' in class, struct, or interface member declaration
Compilation error (line 20, col 5): Invalid token 'if' in class, struct, or interface member declaration
Compilation error (line 20, col 10): Invalid token '==' in class, struct, or interface member declaration
Compilation error (line 21, col 14): Invalid token '=' in class, struct, or interface member declaration
Compilation error (line 23, col 5): A namespace cannot directly contain members such as fields or methods
Compilation error (line 40, col 4): Type or namespace definition, or end-of-file expected

Debugging is the part which bothers me the most. I tried so hard finding what's wrong in my code but couldn't find one. Maybe I was too tired?
Anyway, many thanks for replying.
 
I just pasted your code into a new Console application project and it built without issues. If it's not working for you then there may be something broken in your system or maybe you're using an older version of C# that doesn't support local functions. Given that the first error message refers to the line on which the Input function is declared, I suspect that that last may be the issue. Local functions were introduced in C# 7.0. If you move that function, i.e.
string Input()
{
    string input = Console.ReadLine();
    return input;
}

outside the Main method and declare it 'public static', does you code compile then? To be explicit, change:
public static void Main()
{
    string Input(){
        string input = Console.ReadLine();
        return input;
    }

    while (true){

to:
public static string Input(){
    string input = Console.ReadLine();
    return input;
}

public static void Main()
{
    while (true){
 
There's no more error when I moved the function.
But it doesn't show what I wanted hahah, the program has messed up a bit.
Will try to fix it myself, and ask again if it fails.
 

Latest posts

Back
Top Bottom