This is an example from an assignment I'm working on in which we need to validate that the user enters at least 1 Like for the temp.like[3] array and 1 Dislike for the temp.dislike[3] array.

I tried using a while statement that said " while (temp.like == "") " or " while (temp.like == null) " but had trouble with both.

Can anyone suggest a validation method to make sure the user has entered at least 1 like and 1 dislike?

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

namespace LikesAndDislikes
{
    class Program
    {
        const int MAX_LIKES = 3;
        const int MAX_DISLIKES = 3;
        struct clientType
        {
            public string[] likes;
            public string[] dislikes;
        }
        static void Main(string[] args)
        {
            clientType temp = new clientType();
            temp.likes = new string[MAX_LIKES];
            temp.dislikes = new string[MAX_DISLIKES];
            Console.WriteLine("\nEnter at least 1 like\n");
            for (int i = 0; i < MAX_LIKES; i++)
            {
                Console.WriteLine("{0}{1}", "Enter like # ", (i + 1));
                temp.likes[i] = Console.ReadLine().Trim();
            }
            Console.WriteLine("\nEnter at least 1 dislike\n");
            for (int i = 0; i < MAX_DISLIKES; i++)
            {
                Console.WriteLine("{0}{1}", "Enter dislike # ", (i + 1));
                temp.dislikes[i] = Console.ReadLine().Trim();
            }
            print(temp);
        }
        static void print(clientType temp)
        {
            for (int i = 0; i < MAX_LIKES; i++)
            {
                Console.WriteLine("\nLike {0,-2}{1}",(i + 1), temp.likes[i]);
                
            }
            for (int i = 0; i < MAX_DISLIKES; i++)
            {
                Console.WriteLine("\nDislike {0,-2}{1}", (i + 1), temp.dislikes[i]);
                 
            }
        }
    }
}

Recommended Answers

All 3 Replies

Hint: use the method String.IsNullOrEmpty()

You may want to change around your for loop into a while or do/while loop. You could use either of those structures to make sure you have at least 1 item in each and keep re-prompting if you don't.

You can also drop the explicit coding of the loop in favor of using the Any extension method. Sample:

int[] array = { 1, 2, 3, 4, 5 };
bool hasEvenElement = array.Any(item => item % 2 == 0);

In this example, all it is doing is checking if any element within the array is even. If so, it returns true. Once it finds the first element satisfying the criteria, it no longer has to check. It's similar to writing a foreach or while loop and breaking on the first matching criteria, except you don't have to write the loop.

commented: I'm always impressed by the way you reduce all that code to like 2 statements! +4

Alright check this out. I think it works like I wanted it too.

The do while loop suggestion helped a lot. Using a boolean and especially the 'if' statement is where I made my breakthrough in my logic.

Using the .Length function and checking if my increment was > 0 is where the magic happened for me.

Thanks guys for the suggestions. I am always amazed how helpful and timely this forum is.

Cheers.

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

namespace LikesAndDislikes
{
    class Program
    {
        const int MAX_LIKES = 3;
        const int MAX_DISLIKES = 3;
        struct clientType
        {
            public string[] likes;
            public string[] dislikes;
            public int likeLength;
            public int dislikeLength;
        }
        static void Main(string[] args)
        {
            clientType temp = new clientType();
            createClient(out temp);
            bool validEntry = false;
            do
            {
                Console.WriteLine("\nEnter at least 1 like\n");
                for (int i = 0; i < MAX_LIKES; i++)
                {
                    Console.WriteLine("{0}{1}", "Enter like # ", (i + 1));
                    temp.likes[i] = Console.ReadLine().Trim();
                    if (temp.likes[i].Length > 0)
                        validEntry = true;
                }
            } while (validEntry == false);
            validEntry = false;
            do
            {
                Console.WriteLine("\nEnter at least 1 dislike\n");
                for (int i = 0; i < MAX_DISLIKES; i++)
                {
                    Console.WriteLine("{0}{1}", "Enter dislike # ", (i + 1));
                    temp.dislikes[i] = Console.ReadLine().Trim();
                    if (temp.dislikes[i].Length > 0)
                        validEntry = true;
                }
            } while (validEntry == false);
            print(temp);
        }
        static void createClient(out clientType temp)
        {
            temp.likes = new string[MAX_LIKES];
            temp.likeLength = 0;
            temp.dislikes = new string[MAX_DISLIKES];
            temp.dislikeLength = 0;
        }
        static void print(clientType temp)
        {
            for (int i = 0; i < MAX_LIKES; i++)
            {
                Console.WriteLine("\nLike {0,-2}{1}",(i + 1), temp.likes[i]);
                
            }
            for (int i = 0; i < MAX_DISLIKES; i++)
            {
                Console.WriteLine("\nDislike {0,-2}{1}", (i + 1), temp.dislikes[i]);
                 
            }
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.