I have a basic class that has four attributes (Patient). In Main() I have reserved the memory for the array and before I actually create the instance, I ask the user for the account number to ensure it doesn't already exist within the array. So BinarySearch requires it to be sorted but as soon as I sort it the ability to do the for loop is lost.
//Variables
int intMaxNum = 5; //set max number of patients to 5
int intInputValue;
int intResult;
string strTempName;
int intTempAge;
double dblTempTotal;
Patient[] objectPatient = new Patient[intMaxNum]; //create an array of references
for (int x = 0; x < objectPatient.Length; ++x)
{
//attempt to create a 'shadow' class to search through and keep integrity of main class (objectPatient)
Patient[] tempobjectPatient = new Patient[intMaxNum];
tempobjectPatient = objectPatient;
if (x > 0)
{
Console.Write("\n***Next Patient***");
Array.Sort(tempobjectPatient); //this will sort both objects even though I send the temporary class only - interface impact I'm sure
}
//ask for the Patient Account number
Console.Write("\nEnter Patient Account Number: ");
ReadTheAccountNumber:
intInputValue = Convert.ToInt32(Console.ReadLine());
//create temporary class for comparison
Patient SeekPatient = new Patient();
SeekPatient.PatientNumber=intInputValue; // reset the default info with the input Pateint Account Number
//verify the Patient Account number doesn't already exist
intResult = Array.BinarySearch(tempobjectPatient, SeekPatient);
//intResult = Array.BinarySearch(objectPatient, SeekPatient);
//if (objectPatient.Equals(SeekPatient)) //Can not get the .Equals to work at all...
if (intResult >= 0)
{
Console.Write("\nSorry, Patient Account Number {0} is a duplicate.", intInputValue);
Console.Write("\nPlease re-enter the Patient Account Number: ");
goto ReadTheAccountNumber;
}
else //no match found, get the rest of the data and create the object
{
if (x > 0) { Console.Write("***Patient Account Number unique and accepted***\n"); } //looks silly to display this if entering the first record
Console.Write("Enter the Patient Name: ");
strTempName = Console.ReadLine();
Console.Write("Enter the Patient Age: ");
intTempAge = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the total annual Patient amount due: ");
dblTempTotal = Convert.ToDouble(Console.ReadLine());
objectPatient[x] = new Patient(intInputValue, strTempName, intTempAge, dblTempTotal);
}
}
Here is the class:
class Patient : IComparable
{
//Data fields
private int patientNumber;
private string patientName;
private int patientAge;
private double patientAmountDue;
//Constructors
public Patient(): this(9,"ZZZ",0,0.00)
{
}
public Patient(int _patientNumber, string _patientName, int _patientAge, double _patientAmountDue)
{
PatientNumber = _patientNumber;
PatientName = _patientName;
PatientAge = _patientAge;
PatientAmountDue = _patientAmountDue;
}
//Properties
public int PatientNumber
{
get { return patientNumber; }
set { patientNumber = value; }
}
public string PatientName
{
get { return patientName; }
set { patientName = value; }
}
public int PatientAge
{
get { return patientAge; }
set { patientAge = value; }
}
public double PatientAmountDue
{
get { return patientAmountDue; }
set { patientAmountDue = value; }
}
//Interfaces
int IComparable.CompareTo(Object o)
{
int returnVal; //temporary value container
Patient temp = (Patient)o; //create temp instance of the class
if (this.PatientNumber > temp.PatientNumber)
returnVal = 1;
else
if (this.PatientNumber < temp.PatientNumber)
returnVal = -1;
else
returnVal = 0; //exact match
return returnVal;
}
}
You can put all the patient numbers into HashSet<int> and test via Contains() if a number is allocated one:
class Patient : IComparable {
...
// Simplest, not thread safe
private static HashSet<int> s_AllocatedPatientNumbers = new HashSet<int>();
public static Boolean IsNumberAllocated(int patientNumber) {
return s_AllocatedPatientNumbers.Contains(patientNumber);
}
public int PatientNumber {
get {
return patientNumber;
}
set {
s_AllocatedPatientNumbers.Remove(patientNumber);
patientNumber = value;
s_AllocatedPatientNumbers.Add(patientNumber);
}
}
}
So whenever you need to test if the number has been allocated you have no need to create a temporal patient, sort the array etc. just one simple call:
if (Patient.IsNumberAllocated(intInputValue)) {
...
}
This
Patient[] tempobjectPatient = new Patient[intMaxNum];
creates a new array and assigns it to tempobjectPatient. But this new array is never used, because here
tempobjectPatient = objectPatient;
you immediately assign the old one to tempobjectPatient. So after this, you don't have two array instances. Both tempobjectPatient and objectPatient refer to the same instance.
You probably want:
Patient[] tempobjectPatient = (Patient[])objectPatient.Clone();
Replace the Array with a Dictionary, it is desigend to be used with unique keys:
Dictionary<int,Patient> patients = new Dictionary<int,Patient>();
while (true)
{
Console.Write("\nEnter Patient Account Number: ");
int number = Convert.ToInt32(Console.ReadLine());
if (patients.ContainsKey(number))
{
Console.Write("\nSorry, Patient Account Number {0} is a duplicate.", number);
Console.Write("\nPlease re-enter the Patient Account Number: ");
continue;
}
Console.Write("***Patient Account Number unique and accepted***\n");
Console.Write("Enter the Patient Name: ");
string name = Console.ReadLine();
Console.Write("Enter the Patient Age: ");
int age = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the total annual Patient amount due: ");
double amountDue = Convert.ToDouble(Console.ReadLine());
patients.Add(number, new Patient(number, name, age, amountDue));
}
The loop is now missing an exit condition.
EDIT:
While this does not answer the question of the title i think it is what the OP was looking for.
Related
I am creating a console application that is using user input to determine the data within two lists. Each list has the same data objects. They are name, rollNo, course and stream.
When all of the data has been inputted by the user then they are asked which stream information they would like to see. I then want to have the data that correlates with the stream input be displayed. For example if they want to see stream 1 then the information for the trainer and students on that stream should appear in the console.
This is my code below.
Student studentOne = new Student();
Console.WriteLine("Enter the Name of Student:");
studentOne.name = Console.ReadLine();
Console.WriteLine("Enter the roll number of Student:");
studentOne.rollNo = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the course of Student:");
studentOne.course = Console.ReadLine();
Console.WriteLine("Enter the Stream of Student:");
studentOne.stream = int.Parse(Console.ReadLine());
Student studentTwo = new Student();
Console.WriteLine("Enter the Name of Student:");
studentTwo.name = Console.ReadLine();
Console.WriteLine("Enter the roll number of Student:");
studentTwo.rollNo = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the course of Student:");
studentTwo.course = Console.ReadLine();
Console.WriteLine("Enter the Stream of Student:");
studentTwo.stream = int.Parse(Console.ReadLine());
Student studentThree = new Student();
Console.WriteLine("Enter the Name of Student:");
studentThree.name = Console.ReadLine();
Console.WriteLine("Enter the roll number of Student:");
studentThree.rollNo = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the course of Student:");
studentThree.course = Console.ReadLine();
Console.WriteLine("Enter the Stream of Student:");
studentThree.stream = int.Parse(Console.ReadLine());
var studentData = new List<Student>();
studentData.Add(studentOne);
studentData.Add(studentTwo);
studentData.Add(studentThree);
Trainer trainerOne = new Trainer();
Console.WriteLine("Enter the Name of the trainer:");
trainerOne.name = Console.ReadLine();
Console.WriteLine("Enter the roll number of the trainer:");
trainerOne.trainNo = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the course of the trainer:");
trainerOne.course = Console.ReadLine();
Console.WriteLine("Enter the Stream of the trainer:");
trainerOne.stream = int.Parse(Console.ReadLine());
Trainer trainerTwo = new Trainer();
Console.WriteLine("Enter the Name of the trainer:");
trainerTwo.name = Console.ReadLine();
Console.WriteLine("Enter the roll number of the trainer:");
trainerTwo.trainNo = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the course of the trainer:");
trainerTwo.course = Console.ReadLine();
Console.WriteLine("Enter the Stream of the trainer:");
trainerTwo.stream = int.Parse(Console.ReadLine());
Trainer trainerThree = new Trainer();
Console.WriteLine("Enter the Name of the trainer:");
trainerThree.name = Console.ReadLine();
Console.WriteLine("Enter the roll number of the trainer:");
trainerThree.trainNo = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the course of the trainer:");
trainerThree.course = Console.ReadLine();
Console.WriteLine("Enter the Stream of the trainer:");
trainerThree.stream = int.Parse(Console.ReadLine());
List<Trainer> trainerData = new List<Trainer>();
trainerData.Add(trainerOne);
trainerData.Add(trainerTwo);
trainerData.Add(trainerThree);
Console.WriteLine("Which stream details do you want to view?");
var streamInfo = int.Parse(Console.ReadLine());
if (streamInfo == 1)
{
foreach (var stream in trainerData)
{
}
}
class Student
{
public string name { get; set; }
public int rollNo { get; set; }
public string course { get; set; }
public int stream { get; set; }
}
class Trainer
{
public string name { get; set; }
public int trainNo { get; set; }
public string course { get; set; }
public int stream { get; set; }
}
Any help is appreciated.
Edit: I didn't make clear what I'm asking for. I'm not sure how to have the correct data show depending on what the stream input is. I have tried using an if statement and foreach statement but I don't know if this is the best approach.
Let's decompose the solution, let's start from reading integers:
private static int ReadInteger(string title, Func<int, bool> isValid = null) {
if (isValid is null)
isValid = x => true;
while (true) {
if (!string.IsNullOrWhiteSpace(title))
Console.WriteLine(title);
if (int.TryParse(Console.ReadLine(), out int result) && isValid(result))
return result;
Console.WriteLine("Invalid value, please, try again.");
}
}
private static string ReadString(string title, Func<string, bool> isValid = null) {
if (isValid is null)
isValid = x => true;
while (true) {
if (!string.IsNullOrWhiteSpace(title))
Console.WriteLine(title);
string result = Console.ReadLine();
if (isValid(result))
return result;
Console.WriteLine("Invalid value, please, try again.");
}
}
Then we can implement ReadStudent and ReadTrainer:
public static Student ReadStudent() {
return new Student() {
name = ReadString("Enter the Name of Student:", x => !string.IsNullOrWhiteSpace(x)),
rollNo = ReadInteger("Enter the roll number of Student:", x => x >= 0),
course = ReadString("Enter the course of Student:", x => !string.IsNullOrWhiteSpace(x)),
stream = ReadInteger("Enter the Stream of Student:", x => x >= 0),
};
}
public static Trainer ReadTrainer() {
return new Trainer() {
name = ReadString("Enter the Name of trainer:", x => !string.IsNullOrWhiteSpace(x)),
rollNo = ReadInteger("Enter the roll number of trainer:", x => x >= 0),
course = ReadString("Enter the course of trainer:", x => !string.IsNullOrWhiteSpace(x)),
stream = ReadInteger("Enter the Stream of trainer:", x => x >= 0),
};
}
Finally, we can create as many students and trainers as we want (note for loop - what if we want to enter 100 students?):
int students = 3;
int trainers = 3;
var studentData = new List<Student>(students);
for (int i = 0; i < students; ++i)
studentData.Add(ReadStudent());
var trainerData = new List<Trainer>(trainers);
for (int i = 0; i < trainers; ++i)
trainerData.Add(ReadTrainer());
Finally, to query the list by stream you can use Linq or good old loop or Linq:
int stream = ReadInteger("Which stream details do you want to view?");
foreach (Student s in studentData)
if (s.stream == stream)
Console.WriteLine($"Student {s.name} Roll No {s.rollNo}");
foreach (Trainer t in trainerData)
if (t.stream == stream)
Console.WriteLine($"Trainer {t.name} Roll No {t.rollNo}");
I am trying to create a program that takes user input for the amount of students and then with that in mind prompt for 5 grades and an average per student.
An example of a student record would be as follows:
Student #...... Grade1..... Grade2..... Grade3..... Grade4..... Grade5..... Average
Code so far (Probably not correct in anyway):
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Question_Two
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Input the number of students: ");
int n = int.Parse(Console.ReadLine());
Student[] students = new Student[6];
string studentId = "Student 1"; //error here
students[0].addGrade(double.Parse(studentId)); // or error here
double value;
value = double.Parse(Console.ReadLine());
students[1].addGrade(value);
value = double.Parse(Console.ReadLine());
students[2].addGrade(value);
value = double.Parse(Console.ReadLine());
students[3].addGrade(value);
value = double.Parse(Console.ReadLine());
students[4].addGrade(value);
value = double.Parse(Console.ReadLine());
students[5].addGrade(value);
students[6].getAverage();
}
public class Student
{
private ArrayList grades;
public Student()
{
grades = new ArrayList();
}
public void addGrade(double val)
{
grades.Add(val);
}
public double getAverage()
{
double avg = 0;
for (int i = 0; i < grades.Count; i++)
{
avg += (double)grades[i];
}
avg /= grades.Count;
return avg;
}
}
}
}
I am not sure where to go from here as I have an error when storing the name into the array.
Creating an array with new does not create each individual element (except primitives like int). You need to call new for every item in the array:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Input the number of students: ");
int n = int.Parse(Console.ReadLine());
// Create array of size n (instead of 6)
// Array is created but individual Students are not created yet!
Student[] students = new Student[n];
// Use a loop to get all student info
for (int s = 0; s < n; s++) {
string studentId = $"Student{s+1}";
// Important! Need to create a student object
students[s] = new Student(studentId);
// Use another loop to get grades
for (int g = 0; g < 5; g++) {
double grade = double.Parse(Console.ReadLine());
students[s].addGrade(grade);
}
// Print average
Console.WriteLine($"{students[s].id} average = {students[s].getAverage()}");
}
}
Also, modify the Student class to have an id
public class Student
{
private ArrayList grades;
public string id;
// Accepts one parameter : an id
public Student(string studentId)
{
id = studentId;
grades = new ArrayList();
}
public void addGrade(double val)
{
grades.Add(val);
}
public double getAverage()
{
double avg = 0;
for (int i = 0; i < grades.Count; i++)
{
avg += (double)grades[i];
}
avg /= grades.Count;
return avg;
}
}
}
You can not parse a string "Student 1" to double like in the following code:
string studentId = "Student 1"; //error here
students[0].addGrade(double.Parse(studentId));
I don't know what you want to the result to be. But make sure that the student id is a numeric value (can be in string form). You can use the following code to get what you want.
Console.WriteLine("Input the number of students: ");
int numberOfStudents = int.Parse(Console.ReadLine());
Student student = new Student();
string studentId = "1";
student.addGrade(double.Parse(studentId));
double value;
value = double.Parse(Console.ReadLine());
student.addGrade(value);
value = double.Parse(Console.ReadLine());
student.addGrade(value);
value = double.Parse(Console.ReadLine());
student.addGrade(value);
value = double.Parse(Console.ReadLine());
student.addGrade(value);
value = double.Parse(Console.ReadLine());
student.addGrade(value);
double avarageNumber = student.getAverage();
class Myclass
{
public string Driver1()
{
string a = "";
Console.Write("Please enter drivers name: ");
a = Console.ReadLine();
return a;
}
public int numberOfTrips()
{
int a = 0;
{
Console.Write("Enter the number of trips: ");
a = Convert.ToInt32(Console.ReadLine());
}
return a;
}
public List<float> Payments()
{
List<float> a = new List<float>();
float input;
for (int i = 0; i<numberOfTrips(); i++)
{
Console.Write("Enter payment {0}: ", (1 + i));
input = float.Parse(Console.ReadLine());
Console.WriteLine("Payment added");
a.Add(input);
}
return a;
}
}
class Program
{
static void Main(string[] args)
{
Myclass a = new Myclass();
string name = a.Driver1();
int trip = a.numberOfTrips();
float total = a.Payments().Sum();
Console.WriteLine("\nDriver: {0}\n" + "Number of trips: {1}\n" + "Total payment: {2}\n", name, trip, total);
}
}
The issue i am having is that the "public int numberOfTrips()" method is running twice before it gets to the method containing the for loop. I think this is to do with the fact i am using it within the for loop to specify when the loop should stop. I am guessing i have done this wrong so how would i correct this? I need the user to be able to set the how many times it will ask for payment.
Any help is appreciated.
Just pass the number from numberOfTrips as a parameter to Payments:
public List<float> Payments(int tripCount)
{
List<float> a = new List<float>();
float input;
for (int i = 0; i < tripCount; i++)
{
Console.Write("Enter payment {0}: ", (1 + i));
input = float.Parse(Console.ReadLine());
Console.WriteLine("Payment added");
a.Add(input);
}
return a;
}
In your Main method:
Myclass a = new Myclass();
string name = a.Driver1();
int trip = a.numberOfTrips();
float total = a.Payments(trip).Sum();
Instead of calling numberOfTrips() in Main() as well as in Payments() you might try creating an instance variable or static variable in MyClass. Then you can fetch the number of trips from that variable after all payments are calculated.
That is correct. The first time it runs is in Main to set the 'trip' variable. The second time it runs is in Payments, inside the for loop declaration.
I'm trying to figure out how to match a candidate name with candidate votes and display the highest vote along with the candidate name.
As in how to match the two arrays I have.
I know I'm missing something but what? I've only started learing C# at home.
namespace NumberOfVotes
{
class Program
{
static void Main(string[] args)
{
int size, minVotes;
int[] numOfCandidates;
int[] numOfVotes;
double avgMarks;
string[] candidateName;
Console.WriteLine("Enter number of candidates");
size = int.Parse(Console.ReadLine());
numOfCandidates = new int[size];
candidateName = new string[size];
numOfVotes = new int[size];
for (int i = 0; i < numOfCandidates.Length; i++)
{
Console.WriteLine("Enter a Candidate Name");
candidateName[i] = Console.ReadLine();
Console.WriteLine("Enter number of votes thus far");
numOfVotes[i] = int.Parse(Console.ReadLine());
}
int max = numOfVotes.Max();
avgMarks = numOfVotes.Average();
minVotes = numOfVotes.Min();
Console.WriteLine("Average votes: {0}", avgMarks);
Console.WriteLine("Min number of votes is: {0}", minVotes);
}
}
}
See these kind of things you can do with thinking about it with your head. StackOverflow isn't a website to post your problem if you're stuck, only if the problem you have needs a solution which can help other people.
This would work(most straightfoward approach to me):
int maxIndex = -1;
int max = -1;
for (int i = 0; i < numOfCandidates.Length; i++)
{
Console.WriteLine("Enter a Candidate Name");
candidateName[i] = Console.ReadLine();
Console.WriteLine("Enter number of votes thus far");
int num = int.Parse(Console.ReadLine()); // <-- unsafe
// just check it every time, if the number is greater than the previous maximum, update it.
if (num > max)
{
max = num;
maxIndex = i;
}
numOfVotes[i] = num;
}
Console.WriteLine("Candidate {0}, with {1} votes, has the most votes", candidateName[maxIndex], max);
However, if you want more things to calculate (like who has the least votes) without doing these kind of things, you should use a Dictionary<string, int>. That's a string associated with a number, a name associated with votes.
(More info about that here: http://www.dotnetperls.com/dictionary)
You should use a Dictionary for this:
static void Main(string[] args)
{
var candidates = new Dictionary<string, int>();
Console.WriteLine("Enter number of candidates");
var size = int.Parse(Console.ReadLine());
for (int i = 0; i < size; i++)
{
Console.WriteLine("Enter a Candidate Name");
var name = Console.ReadLine();
Console.WriteLine("Enter number of votes thus far");
var votes = int.Parse(Console.ReadLine());
candidates.Add(name, votes);
}
Console.WriteLine("Average votes: {0}", candidates.Average(entry => entry.Value));
Console.WriteLine("Min number of votes is: {0}", candidates.Min(entry => entry.Value));
}
You should probably use dictionary instead, but if you want to use array, here's how to do it :
avgMarks = numOfVotes.Average();
int avgIndex = numOfVotes.ToList().IndexOf(avgMarks);
Console.WriteLine("Average votes: {0} Candidate Names: {1}", avgMarks, candidateName[avgIndex]);
Your code just works fine. What you are looking for is the index of the array having highest number of votes. This index will be also useful to get the candidateName having highest number of vote. So, to get that index simply use the maximum value you got from this line :
int max = numOfVotes.Max();
and then use IndexOf static method to find the index of max in your array. For that try this line of code :
int index = Array.IndexOf<int>(numOfVotes, max);
Now simply print out the candidateName and highest number of votes as below :
Console.WriteLine(candidateName[index] + " has highest number of vote : " + numOfVotes[index] );
You can have a clean conception about Array.IndexOf() from DotNetPerls and MSDN
The solution to the problem, as it is, is to do something like this:
int indexOfWinner = Array.IndexOf(numOfVotes, numOfVotes.Max());
string winner = candidateName[indexOfWinner];
I don't know how far along in your C# and programming education you are (OOP and suchlike), so a couple of points you may find obvious or not:
you should use generic collections and not primitive arrays (List<> in this case).
you should encapsulate all this into a class.
This is how I would do it:
class Program
{
static void Main(string[] args) {
Candidates c = new Candidates("foo", "bar", "baz");
Random rand = new Random();
c.addVote(0, rand.Next(100));
c.addVote(1, rand.Next(100));
c.addVote(2, rand.Next(100));
Console.WriteLine(c.getWinner());
Console.WriteLine("number of votes:");
Console.WriteLine(c[0] + ": " + c[0].numberOfVotes);
Console.WriteLine(c[1] + ": " + c[1].numberOfVotes);
Console.WriteLine(c[2] + ": " + c[2].numberOfVotes);
}
}
class Candidates
{
private List<Candidate> candidates;
public Candidate this[string name] {
get {
return candidates.First(v => v.name == name);
}
}
public Candidate this[int index] {
get {
return candidates[index];
}
}
public Candidates(string firstCandidate, params string[] candidates) { //this is done to disable an empty constructor call
//and also allow multiple candidates
this.candidates = new List<Candidate>(candidates.Length);
this.candidates.Add(new Candidate(firstCandidate));
foreach(var c in candidates) {
this.candidates.Add(new Candidate(c));
}
}
public void addVote(int candidateNumber, int numberOfVotes = 1) {
candidates[candidateNumber].numberOfVotes += numberOfVotes;
}
public Candidate getWinner() {
candidates.Sort((candidate1, candidate2) => candidate2.numberOfVotes.CompareTo(candidate1.numberOfVotes));
return candidates[0];
}
}
class Candidate
{
public string name { get; private set; }
public int numberOfVotes { get; set; }
public Candidate(string name) {
this.name = name;
this.numberOfVotes = 0;
}
public override string ToString() {
return name;
}
}
Can someone tell me why this doesnt work. When I enter the loop it prints everything instead of one line and get the users input. It prints Enter the integer the account numberEnter the integer the account balanceEnter the account holder lastname
Got it working thanks everyone, but now the searchaccounts doesnt work
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class accounts
{
private int[] accountnum = new int[5]; //private accountnum array of five integer account numbers
private int[] accountbal = new int[5];//private balance array of five balance amounts
private string[] accountname = new string[5];//private accntname array to hold five last names
public void fillAccounts()
{
int bal;
int accountnumber;
string name;
for (int x = 0; x < 5; ++x)
{
Console.Write("Enter the integer the account number");
accountnumber = Console.Read();
Console.Write("Enter the integer the account balance");
bal = Console.Read();
Console.Write("Enter the account holder lastname");
name = Console.ReadLine();
accountnum[x] = accountnumber;
accountbal[x] = bal;
accountname[x] = name;
}
}
public void searchAccounts()
{
Console.WriteLine("Enter the account number");
int acctnum = Console.Read();
for (int x = 0; x < 6; ++x)
{
if (x < 5)
{
if (accountnum[x] == acctnum)
{
Console.WriteLine("Account #{0} has a balance of {1} for customer {2}", acctnum, accountbal[x].ToString("C"), accountname[x]);
break;
}
}
else
{
Console.WriteLine("You entered invalid account number");
}
}
}
public void averageAccounts()
{
int sum = 0;
int avg;
for (int x = 0; x < 5; ++x)
{
sum = accountbal[x] + sum;
}
avg = sum / 5;
Console.WriteLine("The average dollar amount is {0}", avg.ToString("c"));
}
}
class assignment3_alt
{
static void Main(string[] args)
{
accounts myclass = new accounts();
string userin;
myclass.fillAccounts();
int i = 0;
while (i != 1)
{//use the following menu:
Console.WriteLine("*****************************************");
Console.WriteLine("enter an a or A to search account numbers");
Console.WriteLine("enter a b or B to average the accounts");
Console.WriteLine("enter an x or X to exit program");
Console.WriteLine("*****************************************");
Console.Write("Enter option-->");
userin = Console.ReadLine();
if (userin == "a" || userin == "A")
{
myclass.searchAccounts();
}
else if (userin == "b" || userin == "B")
{
myclass.averageAccounts();
}
else if (userin == "x" || userin == "X")
{
break;
}
else
{
Console.WriteLine("You entered an invalid option");
}
}
}
}
}
Console.Read only reads a single character. You need to use Console.ReadLine.
Console.Write("Enter the integer the account number");
accountnumber = int.Parse(Console.ReadLine());
Console.Write("Enter the integer the account balance");
bal = int.Parse(Console.ReadLine());
Console.Write("Enter the account holder lastname");
name = Console.ReadLine();
You might also want to consider using int.TryParse instead of int.Parse so that you can better handle invalid input.
For your new question it is the same error. Just replace:
int acctnum = Console.Read();
with
int acctnum = int.Parse(Console.ReadLine());
or (preferably)
int acctnum;
if (!int.TryParse(Console.ReadLine(), out acctnum))
{
Console.WriteLine("You need to enter a number");
return;
}
The first will fail if the user doesn't enter a valid integer the second will print out a nice error message and return to the loop.
This is not an answer, as other have already put up some good ones. These are just a few tips about your code.
Instead of looping with while (i != 1), never changing the value of i and terminating the loop with break, it would be better to use a do-while loop, like this:
do
{
// blah blah
} while(userin != "x" || userin != "X")
It is less confusing than having a variable with no use at all.