i'm new to c# so i don't know how to use a for loop to increment my nums. num apparently doesn't work in c#


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1;
            int num2;
            int num3;
            int num4;
            int num5;
            int neg = 0;
            int zero = 0;
            int pos = 0;
            int i;
            Console.WriteLine("enter 5 numbers");
            num1 = Convert.ToInt32(Console.ReadLine());
            num2 = Convert.ToInt32(Console.ReadLine());
            num3 = Convert.ToInt32(Console.ReadLine());
            num4 = Convert.ToInt32(Console.ReadLine());
            num5 = Convert.ToInt32(Console.ReadLine());
            for (i=1; i < 6; i++)
            {
                if (num[i] < 0)
                    neg = neg + 1;
                if (num[i] == 0)
                    zero = zero + 1;
                if (num[i] > 0)
                    pos = pos + 1;
            }
        }
    }
}

Apologies if you read what I posted first. It was entirely wrong.

[Edit]
Your num[] variable is never defined. In C# you have to define it as an array before you can use it.
E.g.

int[] num;
for(i=1;<i<6;i++){
  ...
}

But this will also fail as num is still equivocally null so you must assign it as well.

int[] num = new int[] { };

That defines it as an empty array, now you must fill it. I cannot really help you with that part without knowing more about what it is you're attempting to accomplish.

thank you very much, this prog is a simple one that i ask for 5 numbers and determine how many are negative, zero, and positive....
should i add that right after my for loop?

for(int i = 1; i < 6; i++)
            {
                int[] num = new int[] { };
                if (num[i] < 0)
                    neg = neg + 1;
                if (num[i] == 0)
                    zero = zero + 1;
                if (num[i] > 0)
                    pos = pos + 1;
            }

Do i need to also change my num1-num5 and readlines in the for loop?

Well if you're using the for() loop to iterate through the numbers, you could do something like:

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

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			int neg = 0;
			int zero = 0;
			int pos = 0;
			int i;
			int[] num = new int[5];
			Console.WriteLine("enter 5 numbers");
			num[0] = Convert.ToInt32(Console.ReadLine());
			num[1] = Convert.ToInt32(Console.ReadLine());
			num[2] = Convert.ToInt32(Console.ReadLine());
			num[3] = Convert.ToInt32(Console.ReadLine());
			num[4] = Convert.ToInt32(Console.ReadLine());
			for (i = 0; i < 5; i++)
			{
				if (num[i] < 0)
					neg = neg + 1;
				if (num[i] == 0)
					zero = zero + 1;
				if (num[i] > 0)
					pos = pos + 1;
			}
		}
	}
}

Though I'll say it now that I'm not familiar with console applications but given what you did, that theoretically should work.

It's also very critical to understand that in the everyday world, 1 is the starting point for counting. However in the programming world, 0 is always the starting point unless you manually define it otherwise.

The new int[5] is telling it how many ints the array will hold, a total of 5. However it starts at 0 and counts up 5 times, thus resulting in 0-4, not 1-5. If you want 1-5, it's going to take a bit more work and is not usually worth it.

Stab in the dark here but would

String num;
for (i=1; i < 6; i++)
{
if ("num"+i < 0)
  neg = neg + 1;

if ("num"+i == 0)
  zero = zero + 1;
if ("num"+i > 0)
  pos = pos + 1;
}

would this work?

Stab in the dark here but would

String num;
for (i=1; i < 6; i++)
{
if ("num"+i < 0)
  neg = neg + 1;

if ("num"+i == 0)
  zero = zero + 1;
if ("num"+i > 0)
  pos = pos + 1;
}

would this work?

Did my idea work?

Stab in the dark here but would

String num;
for (i=1; i < 6; i++)
{
if ("num"+i < 0)
  neg = neg + 1;

if ("num"+i == 0)
  zero = zero + 1;
if ("num"+i > 0)
  pos = pos + 1;
}

would this work?

No, it wouldn't even compile. Adding an integer to a string results in a string, and you can't compare strings to integers.

The original poster never does anything with the numbers other than compare them to his various accumulation if statements, so doesn't really need an array to store them in, he could just get each one, compare it, and then get the next:

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {
            int num;
            int neg = 0;
            int zero = 0;
            int pos = 0;

            Console.WriteLine("enter 5 numbers");
            for (int i = 0; i < 5; i++) {
                num = Convert.ToInt32(Console.ReadLine());
                if (num < 0) {
                    neg++;
                } else if (num == 0) {
                    zero++;
                } else {
                    pos++;
                }
            }
        }
    }
}

Of course this, as the original, doesn't do anything with the neg/zero/pos values, it just calculates them then exits.

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.