C#C
C#4y ago
HubTaken

✅ I don't know why this doesn't work.

I am doing the first task of advent of code in C# and I have problem with my code. I am not sure how to solve it and I been at it for about over an hour. my code doesn't set a variable value to 0 when I want it and I don't understand why.
the input file is from first task on advent of code.


string[] lines = File.ReadAllLines("C:\\Users\\huber\\Desktop\\AdventOfCode2022\\task1\\task1\\input.txt");

int highestCalories = 0;
int currentCalories =0;

foreach (string line in lines)
{
    string linetext ="";
    int number = 0;
    

    linetext = line.ToString().Trim();
    bool success = int.TryParse(linetext, out number);
    if (success)
    {
        currentCalories += currentCalories + number;
        Console.WriteLine(number);
    }
    else
    {
        Console.WriteLine("space");
        currentCalories = 0;
        // I think this is the error. current calories do not get set to 0.    
    }
        
   
    if (currentCalories > highestCalories)
    {
        highestCalories = currentCalories;
    }
   

}

Console.WriteLine("Highest Calories {0}", highestCalories);
Console.ReadLine();
Was this page helpful?