hello

I wrote a program where I can add the items and if it's more than 250 Euro, 10% discount will be given.

I've used the "if-statement" and I just started only with two numbers. Now, I would like to add as many as I can, is there a way and I would like that the program will stop if there's nothing to add more.

I've researched in Google but it seems I can't find topics on how to deal with it, maybe you guys have an idea. It will be great appreciated.

Thanks, below is my code I made:

This is what I've done:

double firstValue;
double secondValue;
double theSumValue;


Console.WriteLine("The price of the first item:");
firstPrice = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("The price of the second item:");
secondPrice = Convert.ToDouble(Console.ReadLine());

theSumValue = firstPrice + secondPrice;

if(theSumValue >= 250)
theSumValue *= 0.9;

Console.WriteLine("The final value in Euro is " + theSumValue);

use a while loop to get values.Then use an array list to store those values.use another loop to get the sum.your problem will be solved.

Your code, as you posted it will not work.
Try and get a look at this:

using System;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //An ArrayList can store any number of items via the Add method
            ArrayList Prices = new ArrayList();

            //initialize the total price
            double theSumValue = 0.0;

            //Enter prices and store them in an ArrayList
            Console.WriteLine("Enter the number of prices:");
            int NumberOfPrices = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < NumberOfPrices; i++)
            {
                Console.WriteLine("Enter the price of item {0}:", i+1);
                Prices.Add(Convert.ToDouble(Console.ReadLine()));
            }

            //Calculate sum
            foreach (double d in Prices)
            {
                theSumValue += d;
            }

            //Add reduction if needed
            if (theSumValue >= 250.0)
                theSumValue *= 0.9;

            Console.WriteLine("The final value in Euro is {0} ." ,theSumValue); 

            //Keep the console on screen
            Console.WriteLine("Press any key to quit.");
            Console.ReadKey();
        }
    }
}

Be aware that this is kept as simple as possible, no error checking is done here etc.

commented: .NET 1.1? Seriously? +0

hello ddanbe,

thank you for your reply. I did try the code but I'm getting an error.

"Error 1 The type or namespace name 'ArrayList' could not be found (are you missing a using directive or an assembly reference?)"

do I need to create here an additional namespace?

thank you and regards

If you add using System.Collections; it should work.

hi thanks, i tried it but it didn't work.
i'm not sure if i have to configure here.

That's a pretty weird error. What are you using to build and run things?

hi it worked. I thought the using.System.Collections.Generic is already the same as using.System.Collections. That's why. But then I tried adding it at the end then it work. I just need to rename some of the variables because it sounds quite different.
like: "Enter the number of prices". I changed it to "Enter the number of items:"
thoughtcoder, thanks. I'm using microsoft visual c# 2008 and trying the examples in the console application.

by the way I would like to know why, do I have to add the "using System.Collections;" what is the reason behind?

thanks for your help again;-)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

hi again,
i would like to know why do I have to add the "using System.Collections;" what is the reason behind?

thanks again
regards

ArrayList is in the System.Collections namespace and List<> is in the System.Collections.Generic namespace. Use List<double> instead of an ArrayList, and use System.Collections.Generic instead of System.Collections. There's no reason to use obsolete collection types here.

i see the ArrayList and the System.Collections are already obsolete?
but are they also part of C#?

thanks

Yes. You can still use them. They're not formally obsolete -- they aren't going away.

But it's better to use List<double> because that gives you better type safety and makes your code easier for others to read.

Yes. You can still use them. They're not formally obsolete -- they aren't going away.

But it's better to use List<double> because that gives you better type safety and makes your code easier for others to read.

thanks. if i use the List<double> is my code correct here:
btw what does the via the Add Method here means?

//An ArrayList can store any number of items via the Add method
List<double> Prices = new List<double>();

//initialize the total price
double theSumValue = 0.0;

Prices.Add(2.0);

This only appends to your list, this does not so called 'add' or sum them together

for the sum you need to use a foreach or enumerator as you had before, and increment sumValue

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.