C
C#14mo ago
Aelia

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?
No description
43 Replies
Angius
Angius14mo ago
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
Aelia
AeliaOP14mo ago
But how do i convert the string into int then
Angius
Angius14mo ago
You already did
Aelia
AeliaOP14mo ago
then why cant i add the nummers
Angius
Angius14mo ago
But now you're trying to save that int in a variable of type string Square peg does not fit round hole
Aelia
AeliaOP14mo ago
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.
Angius
Angius14mo ago
string input1 = int.Parse(Console.ReadLine());
string input1 = int.Parse(Console.ReadLine());
What does int.Parse() return? What type?
Aelia
AeliaOP14mo ago
int
Angius
Angius14mo ago
So why are you assigning it to a string variable?
Aelia
AeliaOP14mo ago
Because my teacher says so
Angius
Angius14mo ago
Then either you misunderstood your teacher, or they're mentally-deficient
Aelia
AeliaOP14mo ago
idk shes kinda special
SinFluxx
SinFluxx14mo ago
From this description that doesn't sound like what she said
Aelia
AeliaOP14mo ago
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
Angius
Angius14mo ago
That's what you're doing, but in fewer steps
Aelia
AeliaOP14mo ago
so im not supposed to use int.parse?
Angius
Angius14mo ago
Maybe you need
string input1 = getinput
string input2 = getinput

int num1 = input1-to-int
int num2 = input2-to-int

int sum = num1 + num2
string input1 = getinput
string input2 = getinput

int num1 = input1-to-int
int num2 = input2-to-int

int sum = num1 + num2
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 variables
Aelia
AeliaOP14mo ago
but the way is was doing it now doesn't work in vs im sorry im so slow
Angius
Angius14mo ago
Re-read our conversation then And understand why an integer cannot be placed in a string variable
Aelia
AeliaOP14mo ago
But cant you convert the string
Angius
Angius14mo ago
YOU ARE CONVERTING IT AND THE RESULT IS AN INTEGER
Aelia
AeliaOP14mo ago
BUT WHY DOES IT SAY IM NOT IN VS
Angius
Angius14mo ago
THAT HAS TO BE SAVED IN AN INT VARIABLE NOT A STRING VARIABLE
Aelia
AeliaOP14mo ago
pepehands
Angius
Angius14mo ago
SQUARE PEG DOES NOT FIT ROUND HOLE
Aelia
AeliaOP14mo ago
pepehands
SinFluxx
SinFluxx14mo ago
@Aelia if you want to save an int variable, how would you write that, what comes before the = sign?
Aelia
AeliaOP14mo ago
i would write Int input1 = Console.ReadLine();
SinFluxx
SinFluxx14mo ago
Ignore the bit on the right hand side, but yes that's right
Aelia
AeliaOP14mo ago
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 pepecry thought I did something wrong with .Parse like didn't spell it right
SinFluxx
SinFluxx14mo ago
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)
Pobiega
Pobiega14mo ago
So uhhh You ignored the article I linked you about this?
SinFluxx
SinFluxx14mo ago
Oh no
Aelia
AeliaOP14mo ago
It was really long
Pobiega
Pobiega14mo ago
Are you 12?
Aelia
AeliaOP14mo ago
no just have adhd
Pobiega
Pobiega14mo ago
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
Aelia
AeliaOP14mo ago
Im not doing this for work just school
Pobiega
Pobiega14mo ago
How does that change anything? You still need to learn it, at least partially
Aelia
AeliaOP14mo ago
Well now I did i understood what I did wrong
Pobiega
Pobiega14mo ago
Good
Want results from more Discord servers?
Add your server