I am trying to save the value i get through Console.Read() in an Integer, but whatever i type in my keyboard, the console always gives out 13. I tried to copy an example code, which definitly has to work, but im still only getting 13 as value.
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Console.Read();
int Test = Console.Read();
Console.WriteLine(Test);
}
}
}
After i typed in a number, it always shows '13' in the console.
Console.Read() reads first character from input stream.
In your case, You are trying to convert second character of input stream(i.e.after first Console.Read()) which is Carriage return and its ASCII value is 13, Test variable which is of type int, storing carriage return in integer format. i.e 13
If you want to convert first character from input stream, then try below
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
int Test = Console.Read(); //Or you can use Console.ReadLine() to read entire line.
Console.WriteLine(Test);//Print first character of input stream
}
}
}
Here, you have to use Console.ReadLine to read the number. As Console.ReadLine returns string, you have to convert it to Int32. The following code will fix the issue
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
int Test = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(Test);
}
}
}
Try changing this to:
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
int Test = Console.ReadLine();
Console.WriteLine(Test);
}
}
}
Since 13 is key code for enter, you are probably getting that number from trying to read the whole line.
Related
I'm new to coding. I am trying to improve myself by making simple coding. I just encountered such error while writing a code to retrieve data from user.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FuelCalc
{
internal class Program
{
static void Main(string[] args)
{
//readline
Console.WriteLine("Lütfen size hitap edebilmem için adınız ve soyadınızı giriniz...");
string namesurn= Console.ReadLine();
Console.WriteLine("Merhaba " + namesurn +" lütfen aracınızın ortalama yakıt tüketimini giriniz...");
int yakit = Console.ReadLine();
Console.ReadLine();
}
}
}
What you could try is adding a loop to repeat until the user enters a number. The int.TryParse method will return true if the string returned from Console.ReadLine() can be interpreted as an int and you will have a value for yakit in that case. Otherwise, ask the user to try again.
(I'm relying on Google Translate here leave me a comment if I did something wrong!)
static void Main(string[] args)
{
//readline
Console.WriteLine("Lütfen size hitap edebilmem için adınız ve soyadınızı giriniz...");
string namesurn = Console.ReadLine();
Console.WriteLine("Merhaba " + namesurn + " lütfen aracınızın ortalama yakıt tüketimini giriniz...");
// Loop until a valid int is received.
while(true)
{
if(int.TryParse(Console.ReadLine(), out int yakit))
{
Console.WriteLine($"{yakit} alınan");
break;
}
else
{
Console.WriteLine("Numara gerekli");
}
}
Console.ReadLine();
}
You may want to consider float or double instead of int in case the user enters a decimal point character like "40.9" in which case you could substitute:
float.TryParse(Console.ReadLine(), out float yakit)
I'm trying to calculate a big number using Mathf.Pow() but when I place a breakpoint it shows infinity, I already tried using System.Numerics.BigInteger but it shows Big Integer cannot display infinity
Here's my code
using System;
using System.Numerics;
namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
//43
BigInteger res = new BigInteger(MathF.Pow(43, 27));
Console.WriteLine(res);
}
}
}
As Jeremy said
You could try BigInteger.Pow instead
I used BigInteger res = BigInteger.Pow(43,27); and that worked, thanks!
Trying to make a simple app which will ask a few questions.
But for some reason, my AskQuestion function doesn't work.
I plan on adding an easily swap able database later which is why I'm trying to take a slightly more modular approach and as I am a beginner I am unsure what I did wrong. The only errors are in line 21 for the AskQuestion class.
Errors are:
CS1001 Identifier Expected
CS1514 { expected
CS1513 } expected
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz
{
class Program
{
// Question Base
class Question
{
public String question = "Empty Question";
public String correctanswer = "Empty Answer";
public String givenanswer = "Empty Answer";
public String response = "Empty Response.";
public bool cleared = false;
}
// Ask Base
class void AskQuestion(Question Q)
{
while (Q.cleared == false)
{
Console.WriteLine(Q.question);
Q.givenanswer = Console.ReadLine();
Q.givenanswer.ToLower();
if (Q.givenanswer == Q.correctanswer)
{
Console.WriteLine(Q.response);
Q.cleared = true;
}
else
{
Console.WriteLine("Wrong. Try again.");
}
}
}
// Main Function
void Main(string[] args)
{
string Name;
Console.WriteLine("Welcome challenger! You're going to have a good time.");
Console.WriteLine("Make sure you use proper grammar. Or you may be stuck for no reason.");
Console.WriteLine("What is your name challenger?");
Name = Console.ReadLine();
Console.WriteLine("Welcome {0} to the challenge. I wish you best of luck. You will need it.",Name);
Question Q1 = new Question();
Q1.question = "What is the color of the sun?";
Q1.correctanswer = "White";
Q1.response = "Correct. Despite the fact it appears Yellow on earth, if you observe the sun from space, you would see it's true color. White.";
AskQuestion(Q1);
Q1.cleared = true;
Console.WriteLine("Nice little warmup. But, lets get a bit serious.");
}
}
}
change this
class void AskQuestion(Question Q)
to
void AskQuestion(Question Q)
This should be a method. The keyword class tells the compiler you want to create a inner class inside the out class Program
Q.givenanswer.ToLower(); doesn't make Q.givenanswer lowercase - it returns a new lowercase string which you need to assign to a variable, or just `Q.givenanswer = Q.givenanswer.ToLower();
So I am making a super basic (I'm in my second week of learning C# so please excuse my ignorance) program that takes a string input from a user and outputs the string backwards. I have copied the book to a T in regards to a majority of it but I have noticed spelling errors in some of their code so I don't have a lot of faith in what they are showing. My compiler is giving me an error with WriteLine and ReadLine and I don't understand why as the book says it works. This is the error I am getting;
"WriteLine does not exist in the current context" same with "ReadLine"
My code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
static class funcStrings
{
public static string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
}
class runProgram
{
class Program
{
static void Main()
{
string name;
WriteLine("Enter your name to be reversed ");
name = ReadLine();
Console.WriteLine(funcStrings.ReverseString(name));
}
}
}
}
Thanks for any guidance here
You just need to add Console. in front:
Console.WriteLine("Enter your name to be reversed ");
name = Console.ReadLine();
You can add such namespace:
using static System.Console;
This brings all the static members from the System.Console class into scope, so that you don't need to prefix them with Console. It is a C# 6 feature, and useful when accessing many members in a static class. See relevant documentation.
The problem is probably that it should be Console.WriteLine and Console.ReadLine.
Currently it is:
static void Main()
{
string name;
WriteLine("Enter your name to be reversed ");
name = ReadLine();
Console.WriteLine(funcStrings.ReverseString(name));
}
You need to add a Console.WriteLine("Enter your name to be reversed"):
I'm taking a programming class in c# and it is our first week and I'm trying to work ahead a little. To that end, I am trying to rework one of our class labs and am stuck with my 'GetInt' method.
Ideally, the GetInt method (line 50) takes in a string (Enter a number) and returns the number as an int so i can then use it to do some math. Currently line #24 turns the test error 'Cannot implicity convert type 'int' to 'string'.
Any help is appreciated. I am very much a newb, so please don't assume i know anything.
Thank you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lec022_If_statement_int
{
class Program
{
//Set to public so it is visible
//void because it returns nothing
//Play is a method within the class Program I've added
public void Play()
{
DisplayStr("Lecture 2c | If Statements with ints");
DisplayReturns();
DisplayStr("Welcome to Dunut King");
DisplayReturns();
//Collect User Name
//GetString converts to lower, trims
String numDonuts = GetInt("How many donuts would you like?: ");
DisplayReturns();
//Display welcome
Console.WriteLine("You asked for " + numDonuts + " donuts.");
DisplayReturns();
DisplayReturns();
DisplayStr("Have a great Day!");
}
//MaxBox 2.0
public void DisplayStr(String StrTxt)
{ Console.Write(StrTxt); }
public void DisplayReturns()
{ Console.Write("\n\n"); }
public string GetString(String StrVar)//note - using strings here
{
Console.Write(StrVar);
return Console.ReadLine().ToLower().Trim();
}
public int GetInt(string intVar)//note - using ints here
{
Console.Write(intVar);
return int.Parse(Console.ReadLine());
}
//Initiate Program
static void Main(string[] args)
{
Program myProgram = new Program();
myProgram.Play();
Console.Read();
}
}
}
Your GetInt method returns int. So you need to change from
String numDonuts = GetInt("How many donuts would you like?: ");
to
int numDonuts = GetInt("How many donuts would you like?: ");
The problem is that the method GetInt returns int.
And you're trying to assign its return value to this String variable on line 24.
String numDonuts
You cannot do this.
Change the first word on line #24 from 'String' to 'int' because I am asking for an int not a string.
Your problem is there:
String numDonuts = GetInt("How many donuts would you like?: ");
You try to set int value to string variable.
change to
var numDonuts = GetInt("How many donuts would you like?: ");
Your GetInt method returns integer type so you should use an integer type to store it, not a string.
Here is the fixed code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lec022_If_statement_int
{
class Program
{
//Set to public so it is visible
//void because it returns nothing
//Play is a method within the class Program I've added
public void Play()
{
DisplayStr("Lecture 2c | If Statements with ints");
DisplayReturns();
DisplayStr("Welcome to Dunut King");
DisplayReturns();
//Collect User Name
//GetString converts to lower, trims
int numDonuts = GetInt("How many donuts would you like?: ");
DisplayReturns();
//Display welcome
Console.WriteLine("You asked for " + numDonuts + " donuts.");
DisplayReturns();
DisplayReturns();
DisplayStr("Have a great Day!");
}
//MaxBox 2.0
public void DisplayStr(String StrTxt)
{ Console.Write(StrTxt); }
public void DisplayReturns()
{ Console.Write("\n\n"); }
public string GetString(String StrVar)//note - using strings here
{
Console.Write(StrVar);
return Console.ReadLine().ToLower().Trim();
}
public int GetInt(string intVar)//note - using ints here
{
Console.Write(intVar);
return int.Parse(Console.ReadLine());
}
//Initiate Program
static void Main(string[] args)
{
Program myProgram = new Program();
myProgram.Play();
Console.Read();
}
}
}
I've just tested the program. You enter a number and you get a response. Works nicely. (For it's simple purpose :-) )