Not being able convert from string to int
static void test()
{
Console.Write("Ange det första heltalet: ");
string input1 = int.Parse(Console.ReadLine());
Console.Write("Ange det andra heltalet: ");
string input2 = int.Parse(Console.ReadLine());
int summa = input1 + input2;
Console.WriteLine("Summan är:" + summa);
}
what am i doing wrong?
43 Replies
Well, uh, you're trying to parse a string to an integer, yeah?
So the result is... an integer
You're not gonna save an integer in a variable of type
string
But how do i convert the string into int then
You already did
then why cant i add the nummers
But now you're trying to save that int in a variable of type string
Square peg does not fit round hole
Write a program that allows the user to enter two integers as strings. The program will then convert these strings to integers, perform an addition, and print the result.
What does
int.Parse()
return?
What type?int
So why are you assigning it to a
string
variable?Because my teacher says so
Then either you misunderstood your teacher, or they're mentally-deficient
idk shes kinda special
From this description that doesn't sound like what she said
Its in Swedish so maybe im misunderstanding
I just translated using google translator
Let the user write two numbers as strings. Then make the program convert the string into int and add the numbers
That's what you're doing, but in fewer steps
so im not supposed to use int.parse?
Maybe you need
Doesn't make much of a difference whether you do it this way, step by step, or the way you do it now
But this is the only reasonable scenario where I can see you having
string
variablesbut the way is was doing it now doesn't work in vs
im sorry im so slow
Re-read our conversation then
And understand why an integer cannot be placed in a string variable
But cant you convert the string
YOU ARE CONVERTING IT
AND THE RESULT IS AN INTEGER
BUT WHY DOES IT SAY IM NOT
IN VS
THAT HAS TO BE SAVED IN AN
INT
VARIABLE
NOT A STRING
VARIABLESQUARE
PEG
DOES
NOT
FIT
ROUND
HOLE
@Aelia if you want to save an
int
variable, how would you write that, what comes before the = sign?i would write Int input1 = Console.ReadLine();
Ignore the bit on the right hand side, but yes that's right
omg
i think i understand now
You cant write it like this int input1 = Console.ReadLine();
You write it like this int input1 = int.Parse(Console.ReadLine());
nvm
no
that was right
sorry i focused on the wrong part of the code thought I did something wrong with .Parse like didn't spell it right
If there's a typo like that then your error will tell you it doesn't know what you mean (you can easily try that and see what it says)
So uhhh
You ignored the article I linked you about this?
Oh no
It was really long
Are you 12?
no just have adhd
if you can't make it through that article (and you dont need to read the whole thing top to bottom lol), you're gonna have a really hard time learning programming
Im not doing this for work just school
How does that change anything?
You still need to learn it, at least partially
Well now I did
i understood what I did wrong
Good