0

I'm stuck on this program and any help would be appreciated.

There are 3 methods: inputPhone, outputPhones, and Main. The inputPhone runs in a loop and accepts user input. This information should be stored in an array. Until the loop is broken, the user will continue putting in information. If broken, every phone inputted will be displayed.

I'm using ref parameters. I need to store those values in the Main method, then pass the array through the method.

I'm just looking for a push in the right direction; an example would be great.

EDIT: Problem has been solved. Thank you so much for your input guys. I'll add in your suggestions soon to clean up my code to have it run a little nicer.

asked Jan 24, 2014 at 19:33
4
  • 6
    Just a suggestion - continue is a keyword in C#, I would suggest naming that variable something different (e.g. shouldContinue). Commented Jan 24, 2014 at 19:36
  • Good point, it works as is but I'll change it. Thanks for the pointer. :) Commented Jan 24, 2014 at 19:38
  • No problem, just something worth considering! Commented Jan 24, 2014 at 19:40
  • 3
    You really should use a List<Phone> instead Commented Jan 24, 2014 at 19:40

5 Answers 5

2

First of all I would create a separate class for storing your phones data:

public class Phone
{
 public string Manufacturer { get; set; }
 public string Model { get; set; }
 public bool HasCord { get; set; }
 public double Price { get; set: }
}

Then instead of:

static void inputPhone(ref string manufacturer, ref string model, ref bool hasCord, ref double price)

You could have:

static Phone GetPhone()

And you could create an instance of a Phone inside GetPhone method and return the object that is filled in with appropriate data.

Instead of:

double[] prices = new double[100];
string[] manufacturers = new string[100];
string[] models = new string[100];
bool[] hasCords = new bool[100];

You could then have:

List<Phone> phones = new List<Phone>(); 

And then after each call to GetInput (previously: inputPhone) add it to the list:

phones.Add(GetPhone()); 

Then change the outputPhones to:

static void DisplayPhones(List<Phone> phones)

So all in all your code would look like this:

static Phone GetPhone()
{
 Phone phone = new Phone(); 
 Console.Write("Enter the phone manufacturer: ");
 phone.Manufacturer = Console.ReadLine();
 Console.Write("Enter the phone model: ");
 phone.Model = Console.ReadLine();
 Console.Write("Is it cordless? [Y or N]: ");
 phone.HasCord = Console.ReadLine().ToUpper() == "Y";
 Console.Write("Enter the phone price: ");
 phone.Price = Convert.ToDouble(Console.ReadLine());
 return phone; 
}
static void DisplayPhones(List<Phone> phones)
{
 for (int i = 0; i < phones.Count; i++)
 {
 Console.WriteLine("==Phone #{0}==", i);
 Console.WriteLine("Phone Manufacturer: {0}", phones[i].Manufacturer);
 Console.WriteLine("Phone Model: {0}", phones[i].Model);
 Console.WriteLine("Has Cord: {0}", phones[i].HasCord ? "Yes" : "No");
 Console.WriteLine("Phone Price: {0}", phones[i].Price);
 }
 Console.WriteLine("Number of phones entered: {0}", phones.Count);
}
static void Main(string[] args)
{
 List<Phone> phones = new List<Phone>(); 
 bool shouldContinue = true;
 do
 {
 phones.Add(GetPhone());
 Console.Write("Would like to process another phone? [Y or N]: ", shouldContinue);
 shouldContinue = Console.ReadLine().ToUpper() == "Y";
 } while (shouldContinue == true);
 if (shouldContinue == false)
 {
 DisplayPhones(phones);
 }
}
answered Jan 24, 2014 at 19:44

1 Comment

This makes a lot of sense. Like I responded below, I was not aware of the List option; I'm still relatively new to C# so, this will be helpful for future projects. Thank you :)
1

You'll need to access a particular item of the array using array[numberOfPhones], then pass it using ref array[numberOfPhones].

do
{
 inputPhone(ref manufacturers[numberOfPhones], 
 ref models[numberOfPhones], 
 ref hasCords[numberOfPhones], 
 ref prices[numberOfPhones]);
 numberOfPhones++; // Increase the number of phones afterwards
 Console.Write("Would like to process another phone? [Y or N]: ", Continue);
 Continue = Console.ReadLine().ToUpper() == "Y";
} while (Continue == true);
answered Jan 24, 2014 at 19:42

4 Comments

Perfect! I knew I was close I just couldn't get over that last step. Much appreciated. Will accept your response when it lets me.
While this will technically work, I think the other responses show a much better and simpler design.
I agree that it could be done easier, but the issue/question was passing the variables as references, not redesigning his code.
Well now I know how to do it both ways. You guys rock!
1

Make all thos arrays properties of that class Phone and you will have just to access them where you want in that class. Beside of that you can use out insted of ref and not be aware of initialize everything befor run the code.

answered Jan 24, 2014 at 19:41

Comments

1

This your code rewritten to take advantage of a List<Phone> instead of arrays. As you can see the whole code is simplified a lot comparing to an array based approach

class Phone
{
 // Property to store the fields of a Phone object
 public string Manifacturer {get;set;}
 public string Model {get;set;}
 public string IsCordless {get;set;}
 public decimal Price {get;set;}
 // No parameters to pass, but returns a Phone instance 
 static Phone inputPhone()
 {
 Phone p = new Phone();
 Console.Write("Enter the phone manufacturer: ");
 p.Manufacturer = Console.ReadLine();
 Console.Write("Enter the phone model: ");
 p.Model = Console.ReadLine();
 Console.Write("Is it cordless? [Y or N]: ");
 p.IsCordless = Console.ReadLine().ToUpper() == "Y";
 Console.Write("Enter the phone price: ");
 p.Price = Convert.ToDecimal(Console.ReadLine());
 return p;
 }
 // Pass the list and loop on every element 
 static void outputPhones(List<Phone> pList)
 {
 foreach (Phone o in pList)
 {
 Console.WriteLine("Phone Manufacturer: {0}", p.Manufacturer);
 Console.WriteLine("Phone Model: {0}", p.Model);
 Console.WriteLine("Has Cord: {0}", p.IsCordless ? "Yes" : "No");
 Console.WriteLine("Phone Price: {0}", p.Price);
 }
 Console.WriteLine("Number of phones entered: {0}", pList.Count);
 }
 static void Main(string[] args)
 {
 // Initialize an empty Phone list
 List<Phone> pList = new List<Phone>();
 bool continueLoop = true;
 do
 {
 // Get the input from the user and add to the list
 Phone p = inputPhone();
 pList.Add(p);
 Console.Write("Would like to process another phone? [Y or N]: ", continueLoop);
 continueLoop = Console.ReadLine().ToUpper() == "Y";
 } while (continueLoop == true);
 // output the list 
 outputPhones(pList);
 }
}
answered Jan 24, 2014 at 19:47

Comments

1

Why not make phone a class:

public class Phone
{
 public double Price {get;set;}
 public string Manufacturer {get;get;}
 public string Model {get;set;}
 public bool HasCord{get;set;}
 public override string ToString()
 {
 return string.Format("Phone Manufacturer: {0}\nPhone Model {1}\nHas Cord: {2}\nPhone Price{3}", this.Manufacturer, this.Model, this.HasCord ? "Yes" : "No", this.Price);
 }
}

Then, you can change your program to be more object oriented and look like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace newProg
{
 class YourApp
 {
 private Phone inputPhone()
 {
 Console.Write("Enter the phone manufacturer: ");
 string manufacturer = Console.ReadLine();
 Console.Write("Enter the phone model: ");
 string model = Console.ReadLine();
 Console.Write("Is it cordless? [Y or N]: ");
 bool hasCord = Console.ReadLine().ToUpper() == "Y";
 Console.Write("Enter the phone price: ");
 double price = Convert.ToDouble(Console.ReadLine());
 return new Phone {
 Manufacturer = manufacturer,
 Model = model,
 HasCord = hasCord,
 Price = price;
 };
 }
 static void outputPhones(List<Phone> phones)
 {
 foreach (var index = 0; index < phones.Length; index++)
 {
 Console.WriteLine("==Phone #{0}==", index);
 Console.WriteLine(phone.ToString());
 }
 Console.WriteLine("Number of phones entered: {0}", phones.Length);
 }
 static void Main(string[] args)
 {
 List<Phone> phones = new List<Phone>();
 bool Continue = true;
 do
 {
 phones.Add(inputPhone());
 Console.Write("Would like to process another phone? [Y or N]: ", Continue);
 Continue = Console.ReadLine().ToUpper() == "Y";
 } while (Continue == true);
 if (Continue == false)
 {
 outputPhones(phones);
 }
 }
}
answered Jan 24, 2014 at 19:48

2 Comments

I never knew about List, but seems a great way to simplify everything.
List is probably the most useful data structure in C#. There are many other useful data structures in the System.Collections namespace. I'm glad I could help.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.