tags:

views:

143

answers:

3

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");
                }
            }
        }
    }
}
+5  A: 

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.

Mark Byers
A: 

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.

Martinho Fernandes
+1  A: 

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.

James J. Regan IV