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

Recommended Answers

All 3 Replies

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 . . .

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.

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.