C
C#16mo ago
Cheetah

How to make string to int

pls help me i needa make a string variable to int variable for my code but i cant figure out how heres my code
using System;
using System.Runtime.InteropServices;

namespace Guessing_game
{
internal class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int num = rnd.Next();
Console.WriteLine(num);
Console.WriteLine("Guess the number");
Console.ReadLine();
string Guess = Console.ReadLine();

int IntGuess = int.Parse(Guess);

Console.WriteLine(IntGuess);
if (num == IntGuess)
{
Console.WriteLine("Good job");
}
}
}
}
using System;
using System.Runtime.InteropServices;

namespace Guessing_game
{
internal class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int num = rnd.Next();
Console.WriteLine(num);
Console.WriteLine("Guess the number");
Console.ReadLine();
string Guess = Console.ReadLine();

int IntGuess = int.Parse(Guess);

Console.WriteLine(IntGuess);
if (num == IntGuess)
{
Console.WriteLine("Good job");
}
}
}
}
24 Replies
SinFluxx
SinFluxx16mo ago
what's the problem you're having?
Cheetah
CheetahOP16mo ago
im trying to make what the user types in an interger so i can do like == stuff but it dont work :( and i cant figure out
SinFluxx
SinFluxx16mo ago
what do you mean by "dont work" you're receiving an error?
Cheetah
CheetahOP16mo ago
when i use variable "Guess" it says i cant use == with Int and String and when i use IntGuess as variable and try to print it nothing comes up wait
MODiX
MODiX16mo ago
Sorry @cheetahsflame your message contained blocked content and has been removed!
Cheetah
CheetahOP16mo ago
wait i might be dumb hold on do i use IntGuess or Guess as int variable here
Pobiega
Pobiega16mo ago
Guess is declared as a string and can thus only ever hold strings IntGuess is declared as an int.
Cheetah
CheetahOP16mo ago
yea ok good thats what i thought but here its suppose to say a number and if i write the number it says good job but nothing happens so im wondering if IntGuess thing isnt working
Pobiega
Pobiega16mo ago
how about debugging it?
Cheetah
CheetahOP16mo ago
how
Pobiega
Pobiega16mo ago
well actually before that, I see a bug
Console.ReadLine();
string Guess = Console.ReadLine();
Console.ReadLine();
string Guess = Console.ReadLine();
you are doing readline twice that seems bad, so remove the first one
Cheetah
CheetahOP16mo ago
i did now ok that fixed it? for some reason
Pobiega
Pobiega16mo ago
well yes so what was happening is that you were entering a value that was ignored, then it was waiting for a second value and you didnt know
Cheetah
CheetahOP16mo ago
oh ok
Pobiega
Pobiega16mo ago
ReadLine() halts your application waiting for a string to be given, and only "resumes" after it sees an enter press since this was happening twice, your program was actually frozen
Cheetah
CheetahOP16mo ago
ok thank you
Pobiega
Pobiega16mo ago
however, you really should learn how to $debug
MODiX
MODiX16mo ago
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
Pobiega
Pobiega16mo ago
assuming you are using visual studio, this guide should teach you the basics
Cheetah
CheetahOP16mo ago
ok ill read that some time
Pobiega
Pobiega16mo ago
good its a way to see whats going on and what values your variables have etc while the program is running. you can also follow the execution line by line its extremely handy
Cheetah
CheetahOP16mo ago
oh ok so with debug idont need to print it in my program it does it for me
SinFluxx
SinFluxx16mo ago
yeah, instead of Console.WriteLine(num); you can just debug to look and see what the value of num is
Cheetah
CheetahOP16mo ago
ok
Want results from more Discord servers?
Add your server