© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
15 replies
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();
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();
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ I don't know why this wont work
C#CC# / help
3y ago
why this doesn't work
C#CC# / help
2y ago
✅ why doesn't this work?
C#CC# / help
3y ago
❔ Why doesn't this button work?
C#CC# / help
3y ago