C
C#3y ago
nks1fu

✅ Classes and for loop

74 Replies
Kouhai
Kouhai3y ago
What's the question?
nks1fu
nks1fuOP3y ago
oh i deleted it on accident when i run it should save the names in a list and the type and later on print it out but when i run it it doesnt ask about the cat and when it should print out list and types it only prints out dog the number of times the inputs were about cats and dogs
Kouhai
Kouhai3y ago
Okay there are couple of bugs here
nks1fu
nks1fuOP3y ago
Kouhai
Kouhai3y ago
Can you send the code as text though instead of screen shots, it would be easier to explain $code
MODiX
MODiX3y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
nks1fu
nks1fuOP3y ago
namespace ConsoleApp
{
class Program
{
static internal void Main()
{
List<Dog> dogs = new List<Dog>();
List<Cat> cats = new List<Cat>();

Console.WriteLine("Enter how many dogs: ");
Dog dog1 = new Dog();
dog1.Count_Dog = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter how many cats: ");
Cat cat1 = new Cat();
cat1.Count_Cat = Convert.ToInt32(Console.ReadLine());


if (dogs.Count > 20)
{
Console.WriteLine("Max is 20");
}

if (cats.Count > 20)
{
Console.WriteLine("Max is 20");
}


else


for (int i = 0; i < dog1.Count_Dog; i++)
{
Console.WriteLine("Name this dog");

dog1.Name = Console.ReadLine();

dogs.Add(dog1);
Console.WriteLine($"What type of dog is {dog1.Name}");

dog1.type = Console.ReadLine();

dogs.Add(dog1);
}


for (int i = 0; i < cats.Count; i++)
{
Console.WriteLine("Name this cat");

cat1.Name1 = Console.ReadLine();

cats.Add(cat1);


Console.WriteLine($"What type of dog is {cat1.Name1}");

cat1.Type1 = Console.ReadLine();

cats.Add(cat1);
}

for (int i = 0; i < dogs.Count; i++)
Console.WriteLine(dogs[i]);

for (int i = 0; i < cats.Count; i++)
Console.WriteLine(cats[i]);
}
}

}
namespace ConsoleApp
{
class Program
{
static internal void Main()
{
List<Dog> dogs = new List<Dog>();
List<Cat> cats = new List<Cat>();

Console.WriteLine("Enter how many dogs: ");
Dog dog1 = new Dog();
dog1.Count_Dog = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter how many cats: ");
Cat cat1 = new Cat();
cat1.Count_Cat = Convert.ToInt32(Console.ReadLine());


if (dogs.Count > 20)
{
Console.WriteLine("Max is 20");
}

if (cats.Count > 20)
{
Console.WriteLine("Max is 20");
}


else


for (int i = 0; i < dog1.Count_Dog; i++)
{
Console.WriteLine("Name this dog");

dog1.Name = Console.ReadLine();

dogs.Add(dog1);
Console.WriteLine($"What type of dog is {dog1.Name}");

dog1.type = Console.ReadLine();

dogs.Add(dog1);
}


for (int i = 0; i < cats.Count; i++)
{
Console.WriteLine("Name this cat");

cat1.Name1 = Console.ReadLine();

cats.Add(cat1);


Console.WriteLine($"What type of dog is {cat1.Name1}");

cat1.Type1 = Console.ReadLine();

cats.Add(cat1);
}

for (int i = 0; i < dogs.Count; i++)
Console.WriteLine(dogs[i]);

for (int i = 0; i < cats.Count; i++)
Console.WriteLine(cats[i]);
}
}

}
public class Dog
{
public string Name { get; set; }

public string type { get; set; }

public int Count_Dog { get; set; }

}
public class Cat
{
public string Name1 { get; set; }

public string Type1 { get; set; }

public int Count_Cat { get; set; }
}
public class Dog
{
public string Name { get; set; }

public string type { get; set; }

public int Count_Dog { get; set; }

}
public class Cat
{
public string Name1 { get; set; }

public string Type1 { get; set; }

public int Count_Cat { get; set; }
}
Kouhai
Kouhai3y ago
First this here
if (dogs.Count > 20)
{
Console.WriteLine("Max is 20");
}

if (cats.Count > 20)
{
Console.WriteLine("Max is 20");
}


else
if (dogs.Count > 20)
{
Console.WriteLine("Max is 20");
}

if (cats.Count > 20)
{
Console.WriteLine("Max is 20");
}


else
This checks how many items are already in the list, it doesn't check the number you provided in the prompt
nks1fu
nks1fuOP3y ago
ooooh
Kouhai
Kouhai3y ago
Second the else is for the second if meaning it doesn't care whether dogs.count is > 20 or is -1
nks1fu
nks1fuOP3y ago
dog1.Count_Dog
Kouhai
Kouhai3y ago
Third what even is Count_dog and Count_cat?
nks1fu
nks1fuOP3y ago
number of dogs they inputed
Kouhai
Kouhai3y ago
Why not just do
int dogCount;
int catCount;
do
{
Console.WriteLine("Enter how many dogs: ");
}while(!int.TryParse(Console.ReadLine(), out dogCount);

do
{
Console.WriteLine("Enter how many catss: ");
}while(!int.TryParse(Console.ReadLine(), out catCount);
int dogCount;
int catCount;
do
{
Console.WriteLine("Enter how many dogs: ");
}while(!int.TryParse(Console.ReadLine(), out dogCount);

do
{
Console.WriteLine("Enter how many catss: ");
}while(!int.TryParse(Console.ReadLine(), out catCount);
You shouldn't create a new instance of Dog and Cat to hold the number That doesn't make sense, right?
nks1fu
nks1fuOP3y ago
yea TryParse never heard of that
Kouhai
Kouhai3y ago
TryParse returns a bool, true if success otherwise false, it outs parsed value
nks1fu
nks1fuOP3y ago
List<Dog> dogs = new List<Dog>();
List<Cat> cats = new List<Cat>();

Console.WriteLine("Enter how many dogs: ");
Dog dog1 = new Dog();
dog1.Count_Dog = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter how many cats: ");
Cat cat1 = new Cat();
cat1.Count_Cat = Convert.ToInt32(Console.ReadLine());
List<Dog> dogs = new List<Dog>();
List<Cat> cats = new List<Cat>();

Console.WriteLine("Enter how many dogs: ");
Dog dog1 = new Dog();
dog1.Count_Dog = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter how many cats: ");
Cat cat1 = new Cat();
cat1.Count_Cat = Convert.ToInt32(Console.ReadLine());
so i dont need this? i just need list
Kouhai
Kouhai3y ago
You need to get the user's input But you don't need to use a property declared in an instance of Dog and Cat to hold the counts
nks1fu
nks1fuOP3y ago
i need to get only type now
Kouhai
Kouhai3y ago
Also
else
else
an if or if else or else Without curly braces means only the next statement is under it's scope Which means, it only cares about this part
for (int i = 0; i < dog1.Count_Dog; i++)
{
Console.WriteLine("Name this dog");

dog1.Name = Console.ReadLine();

dogs.Add(dog1);
Console.WriteLine($"What type of dog is {dog1.Name}");

dog1.type = Console.ReadLine();

dogs.Add(dog1);
}
for (int i = 0; i < dog1.Count_Dog; i++)
{
Console.WriteLine("Name this dog");

dog1.Name = Console.ReadLine();

dogs.Add(dog1);
Console.WriteLine($"What type of dog is {dog1.Name}");

dog1.type = Console.ReadLine();

dogs.Add(dog1);
}
type of what?
nks1fu
nks1fuOP3y ago
the type of the animal
Kouhai
Kouhai3y ago
Lastly
for (int i = 0; i < cats.Count; i++)
{
Console.WriteLine("Name this cat");

cat1.Name1 = Console.ReadLine();

cats.Add(cat1);


Console.WriteLine($"What type of dog is {cat1.Name1}");

cat1.Type1 = Console.ReadLine();

cats.Add(cat1);
}
for (int i = 0; i < cats.Count; i++)
{
Console.WriteLine("Name this cat");

cat1.Name1 = Console.ReadLine();

cats.Add(cat1);


Console.WriteLine($"What type of dog is {cat1.Name1}");

cat1.Type1 = Console.ReadLine();

cats.Add(cat1);
}
Iterates until i is larger than or equals cats.Count but cats.Count is obv 0 because nothing was added to it, and if something was added to cats list, you'll run into an infinite loop, because with each addition, cats.Count increases aswell
nks1fu
nks1fuOP3y ago
and save it in a list so it prints out {name}, {dog type}
Kouhai
Kouhai3y ago
Okay, then 1 - Get count of dogs that the user wants to add 2 - Iterate to fill the list with the dogs info 3 - In the for loop, prompt the user for dog's name and type 4 - Iterate over the list and print out the info
nks1fu
nks1fuOP3y ago
and one more question using . TryParse how can i get dog Type information
Kouhai
Kouhai3y ago
TryParse pattern is used to parse something, is dog type an enum, is it a number?
nks1fu
nks1fuOP3y ago
Oh so i just ReadLine normally
Kouhai
Kouhai3y ago
Well, it depends, do you trust the user to really provide a type? What if the user simply pressed enter without entering the dog type?
nks1fu
nks1fuOP3y ago
i mean i dont have to check that ig i just need to fix parts where i used dog1 and cat1 to use readline do i do inside the do brackets or after while loop
Kouhai
Kouhai3y ago
What do you mean?
nks1fu
nks1fuOP3y ago
nks1fu
nks1fuOP3y ago
nvm i did it
Kouhai
Kouhai3y ago
Yeah, I missed a parenthesis
nks1fu
nks1fuOP3y ago
and the part i made the mistake asking name and type of the dog for (int i = 0; i < ?; i++)
Kouhai
Kouhai3y ago
Well what do think should go here?
nks1fu
nks1fuOP3y ago
number of the dogs
Kouhai
Kouhai3y ago
ThumbsUp
nks1fu
nks1fuOP3y ago
for (int i = 0; i < dogs.Count; i++) ;
Kouhai
Kouhai3y ago
What do you think would be the value of dogs.Count
nks1fu
nks1fuOP3y ago
it should be dogCount
Kouhai
Kouhai3y ago
You mean dogs.Count equals dogCount ?
nks1fu
nks1fuOP3y ago
no like we put the number of dogs in dogCount and the dogs.Count would be 0
Kouhai
Kouhai3y ago
Right
nks1fu
nks1fuOP3y ago
and than i readline and add it in a list
Kouhai
Kouhai3y ago
Spot ThumbsUp
nks1fu
nks1fuOP3y ago
for (int i = 0; i < dogCount; i++) ;
Console.WriteLine("name your dog") ;
string Name1 = Console.ReadLine();
for (int i = 0; i < dogCount; i++) ;
Console.WriteLine("name your dog") ;
string Name1 = Console.ReadLine();
i feel smart but i am not
Kouhai
Kouhai3y ago
Well, you understood the problem and started to fix it, don't be hard on yourself!
nks1fu
nks1fuOP3y ago
and adding in a list dogs.Add(Name1);
dogs.Add(Name1);
dogs.Add(Name1);
Kouhai
Kouhai3y ago
What's the type of dogs variable?
nks1fu
nks1fuOP3y ago
Kouhai
Kouhai3y ago
Then what items can it hold?
nks1fu
nks1fuOP3y ago
string
Kouhai
Kouhai3y ago
Do you know generics?
nks1fu
nks1fuOP3y ago
using System.Collections.Generic; that?
Kouhai
Kouhai3y ago
No, the concept of generics in c#
nks1fu
nks1fuOP3y ago
oh hm i dont think so
Kouhai
Kouhai3y ago
Alright, give a read to this https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/generics and to $helloworld as well But a TLDR for this problem List<int> means a List can hold items of type int, List<bool> means it holds bool
Generic classes and methods
Learn about generics. Generic types maximize code reuse, type safety, and performance, and are commonly used to create collection classes.
nks1fu
nks1fuOP3y ago
oooh ik that i didnt know what ment meant still ty imma read it and the main question when i add it in a list i want it to print out {name} {type}, {name} {type} do i need a seperate list
Kouhai
Kouhai3y ago
No you don't need a seperate list
nks1fu
nks1fuOP3y ago
and print it out with a loop and adding [i+1] to the elements oh ngl i always forget things in c# bc in python everything was easy
Kouhai
Kouhai3y ago
Yeah C# can be confusing coming from a python background They have very different ways of doing InaNod
nks1fu
nks1fuOP3y ago
question reading the input do i need to do var name1 or string
Kouhai
Kouhai3y ago
var just implicitly declares the variable without having you explicitly typing the type of the variable name1 is still string if it's assigned a string variable
nks1fu
nks1fuOP3y ago
oh yea ty
Kouhai
Kouhai3y ago
I would change the variable name though name1 doesn't sound good imo
nks1fu
nks1fuOP3y ago
Name >>> and asking the type do i do inside or outside the for loop bc last time i did it outside it didnt work
Kouhai
Kouhai3y ago
What do you mean?
nks1fu
nks1fuOP3y ago
for (int i = 0; i < dogCount; i++) ;
Console.WriteLine("name your dog") ;
string Name = Console.ReadLine();
dogs.Add(Name);
for (int i = 0; i < dogCount; i++) ;
//get dog type

for (int i = 0; i < dogCount; i++) ;
Console.WriteLine("name your dog") ;
string Name = Console.ReadLine();
dogs.Add(Name);
for (int i = 0; i < dogCount; i++) ;
//get dog type

Kouhai
Kouhai3y ago
Okay okay, that's wrong for (int i = 0; i < dogCount; i++) ; is an empty for loop
nks1fu
nks1fuOP3y ago
for (int i = 0; i < dogCount; i++) ;
Console.WriteLine("name your dog") ;
string Name = Console.ReadLine();
dogs.Add(Name);
for (int i = 0; i < dogCount; i++) ;
Console.WriteLine("dog type");
string Type = Console.ReadLine();
dogs.Add(Type);

for (int i = 0; i < dogCount; i++) ;
Console.WriteLine("name your dog") ;
string Name = Console.ReadLine();
dogs.Add(Name);
for (int i = 0; i < dogCount; i++) ;
Console.WriteLine("dog type");
string Type = Console.ReadLine();
dogs.Add(Type);

Kouhai
Kouhai3y ago
Let's take it a step by step Can you explain what each line does?
nks1fu
nks1fuOP3y ago
first loop which goes through until the inputted number write name your dog x times reads it adds in a list as name second loop goes through until the inputted number gets input reades it as string type adds to list as Type
Kouhai
Kouhai3y ago
Okay, do you know about scopes?

Did you find this page helpful?