To give some context. So for this project I have an array of my class variables, the cd1. That is the variable for the AudioCD and it is an array because if the user wants to enter more CD in to the array.
The problem I am getting is that when I enter more then one cd into the class array, at around the part where I add the string or artist is where I get the issue. When I go to print the class variables specific index which should be seperate, mostly everything except the artist array for the shows the last entered lines for some weird reason. I am trying to figure out why it is doing this.
PS. Sorry is my explanation is not the best.
As you see in the image. The out put should be when I input 1 for choice and 1 for the CD, the array should use the array for index 0, but it is using the array from the last inputted CD. The output should be:
man1 man2 man3
but it is:
man4 man5 man6
class AudioCD
{
// Private Variables for CDclass
public string cdTitle { get; private set; }
private string[] artists = new string[4];
public int releaseYear { get; private set; }
public string genre { get; private set; }
public float condition { get; private set; }
// Constructor for the CDclass - initializes all variables used in the CDclass
public AudioCD()
{
cdTitle = "";
artists = new string[] {"","","",""};
releaseYear = 1980;
genre = "";
condition = 0.0f;
}
// Overload Constructor for the CDclass - initializes all the variables to user input variables
public AudioCD(string cdt, string[] art, int reY, string gen, float con)
{
cdTitle = cdt;
if (artists.Length < art.Length)
{
Console.WriteLine("Your Array size is bigger then 4 for the Artist so the first 4 names will be used!");
}
artists = art;
if (reY < 1980)
{
releaseYear = 1980;
}
else
{
releaseYear = reY;
}
genre = gen;
if (con < 0.0f || con > 5.0f)
{
condition = 0.0f;
}
else
{
condition = con;
}
}
public void printAudioCD()
{
Console.Write(cdTitle ", " releaseYear "\n" );
for (int i = 0; i < artists.Length; i )
{
if (artists[i] != "" )
{
Console.WriteLine("Artist (#" (i 1) "): " artists[i]);
}
}
Console.WriteLine("Genre: " genre);
Console.WriteLine("Condition: " condition);
}
}
and Program
class:
class Program
{
static void Main(string[] args)
{
// variables
string uI, cdtitle, genre;
int size = 0, releaseYear, choice, arrInd;
string[] artistArray = new string[4] {"", "", "", "" };
float condition;
//
AudioCD remote = new AudioCD();
Console.Write("How many CDs do you have lying around your car? ");
uI = Console.ReadLine();
size = Convert.ToInt32(uI);
AudioCD[] cd1 = new AudioCD[size];
for (int i = 0; i < size; i )
{
Console.WriteLine("CD #" (i 1));
Console.Write("*Enter Title: ");
uI = Console.ReadLine();
cdtitle = uI;
Console.WriteLine("*Enter Artists (type -1 when finished):");
int j = 0;
do
{
uI = Console.ReadLine();
if (uI != "-1")
artistArray[j] = uI;
j ;
// Resize the array by One Element
if (j >= 4 && uI != "-1")
{
Array.Resize(ref artistArray, artistArray.Length 1);
artistArray[j] = "";
}
} while (uI != "-1" );
Console.Write("*Enter Genre: ");
uI = Console.ReadLine();
genre = uI;
Console.Write("*Enter Release Year: ");
uI = Console.ReadLine();
releaseYear = Convert.ToInt32(uI);
Console.Write("*Enter Condition: ");
uI = Console.ReadLine();
condition = float.Parse(uI);
Console.Write("\n");
// switch to select which class of cd to put information in
cd1[i] = new AudioCD(cdtitle, artistArray, releaseYear, genre, condition);
}
bool isPlaying = true;
while(isPlaying)
{
Console.Write("\n");
Console.WriteLine("[Main Menu]");
Console.WriteLine("1) Album Info");
Console.WriteLine("2) Find a CD");
Console.WriteLine("3) Find an artist");
Console.WriteLine("4) Log off");
Console.Write("Choice: ");
uI = Console.ReadLine();
choice = Convert.ToInt32(uI);
switch(choice)
{
case 1:
{
Console.Write("\nWhich CD do you want? ");
uI = Console.ReadLine();
arrInd = Convert.ToInt32(uI);
if (arrInd >= 1 || arrInd <= size)
{
Console.Write(arrInd ". ");
cd1[(arrInd - 1)].printAudioCD();
}
break;
}
case 2:
{
break;
}
case 3:
{
break;
}
case 4:
{
isPlaying = false;
break;
}
default:
{
break;
}
}
}
}
}
OUTPUT As you see in the image. The out put should be when I input 1 for choice and 1 for the CD, the array should use the array for index 0, but it is using the array from the last inputted CD. The output should be man1,man2, and man3, but it is man4, man5, and man6.
CodePudding user response:
You are using the same array in your Main()
for each iteration and this is the reason why you got the artists from the last inputted CD. I mean this code snippet:
string[] artistArray = new string[4] {"", "", "", "" };
Here we can use List<T>
to avoid writing code of resizing of an array and we can use Clear() method to avoid storing artists from the previous CD.
The whole code would look like this. I've refactored your code a little bit by using List:
The class AudioCD
:
public class AudioCD
{
// Private Variables for CDclass
public string cdTitle { get; private set; }
private List<string> artists = new List<string>();
public int releaseYear { get; private set; }
public string genre { get; private set; }
public float condition { get; private set; }
// Constructor for the CDclass - initializes all variables used in the CDclass
public AudioCD()
{
cdTitle = "";
releaseYear = 1980;
genre = "";
condition = 0.0f;
}
// Overload Constructor for the CDclass - initializes all the variables to user input variables
public AudioCD(string cdt, List<string> art, int reY, string gen, float con)
{
cdTitle = cdt;
artists.AddRange(art);
if (reY < 1980)
{
releaseYear = 1980;
}
else
{
releaseYear = reY;
}
genre = gen;
if (con < 0.0f || con > 5.0f)
{
condition = 0.0f;
}
else
{
condition = con;
}
}
public void printAudioCD()
{
Console.Write(cdTitle ", " releaseYear "\n");
for (int i = 0; i < artists.Count; i )
{
if (artists[i] != "")
{
Console.WriteLine("Artist (#" (i 1) "): " artists[i]);
}
}
Console.WriteLine("Genre: " genre);
Console.WriteLine("Condition: " condition);
}
}
and Main
method would look like this:
// variables
string uI, cdtitle, genre;
int size = 0, releaseYear, choice, arrInd;
List<string> artistArray = new List<string>();
float condition;
Console.Write("How many CDs do you have lying around your car? ");
uI = Console.ReadLine();
size = Convert.ToInt32(uI);
AudioCD[] cd1 = new AudioCD[size];
for (int i = 0; i < size; i )
{
Console.WriteLine("CD #" (i 1));
Console.Write("*Enter Title: ");
uI = Console.ReadLine();
cdtitle = uI;
Console.WriteLine("*Enter Artists (type -1 when finished):");
int j = 0;
do
{
uI = Console.ReadLine();
if (uI != "-1")
artistArray[j] = uI;
j ;
} while (uI != "-1");
Console.Write("*Enter Genre: ");
uI = Console.ReadLine();
genre = uI;
Console.Write("*Enter Release Year: ");
uI = Console.ReadLine();
releaseYear = Convert.ToInt32(uI);
Console.Write("*Enter Condition: ");
uI = Console.ReadLine();
condition = float.Parse(uI);
Console.Write("\n");
// switch to select which class of cd to put information in
cd1[i] = new AudioCD(cdtitle, artistArray, releaseYear, genre, condition);
artistArray.Clear();
}
bool isPlaying = true;
while (isPlaying)
{
Console.Write("\n");
Console.WriteLine("[Main Menu]");
Console.WriteLine("1) Album Info");
Console.WriteLine("2) Find a CD");
Console.WriteLine("3) Find an artist");
Console.WriteLine("4) Log off");
Console.Write("Choice: ");
uI = Console.ReadLine();
choice = Convert.ToInt32(uI);
switch (choice)
{
case 1:
{
Console.Write("\nWhich CD do you want? ");
uI = Console.ReadLine();
arrInd = Convert.ToInt32(uI);
if (arrInd >= 1 || arrInd <= size)
{
Console.Write(arrInd ". ");
cd1[(arrInd - 1)].printAudioCD();
}
break;
}
case 2:
{
break;
}
case 3:
{
break;
}
case 4:
{
isPlaying = false;
break;
}
default:
{
break;
}
}
}