I am currently following tutorials on c# as I am a c++ programmer, and am wanting to see what all the .NET talk is about with c# ;). Here is what I want my program to do. It asks the user to input title, author, subject, and bookID for 3 books (or however many I set the loop for).
I am a bit confused with the usage of the foreach loop. Can someone lend me a hand here please ?

Thank you!

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

namespace ConsoleApplication1
{


    class StringProg
   {
        struct book
        {
            public string title;
            public string author;
            public string subject;
            public int bookID;

            public void Getinfo()
            {
                Console.WriteLine("Please enter the title of the book:");
                title = Console.ReadLine();
                Console.WriteLine("Please enter the author of the book:");
                author = Console.ReadLine();
                Console.WriteLine("Please enter the subject of the book:");
                subject = Console.ReadLine();
                Console.WriteLine("Please enter the book ID of the book:");
                bookID = Convert.ToInt32(Console.ReadLine());

            }

            public void Display()
            {

                Console.Clear();
                Console.WriteLine("This is the information for the book you entered:\n\n");
                Console.WriteLine(title);
                Console.WriteLine(author);
                Console.WriteLine(subject);
                Console.WriteLine(bookID);
            }
        }

      static void Main(string[] args)
      {

          book[] books = new book[3];
          for (int i = 0; i < 3; i++)
          {
              Console.WriteLine("Getting book information for book #:{0}\n\n", i);

          foreach(int j in books[i])
          {
              books[j].Getinfo();
              books[j].Display();
          }
          }
          Console.ReadLine();
      }

   }
}

Recommended Answers

All 3 Replies

You don't need the foreach loop. i is already indexing each book; you just need to call the methods on book[i].

Here is newly written code. I got it working the way I want it to work. It now asks how many books do I wish to enter information for. Then it lets me input information for each one. After that it displays all of the books information. What I want is to learn how I could use the foreach loop for my second for loop in the below code. I am just stubbornly wanting to know how to use it as I OCD on little things like this.

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

namespace ConsoleApplication1
{


    class StringProg
    {
        struct book
        {
            public string title;
            public string author;
            public string subject;
            public int bookID;

            public void Getinfo()
            {
                Console.WriteLine("Please enter the title of the book:");
                title = Console.ReadLine();
                Console.WriteLine("Please enter the author of the book:");
                author = Console.ReadLine();
                Console.WriteLine("Please enter the subject of the book:");
                subject = Console.ReadLine();
                Console.WriteLine("Please enter the book ID of the book:");
                bookID = Convert.ToInt32(Console.ReadLine());
                Console.Clear();


            }

            public void Display()
            {
                Console.WriteLine("\n\tThis is the information for the books you entered:\n\n");
                Console.WriteLine(title);
                Console.WriteLine(author);
                Console.WriteLine(subject);
                Console.WriteLine(bookID);
            }
        }

        static void Main(string[] args)
        {
            int choice;
            int howmany;
            Console.WriteLine("Please enter how many books will be databased");
            howmany = Convert.ToInt32(Console.ReadLine());
            book[] books = new book[howmany];
            choice = books.Length;
            for (int i = 0; i < choice ; i++)
            {
                Console.WriteLine("Getting book information for book #:{0}\n\n", i+1);
                books[i].Getinfo();
            }
            //How would I use the foreach loop below to do the same thing as the below forloop?
            for (int j = 0; j < choice; j++)
            {
                books[j].Display();
            }

            Console.ReadLine();
        }

    }
}

The C# Programming Guide has a quick overview of how to use foreach with arrays. The C# Reference explains a bit about how it uses IEnumerable to get elements of the collection. Follow links and read for more details.

In your case, this:

for (int j = 0; j < choice; j++)
{
    books[j].Display();
}

becomes this:

foreach(book b in books)
{
    b.Display();
}
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.