Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm making a program for school where the user enters as many numbers as desired, then calculating the average of the odd numbers and the average of the even numbers. When I run this, upon the user entering 'done', the program crashes and tells me that the evens list has no sequence available. Also I'm confused as to why there are blank lines created in the terminal which I have to press enter to get rid of and go beyond. Any help would be greatly appreciated.

using System;
using System.Collections.Generic;
namespace Lab7
{
    
    class Program
    {
        static void Main(string[] args)
        {
            //Problem1();
            //Problem2();
            //Problem3();
            Problem4();
            //Problem5();
            //Problem6();
            //Problem7();
            //Problem8();
            //Problem9();
            //Problem10();

            Console.ReadKey();
        }static void Problem4()
        {

            List<int> evens = new List<int>();
            List<int> odds = new List<int>();
            int current;
            string endKey = "DONE";
            
           while (Console.ReadLine().ToUpper() != endKey)
            { Console.WriteLine("Enter a number or 'done' to quit: ");
                if (Console.ReadLine().ToUpper() == endKey)
                    break;
                if (int.TryParse(Console.ReadLine(), out current))
                    if (current % 2 == 0)
                    evens.Add(current);
                else odds.Add(current);
                continue;
            }

            Console.WriteLine("Even average: {0}.", evens.Average());
            Console.WriteLine("Odd average: {0}.",  evens.Average());
            
            } 


What I have tried:

Still quite new to C#, we recently finished with Python. This is very difficult!
Posted
Updated 28-Nov-21 17:44pm
Comments
BillWoodruff 29-Nov-21 0:44am    
Did you remove your previous question, and my responses ?
Grelm 29-Nov-21 1:18am    
Yes, I was desperate to get help and assumed that simply creating a new question would elicit better results, considering that I had by then altered the code to the point where the question was an entirely different one. I appreciate your help though.
BillWoodruff 29-Nov-21 1:26am    
I was hoping to give you enough of a "clue" that you could complete the code by yourself. By removing the thread, you prevent anyone else seeing my response.

"Double posting" like this is technically against forum guidelines.

I find it unfortunate that other responders here write your code for you, rather than educate you. If we do your homework, you will learn nothing.

Think about it.
Grelm 29-Nov-21 1:33am    
I understand your sentiment. I didn't realize that "double posting" is against the rules.

In the future, I won't delete posts so as to allow commenters to have their advice shared with others.

As far as not helping one learn, I totally disagree. When I see the proper syntax etc... I remember it and reuse it. I promise, I'm not interested in copy/pasting answers and blindly turning them in. If I did that then I'd be screwed on exams. I want to learn so I can get work, not just for school grades.
phil.o 29-Nov-21 3:37am    
It's not just a matter of syntax. Important part is how to think about a problem and build an algorithm. Using the right syntax is not the difficult part of the job :)

1 solution

Hi,

I would suggest you pay more attention to proper indentation, and use more
brackets, all this to make the code more readable.

Then, keep in mind that every time you execute Console.ReadLine the user has to enter something. In particular, you cannot use ReadLine twice to offer the user a choice between entering a number and entering 'done'; this must be handled with a single ReadLine.

Finally, validating user input is a must. As a result, you nearly always need to capture user input into a variable, check it, then use it.

BTW: you have a superfluous 'continue' (and you had way too many of them in an earlier problem3 too).

So my code would look more like this:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1 {
	class Program {
		static void Main(string[] args) {
			Problem4();
			Console.ReadKey();
		}

		static void Problem4() {
			List<int> evens = new List<int>();
			List<int> odds = new List<int>();
			int current;
			string endKey = "DONE";

			while (true) {
				Console.WriteLine("Enter a number or 'done' to quit: ");
				string input = Console.ReadLine();
				if (int.TryParse(input, out current)) {
					if (current % 2 == 0) evens.Add(current);
					else odds.Add(current);
				} else {
					if (input.ToUpper() == endKey) break;
					Console.WriteLine("Bad input in: " + input);
				}
			}
			Console.WriteLine("Even average: {0}.", evens.Average());
			Console.WriteLine("Odd average: {0}.", odds.Average());
		}
	}
}
 
Share this answer
 
v2
Comments
Grelm 29-Nov-21 0:02am    
Thank you so much! I didn't realize that "input" was a thing! I thought I'd tried it before.
Luc Pattyn 29-Nov-21 0:15am    
You're welcome.

BTW: input isn't a thing, it is just the name I have chosen for the string variable that I'm using to hold whatever the user types. Don't be afraid of using variables, and please give them meaningful names.
Grelm 29-Nov-21 1:19am    
I used input directly as you did, and it worked! lol. So I guess it is a thing? It does come with a green warning underlining though.

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