Click here to Skip to main content
15,877,296 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
The question is straightforward but the only thing I cannot conquer is getting the elf's calorie data as input from the user. I have to read for a new line and whenever I get a new line I have to update my sum to the maximum calorie/sum an elf can have, and then reassign the sum to 0. Do the same for other elves' calories as well.

(Please read the problem statement from:[^])

The below code is what I have tried but it's entirely wrong!
Need your help!

What I have tried:

#include <iostream>
#include <vector>

using namespace std;

int ans = -1;
int main() {
    int n;
    int t;
    int sum = 0;
    while(t--) {
        if(cin.get() == '\n') {
            if(cin.get() == '\n') {
                if(ans < sum) ans = sum;
                sum = 0;
        sum += n;
    return 0;
Updated 5-Dec-22 15:40pm
Graeme_Grant 5-Dec-22 7:29am    
Day 1 is usually the warm-up question. If you can't solve this one by yourself, then you are going to struggle every day.

HINT: Look at the calorie list to be entered. The hint on how is there in plain sight.
Harsh 5 5-Dec-22 7:33am    
The question is straightforward, it's just the input of the problem which is bothering me. I have already solved today's problem in a few minutes.
Graeme_Grant 5-Dec-22 15:18pm    
It does not change my comment. These are to challenge yourself. If you are asking for help here, or somewhere else, then I would recommend not doing them.
Harsh 5 5-Dec-22 21:47pm    
I think it's fine to seek help if you are stuck somewhere for a very long time. Instead of wasting hours and days finding the solution yourself, it is a good idea to get a little help. It will save time to do more work.
Also, I am not asking about the full solution, I am just asking about how can I get the input. The question is very basic and anyone with a basic understanding of loops and conditional statements can easily do it, it's just this input thing where I was stuck. And now It is already solved!
I have even submitted my own solution below.
Graeme_Grant 5-Dec-22 23:18pm    
If you can not work out these Advent quizzes, don't do them, they are meant to challenge you to work it out, not get others to do it for you.

We had a Coding Challenge here on Code Project a couple of years ago. Should those who were stuck doing the challenges ask for answers here? LMAO!

Programming is problem-solving. A good programmer learns to use his/her tools to debug. Research is a problem-solving technique like Googling to look for ideas on how to solve problems is one such technique. But giving up and asking others to solve quizzes like this one is lazy IMHO.

The below code is what I have tried but it's entirely wrong!
Yes. It is. And you don't know why.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
Share this answer
Harsh 5 5-Dec-22 21:35pm    
Thank you very much!
Saving the calories in a vector would be a good idea.
std::vector<int> numbers;

Then it would be practical to write the calories as described in a file calories.txt and read this file line by line. At each blank line write the sum into the vector and continue reading until the end of the file is reached.
std::ifstream file;"calories.txt");
while (file.peek() != EOF) {
   getline(file, line);

   // TODO: convert to number and sum up



Finally, output everything and you're done.
for(size_t i=1; i <= numbers.size(); i++)
     std::cout << "Elf No: " << i << " carrying " << numbers[i-1] << " calories\n";
Share this answer
Harsh 5 5-Dec-22 21:38pm    
Thank you for the approach, I was not thinking about reading data from a file. In fact, I was thinking about how can I read input as test cases directly from the console.
But I have already found a solution which does not require this file parsing.
Graeme_Grant 5-Dec-22 23:21pm    
You do realize that for the next 23 or 24 Advent quizzes, he will just keep coming back asking you to do his work for him. These are coding challenges, just like school assignments/homework, not hobby or commercial projects.
Harsh 5 5-Dec-22 23:43pm    
Stop being a social worker, you are not my "babysitter". If I am stuck on a single problem for a very long time I will ask it to grow fast and it is my choice. It is not my priority to spend a lot of time on a single problem. Of course, I will spend days and months on a project which is worth it. But not on a problem like this. I am a web developer and an intern at GeeksforGeeks, I know my priorities.
I have a question I will ask it loud, It is non of your business. So, mind your own business!
If you don't wanna help, STOP SPREADING HATE! PLEASE!!!
Graeme_Grant 6-Dec-22 1:39am    
You need to tone down your language. I called you out for help with a coding challenge, it's like asking for help with homework, an assignment, or an exam. No need to go on a rant and name-calling.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900