none
Specify number of lines to input in console RRS feed

  • Question

  • Here's an example of the input i want:

    Input:
    2
    1 2 3 4
    3 2 5 1

    Output: xxx has the biggest sum. 

    The two lines of input

    1 2 3 4

    and

    3 2 5 1

    represent a person each, so i input 4 numbers for each person, and i want to output which person has the biggest sum(sum of the numbers input). In this example the second person has the biggest sum. The number "2" in the first line of input represents how many lines of imput i want to type. So if i type 3 in the beginning i want to input the sum for 3 different people. 

    I haven't been able to figure it out. I can only input an infinite number of lines, but that's not really what i want. Here's what i got:

    string line;
                while ((line = Console.ReadLine()) != null)
                {
                    string[] split = line.Split(new char[] { ' ' }, StringSplitOptions.None);
                    long a1 = Int64.Parse(split[0]), b1 = Int64.Parse(split[1]), a2 = Int64.Parse(split[2]), b2 = Int64.Parse(split[3]);
    
                    Console.ReadLine();
                }

    What approach could i take to achieve the desired output?

    Sunday, February 11, 2018 9:04 PM

Answers

  • // Next, compute the sum of the numbers, and see if it's bigger than the biggest sum we have so far.

    Not sure what this means, compute the sum of the 4 numbers from the first or second person? And should i do it in a loop? Sorry for my idiotic questions :/



    Hi Anonymmous842,

    You could try to put the four sum numbers in a list and compare it.

    In the code I provided in my previous reply, make a new list to store the sum. And them compare it and output the biggest one.

      if (num[index].Count == 4)
                            {
                                foreach (var nu in num[index].ToList())
                                {
                                    Sum = nu + Sum;
                                }
                                Console.WriteLine("{0}: Sum={1}", n, Sum);
    //you could add the sum to list from here.
                                Sum = 0;
                            }
    

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Anonymmous842 Friday, February 16, 2018 4:13 PM
    Thursday, February 15, 2018 5:47 PM
    Moderator

All replies

  • Ask for the number of people, then use a "for" loop to get each person.

             // Get the number of people. 
             Console.WriteLine("How many people are there? ");
             int nPeople = Convert.ToInt32(Console.ReadLine());
    
             // Do whatever you have to do for each person.
             for (int i = 0; i < nPeople; i++)
             {
                // Your code for each person goes here.
             }

    Sunday, February 11, 2018 10:33 PM
  • First note that you have ReadLine twice and that won't work.

    So yes you need to first read the first line and convert the number.

    Then you can have an integer initialized to 0. In the while loop's condition check if the integer has a value indicating you are done. I am being vague because it will help you to learn how to figure it out. This is obviously a class assignment. Inside the loop increment the integer. You should also check for pre-mature end-of-file. Note that the processing of the integer is much like what a for loop does. You should try doing it using a while loop then try to do it using a for loop. Ask questions if you need to.



    Sam Hobbs
    SimpleSamples.Info

    Monday, February 12, 2018 1:43 AM
  • Ask for the number of people, then use a "for" loop to get each person.

    It is obviously a class assignment and most of the people that frequently answer questions agree that it is better to help  the student learn how to figure it out. Giving the code without an explanation might be easy for us but it is not the best help for the student. If the instructor is monitoring these sites and the student turns in the same code as is posted here then it might be a problem for the student.


    Sam Hobbs
    SimpleSamples.Info

    Monday, February 12, 2018 1:46 AM
  • I understand what you are saying SS. Which is why I wrote the simplest bare-bones piece of code I could. I did not write the whole assignment, just a couple of lines of code that answered the question about how not to "input an infinite number of lines". And even then, the code I wrote could be improved considerably (such as by checking if the input is really a number).

    I feel this is reasonable. If a student can prove they have had a go (by showing some of their code) but they are stuck on one small aspect, it is okay for us to help them over the bump.

    Monday, February 12, 2018 2:04 AM
  • "In the while loop's condition check if the integer has a value indicating you are done"

    What do you mean here? Could you elaborate or maybe show an example? It's not a class assignment by the way(i don't study at any school), i'm just interested in learning to code and found this particular problem.


    Tuesday, February 13, 2018 4:02 AM
  • I should have added a comma to make it "In the while loop's condition, check if the integer has a value indicating you are done". In other words if you are reading in 3 records then somehow check to see if three records have been processed.

    I have learned not to spend much time on members with zero points. Even when I spend a hour of my time they never do anything to appreciate the work. There is probably just one person making bunches of new users and it is impossible to know when that is a case.



    Sam Hobbs
    SimpleSamples.Info

    Tuesday, February 13, 2018 4:50 AM
  • "I have learned not to spend much time on members with zero points. Even when I spend a hour of my time they never do anything to appreciate the work. There is probably just one person making bunches of new users and it is impossible to know when that is a case."

    What do you mean they don't appreciate the work? How so? And it's unfair to lump all users with 0 points together, i just found this site and thought it was a good place to get some help.  This is my first account ever here.

    Tuesday, February 13, 2018 2:36 PM
  • Greetings.

    SS is saying that a lot of new members don't mark replies as answers or vote them as helpful. He's right that this can be frustrating, because we get points for providing answers and maximum points when the asker is the one who marks the answer. It can be disheartening when we put a lot of work into a reply only to have it left unacknowledged.

    Personally, I think some new members just don't realise you are supposed to do that. It's not exactly written in neon lights anywhere.

    So, if a particular reply fixes your problem, mark it as the answer via the options in blue writing below the reply. You can mark more than one answer per thread. If any other posts helped, but didn't quite solve the issue, vote them as helpful via the "Vote" text at the top left of the post. You can also vote helpful posts on threads that other members have started, if you think they deserve recognition.

    And you're right. This is a good place to get help. It's largely troll-free (with the occasional very rare exception) and full of people who really know their stuff.

    Good luck.

    Tuesday, February 13, 2018 10:26 PM
  • What I mean is that members with zero points always mark someone else's response as an answer, as if they have a personal problem with me. Even if I spend a lot of time to help. I apologize if that does not apply to you but I have no way of knowing.

    It is very frustrating when someone asks about an entire algorithm, such as an entire program and wants help writing the whole thing. The question in this thread is vague and it can be frustrating trying to help with this type of "question".



    Sam Hobbs
    SimpleSamples.Info

    Tuesday, February 13, 2018 10:48 PM
  • I'm more than happy to mark answers and upvote other users if they're willing to help. 

    I can't figure out how to specify the number of lines that's going to be output, for example if i input "2" in the first line, get two lines of input before the output shows. And i can't connect the first 4 numbers to one person and the next 4 to another person. I've read your suggestions but can't really follow them, could you show examples to visualize it?

    Wednesday, February 14, 2018 3:35 AM
  • SS is right that we shouldn't write too much code for you, just in case you really are a student working on a homework assignment. With that in mind, the following hint is about as far as I can go for now. Have a go at filling in the blanks, and if you get stuck on specific issues, let us know.

             // Get the number of people. 
             Console.WriteLine("How many people are there? ");
             int nPeople = Convert.ToInt32(Console.ReadLine());
    
             // Give the name and sum we are looking for initial values.
             int biggestSum = -1;
             string nameWithBiggestSum = "";
    
             // Do whatever you have to do for each person.
             for (int i = 0; i < nPeople; i++)
             {
                // Ask for the person's name.
                Console.WriteLine("Enter a name : ");
    
                // Get the person's name.
                string name = Console.ReadLine();
    
                // Now ask for and get the four numbers for this person. This will be like the code from your original post.
    
                // Next, compute the sum of the numbers, and see if it's bigger than the biggest sum we have so far.
                // If it is, save it as the current biggest sum, and save the person's name. If not, ignore it.
             }
    
             // After the loop has ended, print the biggest sum we saved inside the loop and the corresponding person's name.
    If you use the above code as-is, without filling in any of the blanks, you will see that it asks for the names of the number of people you enter.

    Wednesday, February 14, 2018 4:23 AM
  • Hi Anonymmous842,

    If you want to connect the first 4 numbers to one person and the next 4 to another person, you could make a list to store name, trying to compare the name and then add the number to different.

    Here is the simple example for your reference.

     static void Main(string[] args)
            {
                List<string> Name = new List<string>();
    
                for (int i = 0; i < 4; i++)
                {
                    Console.WriteLine("Who wants to join the game");
                    string name = Console.ReadLine();
                    Name.Add(name);
                }
    
                ////if you have four people to jion the game, please create four lists to store the number.
                List<List<int>> num = new List<List<int>>();
                num.Add(new List<int> { });
                num.Add(new List<int> { });
                num.Add(new List<int> { });
                num.Add(new List<int> { });
              
                int Sum = 0;
                while (true)
                {
                    Console.WriteLine("Please input name:");
    
                    string n = Console.ReadLine();
                    if (!Name.Contains(n))
                    {
                        MessageBox.Show("Error! The name does not exist!");
                    }
                    Console.WriteLine("Please input number:");
                    int number = Convert.ToInt32(Console.ReadLine());
                    foreach (var item in Name)
                    {
                        if (item == n)
                        {
                            int index = Name.IndexOf(item);
                            // num[index].Add(number);
                            num[index].Add(number);
    
                            //string listname = "list" + index;
                            if (num[index].Count == 4)
                            {
                                foreach (var nu in num[index].ToList())
                                {
                                    Sum = nu + Sum;
                                }
                                Console.WriteLine("{0}: Sum={1}", n, Sum);
                                Sum = 0;
                            }
                        }
                      
                    }
    
    
    
                }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 14, 2018 7:37 AM
    Moderator
  • // Next, compute the sum of the numbers, and see if it's bigger than the biggest sum we have so far.

    Not sure what this means, compute the sum of the 4 numbers from the first or second person? And should i do it in a loop? Sorry for my idiotic questions :/



    Wednesday, February 14, 2018 5:34 PM
  • Wendy,

    Do you really think it helps yourself by killing communities.

    I've seen you doing good things, but this is real one of the worst things I've ever seen done by a CSG MSFT and I'm long active in the forums. 

    Hopefully for you it is not reported to Redmond. 


    Success
    Cor

    Wednesday, February 14, 2018 5:52 PM
  • For each person, as you enter the numbers, compute the sum. Obviously you do this in the loop.

    Wednesday, February 14, 2018 10:17 PM
  • // Next, compute the sum of the numbers, and see if it's bigger than the biggest sum we have so far.

    Not sure what this means, compute the sum of the 4 numbers from the first or second person? And should i do it in a loop? Sorry for my idiotic questions :/



    Hi Anonymmous842,

    You could try to put the four sum numbers in a list and compare it.

    In the code I provided in my previous reply, make a new list to store the sum. And them compare it and output the biggest one.

      if (num[index].Count == 4)
                            {
                                foreach (var nu in num[index].ToList())
                                {
                                    Sum = nu + Sum;
                                }
                                Console.WriteLine("{0}: Sum={1}", n, Sum);
    //you could add the sum to list from here.
                                Sum = 0;
                            }
    

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Anonymmous842 Friday, February 16, 2018 4:13 PM
    Thursday, February 15, 2018 5:47 PM
    Moderator