Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
My C# code keeps returnng me a wrong conversion result. As far as i know, the conversion formula is correct, but it keeps displaying wrong results.
e.g: 70°F gives me 12,777777 °C. Can you check my code out ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Temperature
{
class Program
{
class Temperature
{
public double temp;
public void Convert(double value)
{
double tEmp;
tEmp = (value - 32) / 1.8;
Console.WriteLine("The temperature in °C is :" + tEmp);
Console.ReadKey();
}
}
static void Main(string[] args)
{
double f;
Temperature c = new Temperature();
f = Console.Read();
c.Convert(f);
}
}
}
your problem is
f = Console.Read();
It just reads the first character, not your entire line of input. Try
f = Convert.ToDouble(Console.ReadLine());
Here's a good answer on the difference between Console.Read vs. Console.ReadLine()
Console.Read() returns the ordinal value of the next character in the input stream.
The first character in your input stream is '7' which has an ordinal value of 0x0037. Represented as decimal this is 55 and (55-32)/1.8 is 12.7777.
You should be using Console.ReadLine() rather than Console.Read().
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
In the Main function, declare three integer variables (name them arbitrarily) and initialize these variables with values (ideally different). Write a program that computes the following arithmetic expression: Multiply the values of the last two variables and subtract the value of the first variable from the obtained result. Write the arithmetic expression and its result on the screen in a suitable way.
using System;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
int prvni = 10;
int druha = 20;
int treti = 30;
int vysledek = (treti * druha) - prvni;
Console.WriteLine("Výsledek: {vysledek}");
}
}
}
String-interpolation in that way requires a $ (dollar-sign) before the string to specify that you are doing interpolation, so: Console.WriteLine($"Výsledek: {vysledek}");
For more examples on string interpolation: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated
An alternative solution could be to simply concatenate the variable to the string: Console.WriteLine("Výsledek: " + vysledek);
You need to print the varable correctly.
Console.WriteLine("the answer {0}", vysledek);
Take care,
Ori
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Hi I am working on a simple code wherein I am trying to push integer into a integer array from a string.
Below is my code:
/*
Input:3[a2[b]] Output: abbabbabb
*/
Stack<int> intChar = new Stack<int>();
Stack<string> strChar = new Stack<string>();
string output = "";
Console.WriteLine("Enter the string of chars");
string input= Console.ReadLine();
for (int i = 0; i < input.Length; i++)
{
char c = input[i];
if(Char.IsDigit(c))
{
intChar.Push(input[i]);
}
that if condition is taking up wrong values and I think it is taking up the ascii value of the character and pushing in the ascii value of the same. below us the screenshot of the debug window.
I want to check the entire string and push the integer and char values to their respective Stacks. Please suggest me where I am doing it wrong.
Apparently type char is converted to ascii code. You have to use string to get the real number. Try this
if (Char.IsDigit(c)) intChar.Push( Convert.ToInt32(c.ToString()) );
another way is to use GetNumericValue function. Since it returns double, it needs to be cast to int.
if (Char.IsDigit(c)) intChar.Push( (int)Char.GetNumericValue(c) );
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This is my code, I am currently using Process to receiving angle values in degrees from my Arduino, which uses a MPU6050 for reading said values.
The following code should receive the datastream from the Serial Port:
string dataString = serialPort.ReadLine();
var dataBlocks = dataString.Split(',');
if (dataBlocks.Length < 3)
{
Debug.LogWarning("Invalid data received");
return;
}
int angleX, angleY, angleZ;
if (!int.TryParse(dataBlocks[0], out angleX))
{
Debug.LogWarning("Failed to parse angleX. RawData: " + dataBlocks[0]);
return;
}
TryParse fails to parse the angle.
"Failed to parse angleX. RawData: 174.0"
As the error message points out, the raw data that was transmitted over the serial interface was 174.0.
Is the error related to using dot and commas for decimal seperation?
There is a decimal separator in your value. This suggests it is not an integer but actually a decimal (or real) value.
You should use decimal.TryParse() or double.TryParse() rather than int.TryParse() then cast the result to int.
string val = "17.0";
decimal d = 0;
int i = 0;
if (!decimal.TryParse(val, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out d))
{
Console.WriteLine("Not a decimal");
}
else
{
i = (int)d;
Console.WriteLine(i);
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am really knew to programming so please be nice haha. Excuse the "Noob" Question, I'm just experimenting right now. Can anyone give me tips on what is causing
CS1513 C# } expected
My code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
string country = "USA";
Console.WriteLine("Hello, What country are you from?");
string countryName = Convert.ToString(Console.ReadLine());
if (countryName == country) ;
{
Console.WriteLine("You are Eligable for the competition ! :-) ");
}
else {
Console.WriteLine("You are not Eligable, Sorry!!");
}
}
}
}
I'm trying to give an answer based on the country of the user basically.
Remove the ; from if (countryName == country) ;
The ; is a statement terminator. See why do some lines not have semicolon in C#?
You put a ; after your if statement.
It should be if (countryName == country) instead of if (countryName == country) ;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Question: My else-statement is unreachable, what am i doing wrong?
very VERY new at programming and i'm trying to compare the type so for example people can't enter strings when i'm asking for integers.
My code is probably pretty bad, if i could get a header what to do and why the if-argument skips the else-part i'd be really happy!
Thanks!
class Program
{
static void Main(string[] args)
{
int integer = 0;
start:
Console.WriteLine("How old are you?: ");
int svar = int.Parse(Console.ReadLine());
Utility.CompareTypes(svar, integer);
if (true)
{
Console.WriteLine("Thanks");
}
else
{
Console.WriteLine("You have to enter a number!");
goto start;
}
}
}
class Utility
{
public static bool CompareTypes<T01, T02>(T01 type01, T02 type02)
{
return typeof(T01).Equals (typeof(T02));
}
}
:c
It's not really a question of code, but of logic...
if (true) // <--- this will ALWAYS be true
{
Console.WriteLine("Thanks");
}
else // <--- therefore this will NEVER happen
{
Console.WriteLine("You have to enter a number!");
goto start;
}
Since your else block can never possibly execute under any logical circumstance, the entire block of code can be simplified to:
Console.WriteLine("Thanks");
In order for the else block to execute, the condition checked in the if statement needs to be false. You're currently not checking any actual condition, just a hard-coded true value.
Perhaps you meant to use the result of the previous line of code? Something like this:
var typesAreSame = Utility.CompareTypes(svar, integer);
if (typesAreSame)
{
//...