• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1114

# Nested loop in C# ---- Console Application

Hi Experts!

How  would I change this code to display the balances  for three people A,B,C? ask the for their deposits, but it needs to loop until the N is entered by the user, calculate the interest rate of 10%,  and finally  total the balance plus the interest rate. for A,B, or C.... then exit

``````using System;
public class LoopingBankBal
{
public static void Main()
{
double bankBal = 1000;
const double INT_RATE = 0.04;
string inputString;
char response;
Console.Write("Do you want to see your balance? Y or N ...");
response = Convert.ToChar(inputString);
while(response == 'Y')
{
Console.WriteLine("Bank balance is {0}", bankBal.ToString("C"));
bankBal = bankBal + bankBal * INT_RATE;
Console.Write("Do you want to see next yearâ€™s balance? Y or N ...");
response = Convert.ToChar(inputString);
}
Console.WriteLine("Have a nice day!");
}
}
``````
0
December2000
• 6
• 5
• 3
2 Solutions

Commented:
I'm a bit confused with what's required.

BTW, is it an assignment? :)

The current code is wrong. It will show the same value every time you enter "Y". The current code never asks to enter any deposit amounts...
0

Commented:
So I don't want to just give you the code in case this is a homework assignment.  But I can try to answer your questions:

The first thing I would do is create a method that collected the deposit information for a single person and I would call that method from Main 3 times, once for each person.  For discussion purposes I'll call it CollectDeposits and it could return a Decimal type value.  The loop until N is entered would be inside the CollectDeposits method.

So after we called CollectDeposits 3 times, we would have a 3 decimal values called a, b, and c.

Then you multiply each value by .1 to calculate the interest, lets call those 3 values interestA,interestB,interestC. Then you add interestA to a, interestB to b, etc.  Then you WriteLine the values for each user.

0

Author Commented:
Thanks @ anarki_jimbel I worked on it and got this so far but, it is not elegant and I want to use a string N instead of the 0.... So, I figured it out pretty much.... just thought it was a better way to do it.. usually asking  the question helps me go in the right direction
`````` using System;
public class TotalDeposits
{
public static void Main()
{
double purchase;
double total = 0;
string inputString;
string inputName;
const double QUIT = 0;

Console.WriteLine( "Enter Accont Name a, b, c");

Console.WriteLine("Enter deposit amount ");
purchase = Convert.ToDouble(inputString);
while (purchase != QUIT)
{
total += purchase * (1.1); // INTEREST
Console.WriteLine("Enter next deposi amount, or " +
QUIT + " to quit ");
purchase = Convert.ToDouble(inputString);
}
Console.WriteLine("{1} balance is {0}", total.ToString("C"), inputName);
}
}
``````
0

Commented:
Here is how to call a method (please forgive me if you already know).

inside Main:  Decimal a = CollectDeposit();

private static Decimal CollectDeposit()
{
// get the string, convert it to a decimal and return it.
}

One (of many) ways  to convert a string to a decimal is with Decimal.TryParse() which returns a false if the conversion fails.

For example:

Decimal deposit;
if (Decimal.TryParse(enteredStringValue, out deposit))
{
// deposit now contains a valid decimal
}
else
{
}

Note that I didn't actually compile this but it should be very close.
0

Commented:
Also, it might look like extra work to create a separate method for a small piece of the problem,  but if you do it you will find that it will make it much easier for you to get the program working correctly.
0

Commented:
I agree to MajorBigDeal that moving some code to separate methods makes code cleaner.

But in whole your code is pretty OK. However, you may run your loop as many times as you want. And basically, for any name, not just A,B and C. Is that what you want?
If you need to restrict account names, you may have an array or List with account names (hard-coded)  and every time user enters an account name you check if it is valid (i.e., it exists in the array.
0

Author Commented:
Now that I think of it... when the program QUIT's it should print the total for A, B, and C... the way it is it will only return results for one at a time. I need to reconstruct the loop or should I use a case statement for the A,B, and C?
0

Commented:
Hmmm, I'd probably use Dictionary class, something like Dictionary<string, double>,
where key is an account name (a,b or c) and the value is the account amount. Initially the dictionary have all 3 users with 0 value.

Are you allowed to use this class or any other collections?

We run a loop like for each user in dictionary keys (pseudo code):

for each string user in usersdictionary.keys
{
print user name
save amount to the dictionary:
usersdictionary[user]=amount
//do not display anything her
}

Because we have three users - the above loop runs 3 times.

After it is done - run a similar loop again and calculate and print all required data
0

Author Commented:
Never used dictionary looks cool! How do you code to save usersdictionary[user]=amount?

`````` Console.WriteLine("Enter deposit amount ");
purchase = Convert.ToDouble(inputString);
while (purchase != QUIT)
{
total += purchase * (1.1); // INTEREST
Console.WriteLine("Enter next deposi amount, or " +
QUIT + " to quit ");
purchase = Convert.ToDouble(inputString);
}
``````

As a method?
0

Author Commented:
Use it as a class?
``````Dictionary<string, string> KeyValue = new Dictionary<string, string>();

foreach (KeyValuePair<string, string> item in KeyValue) {
Console.WriteLine("{0} = {1}", item.Key, item.Value);
}
``````

This is totally cool but, I think dictionary is above my head .... Oh well back to the drawing board.
0

Commented:
Dictionary is simple. Let me a bit time to give you an example :)
0

Commented:
OK, look at this example. I show here how to use a Dictionary. By the way, Lists and Dictionaries are very extensively used

``````    class Program
{
static void Main(string[] args)
{

// cretae a dictionary where key is a account name and value - account amount
// amout is initially zero
Dictionary<string, double> accounts = new Dictionary<string, double>();

// checked what we have
foreach (KeyValuePair<string, double> item in accounts)
{
Console.WriteLine("Account {0}, amount = {1}", item.Key, item.Value);
}
// enter new amounts:
// We cannot modify the dictionary in foreach loop
// Therefore copy keys to a list:
List<string> accountNames = new List<string>(accounts.Keys);
foreach(string accountName in accountNames)
{

Console.WriteLine(" Please enter new amount for the Account {0}", accountName);
}

// check again
foreach (KeyValuePair<string, double> item in accounts)
{
Console.WriteLine("Account {0}, amount = {1}", item.Key, item.Value);
}

}
}
``````
0

Author Commented:
Thank you! Sort of lost as to where I place the loop with a dictionary...  Meaning every account should be able to loop until all the deposits are complete and the accounts are totaled at the end.  This whole concept is cool though....
0

Author Commented:
Thank you!
0

## Featured Post

• 6
• 5
• 3
Tackle projects and never again get stuck behind a technical roadblock.