Nițu
Nițu

Reputation: 59

how to write a console application that calculates the sum of a given number of integers

Write a console application that calculates the sum of a given number of integers. The numbers are entered one per line, and the application will read one by one until the user writes the character instead of a number. When the user has typed x, the application knows that all the numbers in the string have been entered and displays their amount.

If the first thing the user enters is the x character, the application will return 0.

Example:

For input:

2
5
-3
1
X

The console will display:

5

and this is my code

string[] answer = new string[10];
int sum = 0

for (int i = 0; i < answer.Length; i++)
{
    sum += Int32.Parse(answer[i]);

    if (answer[i] == "x")
    {
        Console.WriteLine(sum);
    }

    answer[i] = Console.ReadLine();
}

Console.Read();

Can anyone tell me why is not working?

Upvotes: 0

Views: 2013

Answers (4)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

You should check for "x" first since int.Parse("x") throws exception:

Wrong order (current code):

 sum += Int32.Parse(answer[i]); // <- this will throw exception on "x" input

 if (answer[i] == "x") {
   ...
 }
 ... 

Right order:

 if (answer[i] == "x") {
   ...
 }
 else 
   sum += Int32.Parse(answer[i]);
 ...  

in order to check for syntax errors (e.g. when user inputs "bla-bla-bla") I suggest int.TryParse instead of int.Parse and let's get rid of the array why should we collect the items (esp. with unwanted restriction of 10 items)?

 // long: we don't want overflow, e.g. 2000000000, 1000000000 
 long sum = 0;

 while (true) {
   // Trim() - let's be nice and allow user put leading/trailing spaces
   string input = Console.ReadLine().Trim();

   if (string.Equals("x", input, StringComparison.OrdinalIgnoreCase))
     break;

   if (int.TryParse(input, out var item))
     sum += item;
   else {
     //TODO: Incorrect input, neither integer nor "x" (e.g. "abracadabra")  
   }   
 }

 Console.WriteLine(sum);

 Console.Read(); 

Upvotes: 0

MaxB
MaxB

Reputation: 438

First of all, the working code (I didn't focus on X but on any char that isn't a number):

int n;
int sum = 0;
while (int.TryParse(Console.ReadLine(), out n))
{
    sum += n;
}
Console.Write(sum ); 
Console.ReadKey();

Secondly, your code doesn't work because your array is full of 'null'-s when you try to parse the content of its first cell in 'answer[i]'

Here's a dumb (a bit) fix for your code:

string[] answer = new string[10];

//HERE
for (int i = 0; i < answer.Length; i++)
{
    answer[i] = "0";
}    

int sum = 0;
for (int i = 0; i < answer.Length; i++)
{
    sum += Int32.Parse(answer[i]);
    if (answer[i] == "x")
    {
        Console.WriteLine(sum);
    }
    answer[i] = Console.ReadLine();
}
Console.Read();

Another problem with your code is you don't stop the iteration once "x" is entered, but continue until the end of the array (until it's been 10 times).

Here's kind of a complete fix for your code:

string[] answer = new string[10];
for (int i = 0; i < answer.Length; i++)
{
    answer[i] = "0";
}
int sum = 0;

for (int i = 0; i < answer.Length; i++)
{
    answer[i] = Console.ReadLine();
    if (answer[i] == "x")
    {
        break;
    }
    sum += Int32.Parse(answer[i]);
}
Console.WriteLine(sum);
Console.Read();

Upvotes: 1

Sunil
Sunil

Reputation: 3424

Few issues:

  1. I think order of your code instructions is not correct. First time when you parse your array element, its not yet initialized.
  2. int sum = 0 is missing ; at the end.
  3. You should always use TryParse instead of Parse

Try the following code:

string[] answer = new string[10];
int sum = 0, number;
for (int i = 0; i < answer.Length; i++)
{
   answer[i] = Console.ReadLine();
   if (answer[i] == "x")
   {
      Console.WriteLine(sum);
      break;
   }
   if(Int32.TryParse(answer[i], out number))
      sum += number;
}

Upvotes: 1

TheGeneral
TheGeneral

Reputation: 81493

I gave you your terminating 'x'

var answer = Console.ReadLine();
var sum = 0;

while (answer != "x")
{    
    if (Int32.TryParse(answer, out var value))
    {
        sum += value;
    }
    answer = Console.ReadLine();    
}
Console.WriteLine(sum);

Upvotes: 0

Related Questions