I'm trying to fill 3 arrays with user input, it should look like this:
Enter account number 1: 29384
Enter the account balance: 1111
Enter the account holder last name: lastname
Enter account number 2: 34938
Enter the account balance: 2222
Enter the account holder last name: lastname2
Enter account number 3: 46372
Enter the account balance: 3333
Enter the account holder last name: lastname3
and so on...
I have my program set up with for loops to fill a 5 line array but it asks for all five account numbers first then goes onto the balance and so on
using System;
public class Array1
{
public static void Main()
{
int[] scores = new int[5];
int x;
string inputString;
int[] balance = new int[5];
int y;
for(x=0; x < scores.Length; ++x)
{
Console.Write("Enter account number {0} ", x + 1);
inputString = Console.ReadLine();
scores[x] = Convert.ToInt32(inputString);
}
for(y=0; y < balance.Length; ++y)
{
Console.Write("Enter the account balance ");
inputString = Console.ReadLine();
}
}
}
I'm sure there is a much better way to write this. Any help would be appreciated.
Yes.
Why not have a class with a structure
class DataHolder
{
public String account_number;
public int balance;
public String lastname;
}
Then you're at least holding relevant data together
Your code then does as you've instructed, its asked to get all the accounts, then all the balances..
Where as 1 loop would have been sufficient
If you had an array of DataHolder class
DataHolder[] testData = new DataHolder[5];
For (int x=0; < x testData.length; x++)
{
Console.Write("Account Number");
testData[x].account_number= Console.ReadLine();
Console.Write("Balance");
testData[x].balance=Convert.ToInt32(Console.ReadLine());
Console.Write("LastName");
testData[x].lastname=Console.ReadLine();
}
I think you just want one for loop.
for(x=0; x < scores.Length; ++x)
{
Console.Write("Enter account number {0} ", x + 1);
inputString = Console.ReadLine();
scores[x] = Convert.ToInt32(inputString);
Console.Write("Enter the account balance ");
inputString = Console.ReadLine();
// and so on
}
Related
namespace A3PFJBJLP1
{
class Program
{
static void Main(string[] args)
{
// option 1 by parker farewell
Console.WriteLine("~ option 1 ~");
Console.WriteLine("please input a number: ");
for (int StartingNum = Convert.ToInt32(Console.ReadLine()); StartingNum < 20; StartingNum++)
{
Console.WriteLine(StartingNum);
}
Console.ReadLine();
}
}
}
So far this is the code I've tried and I can display the numbers in a list, but only if the number is 20 or less when I need to make a list that displays 20 whole numbers that come after the number inputted by the user
You were close. If you start at a number userInput, you want to display 20 numbers after it you could update the loop as follows.
int userInput = Convert.ToInt32(Console.ReadLine()); // get and store user input
for (int startingNum = userInput; startingNum < userInput + 20; startingNum++)
{
Console.WriteLine(startingNum + 1); // display number after startingNum
}
Having problem with my work i want to write a program that does the sum and average the student grade in the average function i used a do while loop since i want anyone to enter grade until the user enter -1 the loop end.
The problem is i do not want to run the _cal = Int32.Parse(Console.ReadLine()); in the fucntion instead run it in the main by passing a value to Average() parameter then using the value in main as a console.readlIne because i will use the user input to divide by the sum in order to get average am new to programming.
public static void Main()
{
Console.WriteLine("Please Enter the scores of your student: ");
Console.WriteLine("----------------------------------------");
_studentTotalGrade = Average();
Console.WriteLine("sum " + Sum);
Console.ReadLine();
}
public static double Average()
{
double _cal,_sumTotal = 0;
int i = 0;
do
{
_cal = Int32.Parse(Console.ReadLine());
if (_cal == -1)
{
break;
}
if (_cal > 20)
{
Console.WriteLine("Adjust score\");
continue;
}
_sumTotal +=_cal;
i--;
} while (_cal > i);
return _sumTotal;
}
}
I am new to programming and I would be delighted if someone could help me with the following question: "Write a program that ask for the class and number of students. after asking for the name of student, ask for the grades by the Name of student, show average and show highest grade". I am stuck at part 3, and can't the link the names to next part. This is what I got so far:
static void Main(string[] args)
{
// 1 give a name and number of students
Console.Write("Class = ");
Convert.ToString(Console.ReadLine());
Console.Write("Number of students = ");
int aantalStudenten = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
// 2 ask for the names
int[] aantal = new int[aantalStudenten];
Random RandomNumber = new Random();
for (int i = 0; i < aantalStudenten; i++)
{
Console.Write("Geef naam van de {0}e student : ", i + 1);
string studentNaam = Convert.ToString(Console.ReadLine());
}
Console.WriteLine();
// 3 give the grade of each student by the name
for (int j = 0; j < aantalStudenten; j++)
{
Console.Write("Cijfer van {0} : "); // here i need the students name)//
int cijfers = Convert.ToInt32(Console.ReadLine());
}
//class avarege here
//highest grade of the class
int hoogste = stuCijfer.Max();
Console.WriteLine("De hoogste cijfer is {}," hoogste;
//name and grade of each student
Console.WriteLine();
Console.WriteLine("Press any key to stop");
Console.ReadKey();
}
Console.Write("Class = ");
Convert.ToString(Console.ReadLine());
Console.Write("Number of students = ");
int aantalStudenten = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
// 2 ask for the names
string[] namen = new string[aantalStudenten];
int[] cijfers = new int[aantalStudenten];
Random RandomNumber = new Random();
for (int i = 0; i < aantalStudenten; i++){
Console.Write("Geef naam van de {0}e student : ", i + 1);
namen[i] = Convert.ToString(Console.ReadLine());
}
Console.WriteLine();
// 3 give the grade of each student by the name
for (int i = 0; i < aantalStudenten; i++){
Console.Write("Cijfer van {0} : ", namen[i]); // here i need the students name)//
cijfers[i] = Convert.ToInt32(Console.ReadLine());
}
double gemiddeld = 0;
//class avarege here
for (int i = 0; i < cijfers.Count(); i++) {
gemiddeld += cijfers[i];
}
gemiddeld = gemiddeld / cijfers.Count();
Console.WriteLine("Het gemiddelde van de klas is: {0}", gemiddeld);
//highest grade of the class
int hoogste = 0;
for (int i = 0; i < cijfers.Count(); i++) {
if (cijfers[i] > hoogste) {
hoogste = cijfers[i];
}
}
Console.WriteLine("De hoogste cijfer is {0}",hoogste);
//name and grade of each student
Console.WriteLine();
Console.WriteLine("Press any key to stop");
Console.ReadKey();
You should put the names of the students in a list the way you are working now you are overwriting each time the user enters a new name. and then u can loop over the names storing the score in an array in this case u can use two separate one's but it would be better to use a dictionary or a 2D array.
but good luck further
It looks like your trying to get a string where you're picking out the name but it might have been converted to an int. Change the part where you converted it to a int when you received the name.
It also looks like you have for int j. Before you named it int i. Make sure it references back to the exact same name for what you identified each time.
on a side note you could try to store the names into an array and pull them out be referencing the array. That might help.
If I were to code such a program properly I would create a new Student class to contain a name and a score, then create a list or array to contain multiple instances of Student. Hopefully you're familiar with the concepts of creating new classes, and with lists or arrays. If you need help understanding any of those, let me know.
I can't figure out how to split my array correctly so that I can pass those two separate arrays to multiple different methods. I also would like to end the array at any point without the program giving the user an error. Can anyone give me some direction on what to do and help me fix my program?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace proj09LEA
{
class Program
{
// declare a constant integer
const int MAX = 10;
static void Main(string[] args)
{
// declare and array of integers
int[] array = new int[MAX];
Console.WriteLine("\nSaturday Coder's Bowling Team");
Console.WriteLine("Enter in a name and score for each person on the team.");
Console.WriteLine("For example, Mary 143. Just hit Enter when you are done.\n");
// fill an array with user input
for (int i = 0; i < array.Length; i++)
{
// ask the user to enter some data
Console.WriteLine("Enter in a name and score: ");
// the line of data input by the user is stored here
string userInput = Console.ReadLine();
// userInput is split into two pieces, which are stored in this array
string[] parsedInput;
// this line splits the string userInput into two pieces
parsedInput = userInput.Split();
// store the first piece, the name, in a string array
string names = parsedInput[0];
// store the second piece, a score, in an integer variable
int scores = int.Parse(parsedInput[1]);
array[i] = scores;
}
Console.WriteLine("------------ Input Complete ------------\n");
Console.WriteLine("Here are the scores for this game:");
// display the scores for each person from method DisplayScores
DisplayScore(array);
// display the highest score and name of player from method HighScore
HighScore(array);
// display the lowest score and name of player from method LowScore
LowScore(array);
// display the average score from method AverageScore
AverageScore(array);
Console.WriteLine("Press Enter to continue. . .");
Console.ReadLine();
}
static void DisplayScore(int[] array)
{
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine("{0}'s score was {0}.\n", array[i]);
}
}
static void HighScore(int[] array)
{
int max = array.Max();
Console.WriteLine("Congratulations {0}, your score of {0} was the highest.", max);
}
static void LowScore(int[] array)
{
int min = array.Min();
Console.WriteLine("{0}, your score of {0} was the lowest. Better get some practice.", min);
}
static void AverageScore(int[] array)
{
int sum = array.Sum();
int average = sum / array.Length;
Console.WriteLine("The average score for this game was {0:d}.", average);
}
}
}
If you willing to input inputs one by one first name then after scores one by one
Console.WriteLine("Enter in a name: ");
string userInput = Console.ReadLine();
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine("Enter in a score "+i+": ");
string score = Console.ReadLine();
// store the second piece, a score, in an integer variable
int scores = int.Parse(score);
array[i] = scores;
}
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.