C
C#2y ago
krazycay

❔ how can i re use the readline after using it in a if statement

so basically im using readline and if it == "hello" for example, it'd writeline "hi!", then the program would exit. i'd like it to go back to readline after this. how can i do this?
// See https://aka.ms/new-console-template for more information
using ConsoleApp1;
using System.ComponentModel.Design;

namespace ConsoleApp1 // Note: actual namespace depends on the project name.
{
internal class Program
{
static void Main(string[] args)
{
Console.Title = Settings.WinName;
Console.SetWindowSize(Settings.WinSize1, Settings.WinSize2);
Console.BufferHeight = Settings.BufHeight;
Console.BufferWidth = 97;
Console.WriteLine(Settings.Logo);
Console.Write(":> ");
if (Input.userResponse == "hi")
{
Console.WriteLine("no");
}
else if (Input.userResponse == "bad")
{
Console.WriteLine("trash");
Input.userResponse;
}
}
}
}
// See https://aka.ms/new-console-template for more information
using ConsoleApp1;
using System.ComponentModel.Design;

namespace ConsoleApp1 // Note: actual namespace depends on the project name.
{
internal class Program
{
static void Main(string[] args)
{
Console.Title = Settings.WinName;
Console.SetWindowSize(Settings.WinSize1, Settings.WinSize2);
Console.BufferHeight = Settings.BufHeight;
Console.BufferWidth = 97;
Console.WriteLine(Settings.Logo);
Console.Write(":> ");
if (Input.userResponse == "hi")
{
Console.WriteLine("no");
}
else if (Input.userResponse == "bad")
{
Console.WriteLine("trash");
Input.userResponse;
}
}
}
}
36 Replies
krazycay
krazycayOP2y ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// All of the settings can be changed from here

namespace ConsoleApp1
{
public class Input
{
public static string userResponse = Console.ReadLine();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// All of the settings can be changed from here

namespace ConsoleApp1
{
public class Input
{
public static string userResponse = Console.ReadLine();
}
}
no idea how i can do this
Shinigami
Shinigami2y ago
You can keep the readline in a while loop?
krazycay
krazycayOP2y ago
Alright lol how do i do that or wait shit well that just makes it write no constantly
Jimmacle
Jimmacle2y ago
public class Input
{
public static string userResponse = Console.ReadLine();
}
public class Input
{
public static string userResponse = Console.ReadLine();
}
this is very much incorrect
Shinigami
Shinigami2y ago
You can keep the readline in while loop and keep an if statement for exit, if user writes exit you'll exit out of the loop
Jimmacle
Jimmacle2y ago
you should be using Console.ReadLine() directly and storing it in a local variable
krazycay
krazycayOP2y ago
wdym? then how will i see what the readline is unless it's in a variable like that ah well
Jimmacle
Jimmacle2y ago
local variable
krazycay
krazycayOP2y ago
lmao wdym a local variable
Jimmacle
Jimmacle2y ago
a variable declared in your method
krazycay
krazycayOP2y ago
what i just started c# lmfao like 10 min ago dont know what im doing
Jimmacle
Jimmacle2y ago
void MyMethod()
{
string userResponse = Console.ReadLine();
// do other stuff
}
void MyMethod()
{
string userResponse = Console.ReadLine();
// do other stuff
}
may i suggest $helloworld
krazycay
krazycayOP2y ago
alr so
krazycay
krazycayOP2y ago
that breaks the loop and not the if / else statements i want it to go back to the if userresponse == lol
Jimmacle
Jimmacle2y ago
you don't need to break out of if/else statements just remove the breaks
krazycay
krazycayOP2y ago
then it loops saying bad boy or trash or no and doesnt go back to user resonse lol
Jimmacle
Jimmacle2y ago
then the code you're running isn't the code you're showing here every loop that should read a line, then print something out based on the input
krazycay
krazycayOP2y ago
like look
krazycay
krazycayOP2y ago
krazycay
krazycayOP2y ago
ah fixed it
Jimmacle
Jimmacle2y ago
you're also going to want to modify the variable used for your while loop condition inside the loop otherwise it'll loop forever
krazycay
krazycayOP2y ago
thats the point :>
Jimmacle
Jimmacle2y ago
to loop forever?
krazycay
krazycayOP2y ago
yes
Jimmacle
Jimmacle2y ago
if so you can use while (true)
krazycay
krazycayOP2y ago
mm k yo @fixed(void* x = &Jimmacle) how can i make multiple while loops run at the same time
Jimmacle
Jimmacle2y ago
what are you actually trying to do? the answer to that exact question is complicated for a beginner
krazycay
krazycayOP2y ago
ive done what i want now i just want to figure out how to run another while true loop while the one i made before is running
Jimmacle
Jimmacle2y ago
it's easier to help if i actually know the end goal
krazycay
krazycayOP2y ago
Console.Title = Settings.WinName;
Console.SetWindowSize(Settings.WinSize1, Settings.WinSize2);
Console.BufferHeight = Settings.BufHeight;
Console.BufferWidth = 97;
Console.Title = Settings.WinName;
Console.SetWindowSize(Settings.WinSize1, Settings.WinSize2);
Console.BufferHeight = Settings.BufHeight;
Console.BufferWidth = 97;
constantly do that how do i do the goal i wanted to do lol just tell me that
Jimmacle
Jimmacle2y ago
the short answer is multithreading but that doesn't seem like something you should do constantly in a loop i guess you could also use a timer
krazycay
krazycayOP2y ago
it's gonna lock the terminal size basically what i want to do constantly resize it, anyway @fixed(void* x = &Jimmacle)
krazycay
krazycayOP2y ago
krazycay
krazycayOP2y ago
why doesnt this work lol figured it out
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server