Click here to Skip to main content
15,936,975 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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, and i want to output which person has the biggest sum. 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 i want to input the sum for 3 different people.

What I have tried:

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();
            }
Posted
Updated 11-Feb-18 10:37am
v2

You can use a while loop like this:
while (true)
{
    string line = Console.ReadLine();

    if (string.IsNullOrEmpty(line))
        break;

    Console.WriteLine(line);
}

Or:
string line;

do
{
    line = Console.ReadLine();
    Console.WriteLine(line);
}
while (!string.IsNullOrEmpty(line));
 
Share this answer
 
v2
Comments
Wagner 11-Feb-18 16:18pm    
That's not really what i want though, don't know if you understood my question. First input is the number of lines i want to write. For example if i write "2" i want to input two lines of code before i get an output. So after i type "2" i want to write 4 numbers in one line and then again 4 numbers in another line and output which of the lines has the biggest sum, if that makes sense.
RickZeeland 12-Feb-18 2:18am    
Just wanted to point out that your check for null probably doesn't work, so just use the string.IsNullOrEmpty() function.
You should first ask for the number of lines, verify that input is indeed a valid integer, and store it in a counter.
Then, a loop which would execute the desired number of lines.
C#
int count;
while (!int.TryParse(Console.ReadLine(), out count)); // Execute until a valid integer is provided
int i = 0;
while (i < count) {
   // your line input and parsing here
}

Hope this helps. Kindly.
 
Share this answer
 
Comments
Wagner 11-Feb-18 16:52pm    
I think i understand how to specify number of lines now, but if we go with my example in the original comment, how can i print which of the lines of numbers that has the largest sum if we attribute one line to a person named John and the other to a person named Sara? I'm not sure what approach to use.
phil.o 11-Feb-18 16:56pm    
"how can i print which of the lines of numbers that has the largest sum?"
You have to store that information somewhere. An array having 'count' elements for example.

"if we attribute one line to a person named John and the other to a person named Sara"
Then you have to ask for that information in the first place. You can then have 2 arrays, each having 'count' elements: the first array would contain the names, and the second would contain the result of the sum. The sum at index 0 in second array would match the name at index 0 in the first array. This is only one of the possibilities, there are others, but this one would fit.
Wagner 11-Feb-18 17:09pm    
Could you give an example? What do you mean by count elements?
phil.o 11-Feb-18 17:52pm    
An array which has exactly count elements, count being the name of the variable I wrote in the code I gave you as an example.

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