-1

This is my code, and I can't find the problem. I don't understand the error it's putting out.

using System;

class MainClass {
  public static void Main (string[] args) {
    var Number = 0;
while ((Number < 1) | (Number > 10))  {
  Console.WriteLine("Enter a positive whole number: ");
  Number = Console.ReadLine();
  if (Number > 10) {
    Console.WriteLine("Number too large.");
  } else {
    if (Number < 1) {
      Console.WriteLine("Not a positive number.");
    }
  }
}
var c = 1;
for (int k = 0; k < Number - 1; k++) {
  Console.WriteLine(c);
  c = (c * (Number - 1 - k)) % (k + 1);
}
;
  }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    Whats the error message? – tkausl Oct 01 '20 at 23:00
  • 1
    Line numbers/stacktrace ect would be helpful here. At a glance I doubt `(Number < 1) | (Number > 10)` is intended - should be `(Number < 1) || (Number > 10)` ? – asawyer Oct 01 '20 at 23:01
  • Your question is a duplicate from https://stackoverflow.com/questions/23055775/compiler-error-message-cs0029-cannot-implicitly-convert-type-int-to-string. Console.ReadLine() returns a string and you cannot compare a string to an int (in your if). You have to transform the Console.ReadLine return to an int with int.Parse(). – Gimly Oct 02 '20 at 07:20

2 Answers2

0

I might say that you are having a type error, look when you put an input you receive an "string" then you are trying to compare it against a "int" type which in C# are not equivalents, so you have to cast your input to "int"

int Number = 0;
Number = (int)Console.ReadLine();

By doing so, btw, you might have another error there, if you try to write a "string" into that input. So keep it to numbers :) gl

0

Compiler Error CS0029

Cannot implicitly convert type 'type' to 'type'

You are trying to convert a string into an int. They are completely different types. however, there are methods to parse strings to int, since this is user input the best such method would be the int.TryParse method

Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.

var number = 0;
while ((number < 1) | (number > 10))
{
   Console.WriteLine("Enter a positive whole number: ");
   if (!int.TryParse(Console.ReadLine(), out number))
      Console.WriteLine("You didn't even enter a number");
   else if (number > 10)
      Console.WriteLine("Number too large.");
   else if (number < 1)
      Console.WriteLine("Not a positive number.");
}

Another approach would be to combine the checks

var number = 0;
Console.WriteLine("Enter a positive whole number between 1 and 10 : ");
while (!int.TryParse(Console.ReadLine(), out number) || number > 10 || number <= 0)
   Console.WriteLine("You failed, try again");

Console.WriteLine(number);
TheGeneral
  • 79,002
  • 9
  • 103
  • 141