Problem with array of structs

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi guys
Im currently working on an array of structs but am having trouble getting the data from the user.
Ive created the struct, initialized the variables and then created an array of the struct. The problem is filling the struct with user input.
I need the user to enter the book title, author and price so that the data is stored within the array of struct. In other words, if the user wanted to enter details for 4 books, the title, author and price would be stored within the array. As
you can see by my code im having trouble.

Can anyone offer advice on where I have gone wrong?
Thanks for your help, its much appreciated.
Fatbot

<pre class="prettyprint using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
public struct BookInformation
{
public string bookTitle;
public string bookAuthor;
public double bookPrice;
}

static void Main(string[] args)
{
BookInformation currentBook;

currentBook.bookTitle = "";
currentBook.bookAuthor = "";
currentBook.bookPrice = 0;

const int ARRAY_LENGTH = 10;
BookInformation[] bookArray = new BookInformation[ARRAY_LENGTH];

for (int index = 0; index < bookArray.Length; index++)
{
Console.WriteLine("Please enter the book title.");
currentBook.bookTitle = Console.ReadLine();
}

for (int index = 0; index < bookArray.Length; index++)
{
Console.WriteLine("Please enter the book author.");
currentBook.bookAuthor = Console.ReadLine();
}

for (int index = 0; index < bookArray.Length; index++)
{
Console.WriteLine("Please enter the book price.");
currentBook.bookPrice = Convert.ToDouble(Console.ReadLine());
}

Console.ReadKey();
}
}
}[/code]
<br/>

View the full article
 
Back
Top