C# does not convert Number with int.TryParse [closed] - 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);
}

Related

Could not push integer value in Stack. Can not convert char to int [closed]

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) );

Operator '==' cannot be applied to operands of type error [duplicate]

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 8 years ago.
Improve this question
private void button1_Click(object sender, EventArgs e)
{
Random random = new Random();
int getal1 = random.Next(0, 100);
Random random2 = new Random();
int getal2 = random2.Next(0, 100);
int Antwoord = getal1 + getal2;
if (Antwoord == textBox1.Text);
...
}
it says
Operator '==' cannot be applied to operands of type 'string' and 'int'
can someone help me?
if (Antwoord.ToString() == textBox1.Text);
Write it like this. You want to check int to string, this can't happen. You should convert the int value to string or the string value to int. I advice you to convert int to string in other case you can have an exception.
If you need an integer value, entered in the TextBox, you should try to parse its Text:
int textBox1Value;
if (int.TryParse(textBox1.Text, out textBox1Value))
{
// Here the text was successfully parsed to the textBox1Value variable
if (Antwoord == textBox1Value)
{
... // do your stuff
}
}

using strings inside if statement throw error "Operator '||' cannot be applied to operands string and string" [closed]

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
Hi my c# is not what it used to be and have just come back after using java script for a while.
Essentially I am just trying to do a simple if statement using index of arrays but I receive the error message.
"Operator '||' cannot be applied to operands string and string"
How come this is not allowed as it essentially becoming a bool.
string[] userCustomAnswerArray = {"It needs to be reaplaced", "This could be improved", "I struggle to see this"};
int customResponseindex = rand.Next(0, 3);
string[] questionResponseArray = { "Yes", "No but not a problem", userCustomAnswerArray[customResponseindex] };
int questionResponseIndex = rand.Next(0, 3);
string userAnswer = questionResponseArray[questionResponseIndex];
if (userAnswer = questionResponseArray[0] || userAnswer = questionResponseArray[1])
{
}
Thanks for your help !!!!
userAnswer = questionResponseArray[0] is incorrect.
= is the assignment operator while == is the equality operator

Code unreachable when comparing types [closed]

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)
{
//...

Why can't i convert from Fahrenheit to Celsius? [closed]

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().

Categories