Click here to Skip to main content
16,005,181 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, UPDATE
So I got this far and i want to make that the 2nd input is not smaller then 0 but its not also bigger then the size of the array?



C#
using System;
class MainClass

{
        public static void Main (string[] args)
    {
            int[] randomSizedArray;
            string sizeOfArray;
            int convertedSizeArray = -1;
        do{
    Console.WriteLine ("Please Enter the Size of the Array Between 1-99");
    sizeOfArray = Console.ReadLine();
    convertedSizeArray = Int32.Parse(sizeOfArray);
         } while ( convertedSizeArray < 1);

        randomSizedArray= new int[convertedSizeArray];
        Random randomSize = new Random();
        Console.WriteLine ("");
            for (int i=0; i < convertedSizeArray; i++){
                randomSizedArray[i] = randomSize.Next(1,99);
            }

            for (int i=0; i < convertedSizeArray; i++){
                Console.Write(randomSizedArray[i] + " ");
            }


    string swapindex1;
    string swapindex2;
    int index1;
    int index2;
        Console.WriteLine("");
do
  {
    Console.WriteLine ("Please Enter the first value to swap with");
    swapindex1 = Console.ReadLine();
    index1 = Int32.Parse(swapindex1);
  } while ( index1< 1);

do
  {
    Console.WriteLine ("Please Enter the second value to swap with");
    swapindex2 = Console.ReadLine();
    index2 = Int32.Parse(swapindex2);
  } while ( index2< 1 );

        int temp = randomSizedArray[index1];
        randomSizedArray[index1] = randomSizedArray[index2];
        randomSizedArray[index2] = temp;
        Console.WriteLine ("");
            for (int j=0; j < convertedSizeArray; j++){
                Console.Write(randomSizedArray[j] + " ");
        }

    }
}
Posted
Updated 29-Apr-13 9:11am
v3
Comments
Sergey Alexandrovich Kryukov 29-Apr-13 13:29pm    
Forget errors. Throw an exception. Are you familiar with them?
—SA
[no name] 29-Apr-13 13:30pm    
Thanks for the quick reply, and im not familiar at all
Sergey Alexandrovich Kryukov 29-Apr-13 14:57pm    
You need to start with learning Structural Exception Handling, even if you won't use it immediately for this little project. This is one of the fundamentals, simple but absolutely not trivial. It goes totally beyond the usual stack use and method call/return concept. This is not an exaggeration: without exceptions, there is nothing to do in all the modern programming. Pause all you are doing, read on it right now. It will make you totally free from "error" approach.
—SA
[no name] 29-Apr-13 14:58pm    
Okay thanks
ali_heidari_ 29-Apr-13 13:31pm    
wanna throw an exception or a message to user ?

You may replace
Quote:
Console.WriteLine ("Please Enter the Size of the Array Between 1-99");
sizeOfArray = Console.ReadLine();
convertedSizeArray = Int32.Parse(sizeOfArray);

with something like

C#
do
{
  Console.WriteLine ("Please Enter the Size of the Array Between 1-99");
  sizeOfArray = Console.ReadLine();
  convertedSizeArray = Int32.Parse(sizeOfArray);
} while ( convertedSizeArray <1 || convertedSizeArray>99);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Apr-13 14:58pm    
Makes sense, a 5.
—SA
CPallini 29-Apr-13 15:39pm    
Thank you.
replace these
C#
sizeOfArray = Console.ReadLine();
convertedSizeArray = Int32.Parse(sizeOfArray);

with these :
C#
while (true)// a while loop will get data from user until user enter correct data
{
 sizeOfArray = Console.ReadLine();//get the user input
 if (char.IsDigit(sizeOfArray.ToString(), 0))//user entered a digit?
    if (int.Parse(sizeOfArray) > 1 && int.Parse(sizeOfArray) < 99)//the user digit is in correct range?
{
//DO ANYTHING YOU WANT WITH DIGITS HERE.
       break;//so break the while loop if you get correct data
}
    else
                        Console.WriteLine("Enter a digit between 1 to 99");//a message to user
 else
                    Console.WriteLine("Enter a digit between 1 to 99");//a message to user
}
 
Share this answer
 
v3
Comments
[no name] 29-Apr-13 14:11pm    
I seem to be confused on where to put that now, because when i put it just after the int convertedSizeArray, i get all kinds of errors
ali_heidari_ 29-Apr-13 14:43pm    
put them instead of these codes :
sizeOfArray = Console.ReadLine();
convertedSizeArray = Int32.Parse(sizeOfArray);
[no name] 29-Apr-13 15:09pm    
How can i make it so that in the 2nd input, it asks the user to input again if the value is lower then 0 and higher then the array size?
ali_heidari_ 30-Apr-13 1:38am    
yeah! you said you need user enter a number between 1 to 99! not? if you no need this,just remove second if and else! and code will be these :
while (true)// a while loop will get data from user until user enter correct data
{
sizeOfArray = Console.ReadLine();//get the user input
if (char.IsDigit(sizeOfArray.ToString(), 0))//user entered a digit?
{
//DO ANYTHING YOU WANT WITH DIGITS HERE.
break;//so break the while loop if you get correct data
}
else
Console.WriteLine("Enter a digit between 1 to 99");//a message to user
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900