C
C#•2mo ago
'brella boy

errors that i cant figure out for the life of me

yeah
No description
74 Replies
'brella boy
'brella boy•2mo ago
code: and errors
'brella boy
'brella boy•2mo ago
No description
Jimmacle
Jimmacle•2mo ago
start with the first one TryParse needs at least 2 arguments, not 1
Evyr
Evyr•2mo ago
TryParse takes in a string and an out int variable so it'd be if (int.TryParse(inputString, out int parsedNumber) != true) { } you've also got a static local function referencing variables from the method it's nested within, so it can't be static. and you're missing a closing curly brace for the over(string[] args) method numovers doesn't even exist in the scope of the over() method you're also using Console.ReadLine() to try and print text to the console, use Console.WriteLine() and then get the input with a Console.ReadLine() afterwards and you'll need to parse that input string as an integer before you can assign it to an integer variable and remove the semicolons at the end of your if statements
'brella boy
'brella boy•2mo ago
ah i see sorry for taking a while to respond wait i dont have any output though? i just want to check if its an integer what am i supposed to use as output
Evyr
Evyr•2mo ago
use a discard then
'brella boy
'brella boy•2mo ago
...idk what that is but ill check the documentation
Evyr
Evyr•2mo ago
like this
No description
'brella boy
'brella boy•2mo ago
alright ?
'brella boy
'brella boy•2mo ago
No description
Evyr
Evyr•2mo ago
the first argument should be a string
'brella boy
'brella boy•2mo ago
oh right
Evyr
Evyr•2mo ago
int.Parse and int.TryParse try to parse strings as integers
'brella boy
'brella boy•2mo ago
is there any way to just check if any value integer or string oh wait no thatd be impossible ok is there a way to check if anything other than a natural number is entered so any integer greater than 0 no letters, no decimal numbers, no negative numbers
Evyr
Evyr•2mo ago
you could use an unsigned integer uint as a parameter because they can't be negative, but casting a negative integer to them will just cause some unexpected behaviour if it was negative or you could do if (myInteger < 1)
Jimmacle
Jimmacle•2mo ago
using unsigned types to enforce ranges isn't a great idea, it's better to do an actual range check so you check if it's an integer first, then check if that integer is greater than 0
'brella boy
'brella boy•2mo ago
ya but then that doesnt check for like characters i can check if the integer is greater than 0 thats fine
Jimmacle
Jimmacle•2mo ago
e.g.
if (int.TryParse(myString, out var myInt) && myInt > 0)
if (int.TryParse(myString, out var myInt) && myInt > 0)
'brella boy
'brella boy•2mo ago
i was just wondering if there was something that just auto checks if its anything OTHER than a natural number ill try this var just automatically changes the variable to whatever makes the most sense right?
Jimmacle
Jimmacle•2mo ago
yeah, it's exactly the same as if you put int there instead it's just obvious that it will be an int already so no need to repeat it
'brella boy
'brella boy•2mo ago
so should i imput numovers
No description
'brella boy
'brella boy•2mo ago
as a string ykw itd be easier if i showed you what im trying to do
'brella boy
'brella boy•2mo ago
No description
Jimmacle
Jimmacle•2mo ago
if your method receives an int then you already know it's an integer there's nothing to parse
'brella boy
'brella boy•2mo ago
No description
'brella boy
'brella boy•2mo ago
so... this?
Jimmacle
Jimmacle•2mo ago
that would be good if you're supposed to get a string, yes (the logic is inverted though) the text you shared doesn't seem to specify what type numovers should be
'brella boy
'brella boy•2mo ago
i just need a numerical value that i can work with later on
'brella boy
'brella boy•2mo ago
for this
No description
'brella boy
'brella boy•2mo ago
yeah its honestly pretty poor 😭 they expect us to write a cricket program but nobody knows how cricket works and im here trying to decipher my code AND the rules of cricket but whatever
Jimmacle
Jimmacle•2mo ago
so if it was me, i would separate the user input part and the actual calculation part so have some other code that reads the input and converts it to an int, then pass it to this method and do all the actual number stuff
'brella boy
'brella boy•2mo ago
but thats not the issue they kinda expect me to have input validation in the program unless im just not understanding what youre saying
'brella boy
'brella boy•2mo ago
well nvm that
No description
'brella boy
'brella boy•2mo ago
i fixed some of the code but i cant seem to run my subroutine
'brella boy
'brella boy•2mo ago
actually ig this is better??
No description
friedice
friedice•2mo ago
brackets are all over the place. recommend formating and lining up first. Also I dont think you intend to recursively call innings? what even is string[] args in context of the over function?
'brella boy
'brella boy•2mo ago
i practically rewrote my code hold on no clue. i just have it there because someone told me to one time i barely know how c# works 😭
'brella boy
'brella boy•2mo ago
No description
friedice
friedice•2mo ago
yeah I recommend learning the basics first, learning the syntax etc.
'brella boy
'brella boy•2mo ago
oh i know the syntax im overexaggerating i just like dont know anything other than basic functions like arithmetic, printing, reading input, calculations, string functions, loops etc i can do all of that its just the complicated stuff also if anyone is still around i kinda realise now i need to input multiple teams and display the winner but idk how to even start that
friedice
friedice•2mo ago
i would recommend white boarding out your solution before writing any code expected inputs, logic that needs to be done, and output
'brella boy
'brella boy•2mo ago
i mean if you mean for what i just said yeah i am considering that i need to loop the functions so that i can enter more than one team name and number of overs and i need to store it somewhere so that the highest value can be printed later its just that like idk how to go abt that
friedice
friedice•2mo ago
it's easier to break down the problem into mini problems first, what are your expected inputs?
'brella boy
'brella boy•2mo ago
oops sorry for disappearing ok so team_name and numovers numovers will be used to calculate numruns which loops to get a total oh wait i think i kinda see the issue see
'brella boy
'brella boy•2mo ago
my school expects me to input the team name and the number of overs like this
No description
'brella boy
'brella boy•2mo ago
but thats just the problem if i write it like that I can't loop it over
friedice
friedice•2mo ago
so maybe instead of a for loop, you can have your application running with state machine logic. ie while loop. so that user can running the logic for firing that inning function based on a certain criteria whenever user wants and exit the program whenever as well
'brella boy
'brella boy•2mo ago
hm so if i start like the entire program with a while loop maybe linked to an input and if the user like inputs just as an example H the program will stop and return the largest score value?
friedice
friedice•2mo ago
so on init, have something like Hi user. press following button to do stuff. 1. Do Foo 2. Exit and 1 will direct them to input whatever they need "team name", "number" and will output that number, then direct them back into the main while loop
'brella boy
'brella boy•2mo ago
ok that works small issue though how do i like hold the score value if that makes sense like lets say they do the inputs program runs and returns like 20 how do i take that 20 store it and when the program stops running i compare all the input values and print it out
friedice
friedice•2mo ago
the easiest way is to put it in memory during application runtime
'brella boy
'brella boy•2mo ago
how would i go about that
friedice
friedice•2mo ago
a way to do this would be a static integer, something like
static int num = 0;

void Foo()
{
num += 10;
Console.WriteLine("Score: " + num);
}
static int num = 0;

void Foo()
{
num += 10;
Console.WriteLine("Score: " + num);
}
'brella boy
'brella boy•2mo ago
alright ill try and implement this
'brella boy
'brella boy•2mo ago
so like this???
No description
'brella boy
'brella boy•2mo ago
but this would just add 10 each time i need it to like make a new variable??? no to store a number compare a newly inputted number and output it??? idk man ill send my entire code so u can see where its at atm
'brella boy
'brella boy•2mo ago
No description
'brella boy
'brella boy•2mo ago
No description
'brella boy
'brella boy•2mo ago
i probably shouldnt put the static integer there
friedice
friedice•2mo ago
lets work on getting the application flow working first, being able to run the project helps. Personally, i would recommend creating a new console project and getting the while loop going
'brella boy
'brella boy•2mo ago
mk ill just try an output a single score for now
'brella boy
'brella boy•2mo ago
one small issue
No description
friedice
friedice•2mo ago
well the flow is all messed up in the first place, which is why i recommended creating a new projet
'brella boy
'brella boy•2mo ago
oh.. its getting late for me though so do yk how i can fix it? ill just fix that error go to sleep and get back to it later or just hand it in incomplete because disregarding the programming the task was explained vry badly by my teacher
friedice
friedice•2mo ago
you can put keyword "static" right before int over() but your program might infinitely recurse and crash due to stack overflow
'brella boy
'brella boy•2mo ago
oh...
'brella boy
'brella boy•2mo ago
No description
'brella boy
'brella boy•2mo ago
if i do that i get these fun errors
friedice
friedice•2mo ago
yeah there isnt really an easy fix, just rewrite imo
'brella boy
'brella boy•2mo ago
ah mk
friedice
friedice•2mo ago
This would be the skeleton for your application flow, nothing too complicated.
using System;

class Program
{
static void Main(string[] args)
{
bool running = true;
while (running)
{
Console.WriteLine("Enter 1 to call Function1, or 2 to exit:");
string input = Console.ReadLine();

switch (input)
{
case "1":
Function1();
break;
case "2":
running = false;
Console.WriteLine("Exiting the program...");
break;
default:
Console.WriteLine("Invalid input.");
break;
}
}
}

static void Function1()
{
Console.WriteLine("Function1 is called!");
}

}
using System;

class Program
{
static void Main(string[] args)
{
bool running = true;
while (running)
{
Console.WriteLine("Enter 1 to call Function1, or 2 to exit:");
string input = Console.ReadLine();

switch (input)
{
case "1":
Function1();
break;
case "2":
running = false;
Console.WriteLine("Exiting the program...");
break;
default:
Console.WriteLine("Invalid input.");
break;
}
}
}

static void Function1()
{
Console.WriteLine("Function1 is called!");
}

}
FusedQyou
FusedQyou•2mo ago
This conversation is very long so I'm just going to comment on what I see Don't use Convert. Use int.Parse to parse a number. Even better, use int.TryParse to safely parse the number and check if it's a number to begin with. Your initial post tried to use it so I don't see why you switched. Convert is legacy and even if you're definitely only passing numbers you should check regardless. Your methods are also completely misplaced Main is inside over which should not be the case. You rarely need nested loops, and your main method should definitely not be nested. Unnest it and it can call over Other than that the errors relate to variable snot being defined. Where is it supposed to get numover2 and team_name and such from?
'brella boy
'brella boy•5w ago
oh i was using convert bcs i needed it to be an integer at that point that wasnt to check ah I see
'brella boy
'brella boy•5w ago
No description
Want results from more Discord servers?
Add your server