I have used BOLD where the errors occur. Can someone tell me how to fix the erro
ID: 3551796 • Letter: I
Question
I have used BOLD where the errors occur. Can someone tell me how to fix the errors for c# ?
// Program demonstrates overloaded methods
// that display an int, an amount of money, or a string
// decorated with an argument character
// or a default character 'X'
using System;
class DebugEight4
{
static void Main()
{
FancyDisplay(33);
FancyDisplay(44, "@");
FancyDisplay(55.55);
FancyDisplay(77.77, '*');
FancyDisplay("hello");
FancyDisplay("goodbye", '#');
}
private static void FancyDisplay(int num, char decoration = 'X')
{
Console.WriteLine("{0}{1}{2} {1} {0}{1}{2} ",
decoration, num);
}
private static void FancyDisplay(double num, char decoration = 'X')
{
Console.WriteLine("{0}{0}{0} {0}{0}{0} ",
decoration, num.ToString("C"));
}
private static void FancyDisplay(word, char decoration = 'X')
{
Console.WriteLine("{0}{0}{0} {1} {1}{0}{0} ",
decoration, word);
}
}
Explanation / Answer
// Program demonstrates overloaded methods // that display an int, an amount of money, or a string // decorated with an argument character // or a default character 'X' using System; class DebugEight4 { public static void Main() { FancyDisplay(33); FancyDisplay(44, '@'); // convert "@" to '@' as we need to pass character here, FancyDisplay(55.55); // in order to get properly overloaded in func of line 17. FancyDisplay(77.77, '*'); FancyDisplay("hello"); // lines 14 and 15 gets corrected by declaring FancyDisplay("goodbye", '#'); // word as string in line 25. } private static void FancyDisplay(int num, char decoration = 'X') { Console.WriteLine("{0}{1}{2} {1} {0}{1}{2} ", decoration, num); } private static void FancyDisplay(double num, char decoration = 'X') { Console.WriteLine("{0}{0}{0} {0}{0}{0} ", decoration, num.ToString("C")); } private static void FancyDisplay(string word, char decoration = 'X') // declare word as string { Console.WriteLine("{0}{0}{0} {1} {1}{0}{0} ", decoration, word); }Note : Compile error has been removed by correcting the lines in BOLD. However runtime error of signal:-1 still
persists. To make the program run successful we see that the number of indexes in every console.writeline are
greater than the number of arguments it's taking. So, change the above code as follows :
- // Program demonstrates overloaded methods
- // that display an int, an amount of money, or a string
- // decorated with an argument character
- // or a default character 'X'
- using System;
- class DebugEight4
- {
- public static void Main()
- {
- FancyDisplay(33);
- FancyDisplay(44, '@');
- FancyDisplay(55.55);
- FancyDisplay(77.77, '*');
- FancyDisplay("hello");
- FancyDisplay("goodbye", '#');
- }
- private static void FancyDisplay(int num, char decoration = 'X')
- {
- Console.WriteLine("{0}{1} ", decoration, num); // as, there are only 2 values to print
- }
- private static void FancyDisplay(double num, char decoration = 'X')
- {
- Console.WriteLine("{0}{1} ", decoration, num.ToString("C")); // as, there are only 2 values to print
- }
- private static void FancyDisplay(string word, char decoration = 'X')
- {
- Console.WriteLine("{0}{1} ", decoration, word); // as, there are only 2 values to print
- }
- }
stdin :
empty
stdout :
X33
@44
X$55.55
*$77.77
Xhello
#goodbye
- // Program demonstrates overloaded methods
- // that display an int, an amount of money, or a string
- // decorated with an argument character
- // or a default character 'X'
- using System;
- class DebugEight4
- {
- public static void Main()
- {
- FancyDisplay(33);
- FancyDisplay(44, '@');
- FancyDisplay(55.55);
- FancyDisplay(77.77, '*');
- FancyDisplay("hello");
- FancyDisplay("goodbye", '#');
- }
- private static void FancyDisplay(int num, char decoration = 'X')
- {
- Console.WriteLine("{0}{1} ", decoration, num); // as, there are only 2 values to print
- }
- private static void FancyDisplay(double num, char decoration = 'X')
- {
- Console.WriteLine("{0}{1} ", decoration, num.ToString("C")); // as, there are only 2 values to print
- }
- private static void FancyDisplay(string word, char decoration = 'X')
- {
- Console.WriteLine("{0}{1} ", decoration, word); // as, there are only 2 values to print
- }
- }
stdin :
empty
stdout :
X33
@44
X$55.55
*$77.77
Xhello
#goodbye