C
C#12mo 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
SinFluxx12mo ago
what's the problem you're having?
Cheetah
Cheetah12mo 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
SinFluxx12mo ago
what do you mean by "dont work" you're receiving an error?
Cheetah
Cheetah12mo 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
MODiX12mo ago
Sorry @cheetahsflame your message contained blocked content and has been removed!
Cheetah
Cheetah12mo ago
wait i might be dumb hold on do i use IntGuess or Guess as int variable here
Pobiega
Pobiega12mo ago
Guess is declared as a string and can thus only ever hold strings IntGuess is declared as an int.
Cheetah
Cheetah12mo 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
Pobiega12mo ago
how about debugging it?
Cheetah
Cheetah12mo ago
how
Pobiega
Pobiega12mo 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
Cheetah12mo ago
i did now ok that fixed it? for some reason
Pobiega
Pobiega12mo 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
Cheetah12mo ago
oh ok
Pobiega
Pobiega12mo 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
Cheetah12mo ago
ok thank you
Pobiega
Pobiega12mo ago
however, you really should learn how to $debug
MODiX
MODiX12mo 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
Pobiega12mo ago
assuming you are using visual studio, this guide should teach you the basics
Cheetah
Cheetah12mo ago
ok ill read that some time
Pobiega
Pobiega12mo 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
Cheetah12mo ago
oh ok so with debug idont need to print it in my program it does it for me
SinFluxx
SinFluxx12mo ago
yeah, instead of Console.WriteLine(num); you can just debug to look and see what the value of num is
Cheetah
Cheetah12mo ago
ok
Want results from more Discord servers?
Add your server
More Posts
✅ MSSQL on Mac helpHi guys, I have an upcoming course at my university that requires me to install SQL Server 2022 and ❔ new to programming and can't figure outi want to create an appointment scheduler that let's the user create view update and delete but can'❔ ✅ (SOLVED) TcpListener.AcceptTcpClientAsync does not get cancelled```csharp CancellationTokenSource cancel = new(400); TcpClient JoinedClient = await Server.AcceptTc✅ Tracking changes on a complex object provided by a third party libraryHi! I have a third party .Net Framework dll that allows me to load a file. This file is potentially ❔ ✅ How I can fix download and unzipI wrote a code to download the archive and unpack it but it just creates empty files instead of deco❔ Error message: Cannot convert query to sqlhi i have this code, on line 119 is whats causing the error saying the query cannot be converted to Make a method acessable by scripts extending it, but not by outside scriptsI have private which limits a method to itself, and I have public which lets anything access the metOk so i DID NOT add hello world to my code but it still appearsmy code for some ungodly reason has decided to not use my code and use a previous code when i run an❔ Problem with Quick SortIt seems that it partially sorts it and doesn't finish. I really can't see what is wrong ``` void Qu"name space 'helloworld2' already contains a definition for a 'program'Man this my first code ever in c# it worked two seconds ago and now it dont what do i do pls heres t