Hello to all

Below is my simple code. The problem is they are again and again showing that sum and i does not exist in the current context. I am not getting my problem how to correct the code.I am new learner of c# so please explain it also.

namespace table2
{
    class Program
    {

       static public void Main()
        {
            int num; i = 0; sum = 0;
            do
            {

                Console.WriteLine("Enter Number");
                num = Convert.ToInt32(Console.ReadLine());
                if (num < 0)
                {
                    continue;
                }
                sum = sum + num;
                i++;

            }
            while (i < 3);
            Console.WriteLine("Sum of positive nos ={0}",sum);
            Console.ReadLine();

        }
    }
}

Regards
Payal

int num; i = 0; sum = 0;

that was the problem

from the above, could you tell what type of i and sum are;-
answer is no.

declare variables as follow :-

int num,i = 0,sum = 0;

hope this helps you . . .

Edited 3 Years Ago by rishif2

Although it is syntactically correct(as rishif2 did) to write all these declarations on one line, I would prefer:

int num;
int i = 0;
int sum = 0;

You have given ";" after num and that means the end of statement, so it throws an error for "i" and "sum".
Try using

int num, i = 0, sum = 0;

Hope that helps.

This article has been dead for over six months. Start a new discussion instead.