2 Replies - 1284 Views - Last Post: 16 November 2012 - 11:41 AM Rate Topic: -----

#1 rubydazey5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-November 12

Arrays problem

Posted 15 November 2012 - 11:14 PM

Ok I am taking C# in college and am stuck on this arry problem here is my code so far


using System;
namespace Project6Chap7
{
    class Arrays
    {
        static void Main( )
        {
            int[] aNum = new int[1000];
            int[] i = new int[1000];
            string inValue;

            int aNumCount = 0;
            int total = 0;
            Console.Write("Enter a number 1 to 100: "
                        + "(or -99 to exit)", aNumCount + 1);
            inValue = Console.ReadLine();

            while(inValue != "-99")
            {
                aNum[aNumCount] = Convert.ToInt32(inValue);
                ++aNumCount;
                Console.WriteLine("Enter a number 1 to 100: "
                           + "(or -99 to exit)", aNumCount + 1);
                inValue = Console.ReadLine( );
            }

            foreach (int val in aNum)
            {
                total += val;
            }
            Console.WriteLine("Total: " + total);


            
            Console.WriteLine("The first value entered was: " + i);


                   
        }
    }
}


it runs fine so far but my question is how do I call the first number entered. I have tried several different ways but I either get -1 or ToInt32
Please help me

This post has been edited by Martyr2: 15 November 2012 - 11:29 PM
Reason for edit:: Fixed code tags. [code]code in here[/code]. Thanks! :)


Is This A Good Question/Topic? 0
  • +

Replies To: Arrays problem

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,085
  • Joined: 18-April 07

Re: Arrays problem

Posted 15 November 2012 - 11:32 PM

Try this line...

Console.WriteLine("The first value entered was: " + aNum[0].ToString());



Here we are saying "get the first value from the array of integers named aNum". You stored the first entry typed in aNum[0] when you went through the first time in your while loop. aNumCount was zero on that first time through.

:)

This post has been edited by Martyr2: 15 November 2012 - 11:32 PM

Was This Post Helpful? 1
  • +
  • -

#3 heinbau  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 22
  • Joined: 08-November 12

Re: Arrays problem

Posted 16 November 2012 - 11:41 AM

What he said, the only thing that you need to remember (and it messed me up a few times!) is that arrays start their counting at '0' There was a situation where I spent more time than I care to admit trying to solve that problem, and I will never ever ever forget again :/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1