LeFemur
LeFemur

Reputation: 51

Ask user for 5 numbers then add together for total using a loop

using System;
 class SumDoubles
 {
    static void Main()
    {   
        //Declare variables
        double DblSumTotal = 0;
        double LIMIT = 0;


        //Ask user to input 5 numbers to be added
        Console.Clear();
        Console.WriteLine("Enter 5 numbers to be added together.");
        do 
        {
            DblSumTotal = DblSumTotal + (Convert.ToDouble(Console.ReadLine()));
            LIMIT = LIMIT + 1;
        } while (LIMIT < 6);

        //Output total
        Console.WriteLine("The total sum of the 5 numbers is " + DblSumTotal);
        Console.ReadLine();
    }   
}

This is the code I have so far. It will compile but I receive an error when trying to run it:

Unhandled Exception: Input string was not in a correct format. at System.Number.ParseDouble(String value, NumberStyles options, NumberFormat Info numfmt) at System.Double.Parse(String s, IFormatProvider provider) at SumDoubles.Main()"

I am very new to coding and I can't seem to wrap my head around loops. Please help!

Upvotes: 0

Views: 3832

Answers (4)

tinypanda
tinypanda

Reputation: 1

I think this can be simplified by using a for loop.

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

        namespace ConsoleApplication2
        {
            class Program
            {
                static void Main(string[] args)
                {
                    int read;
                    int sum = 0;
                    Console.WriteLine("Enter 5 numbers: "); 
                    for (int i = 0; i < 5; i++)
                    {
                       read = int.Parse(Console.ReadLine());
                       sum = sum + read;
                    }

                    Console.WriteLine("The total sum of the 5 numbers are " + sum);
                }
            }
        }

Or if you still want to use while loops it just as easy.

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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int read;
            int sum = 0;
            int i = 0;
            Console.WriteLine("Enter 5 numbers: "); 
            while(i<5)
            {
               read = int.Parse(Console.ReadLine());
               sum = sum + read;
               i++;
            }

            Console.WriteLine("The total sum of the 5 numbers are " + sum);
        }
    }
}

Upvotes: -2

Oskar H&#253;bl
Oskar H&#253;bl

Reputation: 401

Both Sasse and Tejas Sharma have a point. Complete code might then look something like this:

 using System;
 class SumDoubles
 {
    static void Main()
    {   
        //Declare variables
        double DblSumTotal = 0;
        double LIMIT = 0;


        //Ask user to input 5 numbers to be added
        Console.Clear();
        Console.WriteLine("Enter 5 numbers to be added together.");
        do 
        {
            double d;
            if (!double.TryParse(Console.ReadLine(), out d)) {
                Console.WriteLine("Format error!!!");
            } else {
                DblSumTotal = DblSumTotal + d;
                LIMIT = LIMIT + 1;
            }

        } while (LIMIT < 5);

        //Output total
        Console.WriteLine("The total sum of the 5 numbers is " + DblSumTotal);
        Console.ReadLine();
    }   
}

` Also, if you, like me are using non-English locale (I have a Czech system), then the string that can be parsed to double looks a bit different (i.e. Czech "1,2" vs English "1.2")

Upvotes: 0

Tejas Sharma
Tejas Sharma

Reputation: 3440

Convert.ToDouble throws the exception you're seeing when it gets bad input e.g. an empty string. You probably either want to handle the exception using a try catch block or you could alternatively use double.TryParse() which doesn't throw in the face of bad input.

double output;
bool isValid = double.TryParse("10", out output); // isValid is true and output contains 10
isValid = double.TryParse("lsdf", out output); // isValid is false

I also noticed that you have an "off by 1" error in this code

Console.WriteLine("Enter 5 numbers to be added together.");
do 
{
    DblSumTotal = DblSumTotal + (Convert.ToDouble(Console.ReadLine()));
    LIMIT = LIMIT + 1;
} while (LIMIT < 6);

Your loop has 6 iterations (LIMIT = 0,1,2,3,4,5) and you are expecting 5 numbers. You're probably pressing an extra "enter" which is causing an empty string "" to get passed to Convert.ToDouble and thus leading to the exception. Change while (limit < 6) to while (limit < 5).

Upvotes: 2

Sasse
Sasse

Reputation: 1128

Your program works just fine, except it reads six numbers instead of five.

I think your problem is related to what xeraphim is saying, you're trying to input all the numbers at the same time.

If you type "4" ENTER "5" ENTER "10" ENTER and so on, it will work just fine. =)

PS: Remember that there's a difference between "." and ",".

Upvotes: 1

Related Questions