So, I have to ask the user for a set of 5 to 15 numbers until they enter an EOP. How do I save those numbers in an array? Using those numbers from the array, I will have to do some other stuff, like listing them, finding the average, etc. But I can't figure out how to save the numbers entered by the user into the array.
Console.WriteLine("Please enter a set of grades. Min 5 grades, Max 15 grades:");
Console.WriteLine("To show the menu, enter -99");
for (int y = 0; y < 16; y++)
{
Console.WriteLine("Enter grade:");
strGrades = Console.ReadLine();
intGrades = Int32.Parse(strGrades);
if (intGrades == -99)
{
System.Console.WriteLine("1. Number of values in the array\n");
System.Console.WriteLine("2. List the values in the array\n");
System.Console.WriteLine("3. Average\n");
System.Console.WriteLine("4. Delete a specific value \n");
System.Console.WriteLine("5. Clear all the values in the array\n");
System.Console.WriteLine("6. Change a specific value\n");
System.Console.WriteLine("7. Exit");
strChoice = Console.ReadLine();
Choice = Int32.Parse(strChoice);
int[] arr = new int[15];
for (int x = 0; x <= arr.Length; x++)
{
arr[x] = intGrades;
arr[x] = Int32.Parse(Console.ReadLine());
intCounter++;
if (intGrades == -99)
{
intCounter--;
}
}
Try to use List
Console.WriteLine("Please enter a set of grades. Min 5 grades, Max 15 grades:");
Console.WriteLine("To show the menu, enter -99");
List<int> lstGrades=new List<int>();
for (int y = 0; y < 16; y++)
{
Console.WriteLine("Enter grade:");
strGrades = Console.ReadLine();
intGrades = Int32.Parse(strGrades);
lstGrades.Add(intGrades);
if (intGrades == -99)
{
System.Console.WriteLine("1. Number of values in the array\n");
System.Console.WriteLine("2. List the values in the array\n");
System.Console.WriteLine("3. Average\n");
System.Console.WriteLine("4. Delete a specific value \n");
System.Console.WriteLine("5. Clear all the values in the array\n");
System.Console.WriteLine("6. Change a specific value\n");
System.Console.WriteLine("7. Exit");
strChoice = Console.ReadLine();
Choice = Int32.Parse(strChoice);
/*int[] arr = new int[15];
for (int x = 0; x <= arr.Length; x++)
{
arr[x] = intGrades;
arr[x] = Int32.Parse(Console.ReadLine());
intCounter++;
if (intGrades == -99)
{
intCounter--;*/
}
}
I would do something like this:
Keep in mind that for List you need to use System.Collections.Generic
using System.Collections.Generic;
So... the program:
static void Main()
{
Console.WriteLine( "To show the menu, enter -99" );
Console.WriteLine( "Please enter a set of grades. Min 5 grades, Max 15 grades: " );
List<int> gradesList =new List<int>();
bool exit=false;
do
{
int x = 0;
int.TryParse( Console.ReadLine(), out x );
switch( x )
{
case -99:
exit = true;
break;
case 0:
Console.WriteLine( "Please insert integer values:" );
break;
default:
gradesList.Add( x );
break;
}
} while( !exit );
ShowMenu();
}
static void ShowMenu()
{
bool exit=false;
do
{
Console.WriteLine( "1. Number of values in the array" );
Console.WriteLine( "2. List the values in the array" );
Console.WriteLine( "3. Average" );
Console.WriteLine( "4. Delete a specific value" );
Console.WriteLine( "5. Clear all the values in the array" );
Console.WriteLine( "6. Change a specific value" );
Console.WriteLine( "7. Exit" );
int x = 0;
int.TryParse( Console.ReadLine(), out x );
switch( x )
{
case 1:
//number of values in the array
break;
case 2:
//list of values in the array
break;
case 3:
//average
break;
case 4:
//delete a specific value
break;
case 5:
//clear all values in the array
break;
case 6:
//change a specific value
break;
case 7:
exit = true;
break;
default:
Console.WriteLine("Invalid option");
break;
}
} while( !exit );
}
Related
I've created a program that adds all input values from the user and prints the sum if the user entered 0 or greater than 101. Here's my code:
int n, sum = 0;
do
{
Console.Write("Enter a number:");
n = int.Parse(Console.ReadLine());
sum += n;
}
while (n != 0 && n < 101);
Console.WriteLine("Sum is:" + sum);
Console.ReadKey();
I'm trying to figure how to accepts numbers alternately. For example, Input values are: 4, 7, 8, 3, 6, 1. If the user input two consecutive odd or even number the system will not accept two consecutive odd or even or it will display the sum of all inputted numbers.
Taking the recomendation of Andrew an Peter you can add the list to save your previous inputs and do some checks to the current and prev data to do the logic.
the code that implements this is the following:
//save input list
List<int> inputNumbers = new List<int>();
int n, sum = 0;
do
{
Console.Write("Enter a number:");
n = int.Parse(Console.ReadLine());
//My Recomendation ==============================
//save previus input numbers
inputNumbers.Add(n);
//Check is there are previous input number
if(inputNumbers.Count>1){
//New control vars
Boolean previousNumberIsOdd= false,currentNumberIsOdd= false;
foreach (int item in inputNumbers)
{
Console.Write(item+",");
}
//check if previus number is odd
if((inputNumbers[inputNumbers.Count-2])%2 == 0){
previousNumberIsOdd = true;
}
//Check if current number is odd
if(n%2==0){
currentNumberIsOdd = true;
}
Console.WriteLine("Control vars:" + previousNumberIsOdd +"/"+currentNumberIsOdd);
//Check diferent scenarios and do the logic
//previous and current number are odds
if(previousNumberIsOdd && currentNumberIsOdd){
//break while and write the result
break;
}
//previous and current number are evens
if(!previousNumberIsOdd && !currentNumberIsOdd){
//break while and write the result
break;
}
}
//if there aren't numbers to break the cycle then do the original logic
//End of my recomendation =====================
sum += n;
}
while (n != 0 && n < 101);
Console.WriteLine("Sum is:" + sum);
Console.ReadKey();
}
You can do many optimizations to this code but a put it like that to be very clear in the logic.
By using two flag you can achieve the result.
int n, sum = 0;
bool previous = false, current;
bool init = true;
do
{
Console.Write("Enter a number:");
n = int.Parse(Console.ReadLine());
current = n % 2 == 0;
if( current == previous && !init)
break;
previous = current;
init = false;
sum += n;
}
while (n != 0 && n < 101);
Console.WriteLine("Sum is:" + sum);
Console.ReadKey();
Output
My task is to let a user input numbers, and when they input 0, the loop will end and should display the lowest number. However since they're inputting 0, that becomes the lowest number. I have tried using the OrderBy to skip the first number in the array, but that doesn't seem to be working.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Task 7\n");
int[] numbers = new int[100];
for(int i = 0; i < numbers.Length; i++)
{
Console.WriteLine("Input any number. Once you enter 0, the application will end");
numbers[i] = int.Parse(Console.ReadLine());
if(numbers[i] == 0)
{
break;
}
}
int lowest = numbers.OrderBy(num => num).Skip(1).First();
Console.WriteLine($"The lowest number was: {lowest}");
Console.ReadLine();
}
}
Maybe there's a smarter way overall to end the loop when the user inputs 0
Any ideas?
Use a temporary variable to store the output from int.Parse() before adding it to your array:
var temp = int.Parse(Console.ReadLine());
if(temp == 0)
{
break;
}
numbers[i] = temp;
You'll still get 0 as the lowest value, because the remaining indexes in the array has not been assigned to (int initializes to 0), so you might want to take that into account by either filtering the array before ordering:
int lowest = numbers.Where(num => num > 0).OrderBy(num => num).First();
Or by using a dynamically sized data structure, such as a list:
List<int> numbers = new List<int>();
for(int i = 0; i < 100; i++)
{
Console.WriteLine("Input any number. Once you enter 0, the application will end");
var temp = int.Parse(Console.ReadLine());
if(temp == 0)
{
break;
}
numbers.Add(temp);
}
int lowest = numbers.OrderBy(num => num).First();
This of course assumes you want to store all the input values, otherwise just keep track of the lowest value in a single int
if you don't need to keep all the input values, you can use an int variable to keep the lowest value between this one and the new input value.
So at the end of the loop you have to print this variable which contains the lowest value.
int lowest = Int32.MaxValue;
for(int i = 0; i < numbers.Length; i++)
{
Console.WriteLine("Input any number. Once you enter 0, the application will end");
lowest = Math.Min(int.Parse(Console.ReadLine()), lowest);
if(lowest == 0)
{
break;
}
}
Console.WriteLine($"The lowest number was: {lowest}");
Console.ReadLine();
Put a value to the array after checking:
var number = int.Parse(Console.ReadLine());
if (number == 0) break;
numbers[i] = number;
try this too
int[] numbers = new int[100];
int x = 0;
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine("Input any number. Once you enter 0, the application will end");
switch (x = int.Parse(Console.ReadLine()))
{
case 0:
break;
default:
numbers[i] = x;
break;
}
if (x == 0)
break;
}
int lowest = numbers.OrderBy(num => num).First();
Console.WriteLine($"The lowest number was: {lowest}");
Console.ReadLine();
The issue is that you are declaring int[] numbers = new int[100]; the default value of an int array is 0 therefore if for example you only enter 5 values you have the other 95 values still initialised to 0. when you sort the array you have all the zeros at the beginning.
I mean how to count and sum input numbers until receive "end".
thanks !
And also how to find out input is number or letter in c#?
class Program
{
static void Main(string[] args)
{
int n = 0;
int sum = 0;
string inp;
do
{
Console.Write("Numbers ");
inp = Console.ReadLine();
int num= Convert.ToInt16(inp);
sum = sum + num;
n++;
} while (too == "end");
int average = sum / n;
Console.WriteLine(" " + average);
Console.ReadLine();
}
}
I would suggest you use a normal while loop and also add validation to check to integer input.
For the while loop you want to loop until the input is not equal to "end":
while(inp != "end")
For the validation, you can use int.TryParse method:
int num = 0;
if (int.TryParse(inp, out num)) { }
Here is a modified example of your code:
int n = 0;
int sum = 0;
string inp = null;
while(inp != "end")
{
Console.Write("Numbers ");
inp = Console.ReadLine();
int num = 0;
if (int.TryParse(inp, out num))
{
sum = sum + num;
n++;
}
}
int average = sum / n;
Console.WriteLine(" " + average);
Console.ReadLine();
// A list to hold all of the numbers entered
List<int> numbers = new List<int>();
// Will hold the inputted string
string input;
// This needs to be outside the loop so it's written once
Console.Write("Numbers: " + Environment.NewLine);
// Keep going until we say otherwise
while (true)
{
// Get the input
input = Console.ReadLine();
// Will hold the outcome of parsing the input
int number = -1;
// Check to see if input was a valid number
bool success = int.TryParse(input, out number);
// If it was a valid number then remember it
// If ANY invalid or textual input is detected then stop
if (success)
numbers.Add(number);
else
break;
}
// Write the count and average
Console.WriteLine("Count:" + numbers.Count);
Console.WriteLine("Average:" + numbers.Average());
Console.ReadLine();
Input:
Numbers:
1
2
3
4
5
Output:
Count: 5
Average: 3
The only thing here a little different to what you specified is ANY invalid or textual entry causes it to finish, not just typing the word "end", although that obviously works too.
Basically I've created a times table app. At the end of the app, I've been able to say how many times I got it wrong, and how many times I got it right.
However I want it to say:
You got:
6 x 4 wrong 6 times.
5 x 2 wrong 9 times.
etc...
Rather than you got 20 correct, and 8 wrong.
So I can see the specific multiplications I got wrong. I know where I need to add the code (under the else statement). But not sure how to go about this.
I thought the best solution would be storing all the wrongs as a string in an array, then counting identical strings and outputting a number. But I have no idea how to do this.
Here's my code:
namespace TimesTablesGame
{
class multiplication
{
Random rng = new Random();
int randomNumber;
int randomNumberTables;
int c = 0;
int w = 0;
int numberOfGos = 0;
int minRangeTables;
int maxRangeTables;
int minRange;
int maxRange;
public multiplication()
{
start:
Console.Clear();
Console.WriteLine("\nPlease enter the lowest number range you would like to practice your times tables on: ");
minRangeTables = int.Parse(Console.ReadLine());
Console.WriteLine("\nPlease enter the higest number range you would like to practice your times tables on: ");
maxRangeTables = int.Parse(Console.ReadLine());
Console.WriteLine("\nPlease enter the number of times you would like play: ");
numberOfGos = int.Parse(Console.ReadLine());
Console.WriteLine("\nPlease enter the minimum range you would like to multiply by: ");
minRange = int.Parse(Console.ReadLine());
Console.WriteLine("\nPlease enter the maximum range you would like to multiply by: ");
maxRange = int.Parse(Console.ReadLine());
repeat:
Console.Clear();
for (int i = 1; i <= numberOfGos; i++)
{
randomNumberTables = rng.Next(minRangeTables, maxRangeTables + 1);
randomNumber = rng.Next(minRange, maxRange + 1);
Console.Write("\n\n{0}: {1} x {2} = ", i, randomNumberTables, randomNumber);
if (randomNumberTables * randomNumber == int.Parse(Console.ReadLine()))
{
Console.WriteLine("Correct");
c++;
}
else
{
Console.WriteLine("Wrong it is: " + randomNumberTables * randomNumber);
w++;
}
}
Console.WriteLine("\nYou were correct {0} times, and wrong {1} times.", c, w);
Console.ReadLine();
Console.WriteLine("Would you like to play again? Type y for Yes with new settings, r for repeat using last settings, and any other key to exit.");
char again = Console.ReadKey().KeyChar;
if (again == 'y')
{
c = 0;
w = 0;
goto start;
}
else if (again == 'r')
{
c = 0;
w = 0;
goto repeat;
}
}
}
}
I'd keep track in a Dictionary....
Resisting the urge to re-write the GOTO stuff.....here's a relatively simple way to do it:
Dictionary<string, int> wrongs;
//Console questions...
//Begin looping logic
wrongs = new Dictionary<string, int>();
for (int i = 1; i <= numberOfGos; i++)
{
randomNumberTables = rng.Next(minRangeTables, maxRangeTables + 1);
randomNumber = rng.Next(minRange, maxRange + 1);
string eq = String.Format("{0} x {1} = ", randomNumberTables, randomNumber);
Console.Write("{0}: " + eq, i);
if (randomNumberTables * randomNumber == int.Parse(Console.ReadLine()))
{
Console.WriteLine("Correct");
c++;
}
else
{
Console.WriteLine("Wrong it is: " + randomNumberTables * randomNumber);
if (wrongs.Any(x => x.Key == eq))
{
wrongs[eq]++;
}
else
{
wrongs.Add(eq, 1);
}
w++;
}
}
Console.WriteLine("\nYou were correct {0} times, and wrong {1} times.", c, w);
Console.WriteLine("\n\nYou got:");
foreach (var item in wrongs)
{
Console.WriteLine("{0} wrong {1} times", item.Key, item.Value);
}
//Your logic to repeat/restart
Also....note that this does not count "2 x 4" and "4 x 2" as the same equation....
Also also....You can easily do this without using GOTO....please consider it.
I am working through learning C# on my own - this is not a homework assignment. I am only on chapter 7 so I am hoping for a simple/basic answer.
I am having troubles calling the TalentListing method from Main that gets its parameters from another method. Do I have to duplicate them in the Main method?
I realize the methods are void - I don't need them to return anything, I just need them to run.
Sorry the code is long, I thought it would be best to show it all.
Thank you!
class Program
{
static void Main()
{
int pastContestants;
int currentContestants;
// Call Methods:
Console.Write("LAST YEAR'S TALENT: ");
pastContestants = Contestants();
Console.Write("THIS YEAR'S TALENT: ");
currentContestants = Contestants();
Overview(pastContestants, currentContestants);
CompetitorTalents(currentContestants);
// need to call the TalentListing Method...
TalentListing(CompetitorTalents(contestantNames), CompetitorTalents(validTalentCodes), CompetitorTalents(contestantTalentCodes);
Console.ReadLine();
}
public static int Contestants()
{ // Get & returns valid # of contestants. Called twice - last year & this year
int contestants = 0; // holds return value
const int MIN = 0; // will allow 0 as an answer
const int MAX = 30; // will allow 30 as an answer
Console.Write("Please enter the number of contestants: ");
contestants = Int32.Parse(Console.ReadLine());
while (contestants < MIN || contestants > MAX)
{
Console.Write("Invalid number. Please enter a number between 0 and 30, inclusive: ");
contestants = Int32.Parse(Console.ReadLine());
}
return contestants;
}
public static void Overview(int past, int current)
{ // Accepts contestant - past & current. Displays 1 of 3 messages.
double entranceFee = 25.00;
Console.WriteLine("\nWe had {0} contestants last year and have {1} contestants this year.", past,
current);
//if more than double
if (current > (past * 2))
Console.WriteLine("The competition is more than twice as big this year!");
//if bigger but no more than double
if (current > past && current < (past * 2))
Console.WriteLine("The competition is bigger than ever!");
//if smaller than last year
if (current < past)
Console.WriteLine("A tighter race this year. Come out and cast your vote!");
Console.WriteLine("\nThe revenue expected for this year is {0:C}.", current * entranceFee);
}
public static void CompetitorTalents(int current)
{ // Fill array of competitors and their talent codes.
string[] contestantNames;
string contestantNameEntered;// user entry
char[] contestantTalentCodes;
char talentCodeEntered; // user entry
string[] talents = { "Dancing", "Musical", "Other" };
char[] validTalentCodes = { 'D', 'M', 'O' };
int[] total = new int[talents.Length];
bool validCode = false;
int counter = 0;
contestantNames = new string[current]; // set array size
contestantTalentCodes = new char[current]; // set array size
// put contestants in array
while (counter < current)// loop for all contestants
{
// get contenstants name and put in array
Console.Write("Please enter the name of contestant: ");
contestantNameEntered = Console.ReadLine();
counter += 1; // contestant number
validCode = false;
//place in correct array element
contestantNames[counter - 1] = contestantNameEntered;
// get contestants talent code, verify and place in array
while (validCode == false) // reset per contestant
{
Console.Write("Please enter the contestant's talent code (D=Dancing, M=Musical, O=Other): ");
talentCodeEntered = Char.Parse(Console.ReadLine().ToUpper()); // convert to uppercase
for (int x = 0; x < validTalentCodes.Length && !validCode; ++x)
if (talentCodeEntered == validTalentCodes[x]) // talent code valid?
{
validCode = true; // true if match found
contestantTalentCodes[counter - 1] = talentCodeEntered; //put talent code in array
x = validTalentCodes.Length; // breaks out of loop
}
if (validCode == false) // false if no match found = invalid code.
{
Console.WriteLine("Invalid code talent Code.");
}
}
}
// search all elements of validTalentCodes array (D, M, O), count instances in contestantTalentCodes array
for (int z = 0; z < validTalentCodes.Length; z++) // validTalentCodes
for (int x = 0; x < contestantTalentCodes.Length; x++) // contestantTalentCodes
if (contestantTalentCodes[x] == validTalentCodes[z])
{
total[z]++;
}
for (int x = 0; x < talents.Length; ++x)
Console.WriteLine("Total contenstants for {0} is {1}.", talents[x], total[x]);
}
public static void TalentListing(string contestantNames, char [] validTalentCodes, char [] contestantTalentCodes)
{ // Continuously prompt for talent codes and display contestants with the corresponding talent until quit.
// What talent code would you like to see (or QUIT)?
const char QUIT = 'Q';// must be upper as char are converted!!
char userOption = ' ';
bool validCode = false;
while (userOption != QUIT)
{
Console.Write("\nWhat talent code would you like to view? (D, M, O or Q to quit): ");
validCode = false; // reset from previous section
userOption = Char.Parse(Console.ReadLine().ToUpper());
for (int x = 0; x < contestantNames.Length && !validCode; ++x)
{
if (userOption == validTalentCodes[x]) // valid talent code?
{
validCode = true;
Console.Write("The contestants in {0} talent are: ", validTalentCodes[x]);
// display list of contestants with that code
for (int z = 0; z < contestantTalentCodes.Length; z++) // validTalentCodes
if (contestantTalentCodes[z] == userOption)
{
Console.Write("\n{0}", contestantNames[z]);
}
}
}
if (validCode == false)
Console.WriteLine("Invalid talent code!");
}
Console.WriteLine(); // when QUIT is selected
}
}
The CompetitorTalents(int current) method has no return value, you are using void. The TalentListing receives two strings. So you need to get those two from somewhere. If you want them to come from the CompetitorTalens method, you have to change it so it returns string and actually return something from it.
For this simple app use static global variables. Just copy this code and take a look.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public static string[] talents = { "Dancing", "Musical", "Other" };
public static char[] validTalentCodes = { 'D', 'M', 'O' };
public static string[] contestantNames;
public static char[] contestantTalentCodes;
static void Main()
{
int pastContestants;
int currentContestants;
// Call Methods:
Console.Write("LAST YEAR'S TALENT: ");
pastContestants = Contestants();
Console.Write("THIS YEAR'S TALENT: ");
currentContestants = Contestants();
Overview(pastContestants, currentContestants);
CompetitorTalents(currentContestants);
// need to call the TalentListing Method...
TalentListing();
Console.ReadLine();
}
public static int Contestants()
{ // Get & returns valid # of contestants. Called twice - last year & this year
int contestants = 0; // holds return value
const int MIN = 0; // will allow 0 as an answer
const int MAX = 30; // will allow 30 as an answer
Console.Write("Please enter the number of contestants: ");
contestants = Int32.Parse(Console.ReadLine());
while (contestants < MIN || contestants > MAX)
{
Console.Write("Invalid number. Please enter a number between 0 and 30, inclusive: ");
contestants = Int32.Parse(Console.ReadLine());
}
return contestants;
}
public static void Overview(int past, int current)
{ // Accepts contestant - past & current. Displays 1 of 3 messages.
double entranceFee = 25.00;
Console.WriteLine("\nWe had {0} contestants last year and have {1} contestants this year.", past,
current);
//if more than double
if (current > (past * 2))
Console.WriteLine("The competition is more than twice as big this year!");
//if bigger but no more than double
if (current > past && current < (past * 2))
Console.WriteLine("The competition is bigger than ever!");
//if smaller than last year
if (current < past)
Console.WriteLine("A tighter race this year. Come out and cast your vote!");
Console.WriteLine("\nThe revenue expected for this year is {0:C}.", current * entranceFee);
}
public static void CompetitorTalents(int current)
{ // Fill array of competitors and their talent codes.
string contestantNameEntered;// user entry
char talentCodeEntered; // user entry
int[] total = new int[talents.Length];
bool validCode = false;
int counter = 0;
contestantNames = new string[current]; // set array size
contestantTalentCodes = new char[current]; // set array size
// put contestants in array
while (counter < current)// loop for all contestants
{
// get contenstants name and put in array
Console.Write("Please enter the name of contestant: ");
contestantNameEntered = Console.ReadLine();
counter += 1; // contestant number
validCode = false;
//place in correct array element
contestantNames[counter - 1] = contestantNameEntered;
// get contestants talent code, verify and place in array
while (validCode == false) // reset per contestant
{
Console.Write("Please enter the contestant's talent code (D=Dancing, M=Musical, O=Other): ");
talentCodeEntered = Char.Parse(Console.ReadLine().ToUpper()); // convert to uppercase
for (int x = 0; x < validTalentCodes.Length && !validCode; ++x)
if (talentCodeEntered == validTalentCodes[x]) // talent code valid?
{
validCode = true; // true if match found
contestantTalentCodes[counter - 1] = talentCodeEntered; //put talent code in array
x = validTalentCodes.Length; // breaks out of loop
}
if (validCode == false) // false if no match found = invalid code.
{
Console.WriteLine("Invalid code talent Code.");
}
}
}
// search all elements of validTalentCodes array (D, M, O), count instances in contestantTalentCodes array
for (int z = 0; z < validTalentCodes.Length; z++) // validTalentCodes
for (int x = 0; x < contestantTalentCodes.Length; x++) // contestantTalentCodes
if (contestantTalentCodes[x] == validTalentCodes[z])
{
total[z]++;
}
for (int x = 0; x < talents.Length; ++x)
Console.WriteLine("Total contenstants for {0} is {1}.", talents[x], total[x]);
return ;
}
/// Continuously prompt for talent codes and display contestants with the corresponding talent until quit.
/// What talent code would you like to see (or QUIT)?
public static void TalentListing()
{
const char QUIT = 'Q';// must be upper as char are converted!!
char userOption = ' ';
bool validCode = false;
while (userOption != QUIT)
{
Console.Write("\nWhat talent code would you like to view? (D, M, O or Q to quit): ");
validCode = false; // reset from previous section
userOption = Char.Parse(Console.ReadLine().ToUpper());
for (int x = 0; x < contestantNames.Length && !validCode; ++x)
{
if (userOption == validTalentCodes[x]) // valid talent code?
{
validCode = true;
Console.Write("The contestants in {0} talent are: ", validTalentCodes[x]);
// display list of contestants with that code
for (int z = 0; z < contestantTalentCodes.Length; z++) // validTalentCodes
if (contestantTalentCodes[z] == userOption)
{
Console.Write("\n{0}", contestantNames[z]);
}
}
}
if (validCode == false)
Console.WriteLine("Invalid talent code!");
}
Console.WriteLine(); // when QUIT is selected
}
}
}
You need to close the method call (So you were missing a ) at the end) :
TalentListing(CompetitorTalents(contestantNames), CompetitorTalents(validTalentCodes), CompetitorTalents(contestantTalentCodes));
The methods you are calling are void meaning they dont return anything:
public static string CompetitorTalents(int current)
Then in the method call for the CompetitorTalents you should pass in what is the method needs. I would advise doing this on separate lines:
string contestantNames = CompetitorTalents(currentContestants);
And so on, then in the TalentListing you could just reference the individual variables:
TalentListing(contestantNames, validTalentCodes, contestantTalentCodes);
Well, looking at your method signatures we can see the following:
void TalentListing(string, char[], char[])
void CompetitorTalents(int)
Your CompetitorTalents method returns a void, but you are trying to pass the result of that method as parameters for your TalentListing method. So you are essentially trying to call TalentListing(void, void, void), when really it wants a string, a char[], and then another char[]