Visual Studio 2005, C# using command prompt when running the program
static
{
try{
double qA = Convert.ToDouble(Console.ReadLine()); return qA * 0.10;} catch{
Console.WriteLine("Invalid Input!");}}this at first works fine until i added the try-and-catch! it produces an error
'not all code paths return a value'. where's the problem?
-----------------------------------------------------------------------
and also, i want to ask how will i repeat the program for another user.
i ask like this.. Try Again? [Y/N]:
i dnt know what to do when the user presses yes.
im just new to C#. i study through tutorials but i can't find these things so i hope you understand
tnx in advance
SolitairePosted Nov 28, 2006, 3:05 PM
It's not impossible. If you are using a Console application with C# 2005, you can simply clear the screen with:
Console.Clear();
You can also use the TryParse method instead of a try-catch block:
string sa; double da;
Console.Write("Enter a number: ");
sa = Console.ReadLine();
Double.TryParse(sa, out da);
Console.WriteLine("{0} parsed to a Double is: {1}", sa, da);
If the input is non-numeric, it will simply return 0, without an error. If the user intended it to be a zero, you can test if his string input was "0" otherwise inform him to reenter.
It's also possible to control the cursor location in a Console application (starting with NET framework 2.0) with Console.CursorTop and Console.CursorLeft, then erase the text following that location by overwriting it with spaces, and then returning to the same location at the beginning of the input.
Andrew FurtadoPosted Nov 28, 2006, 11:36 AM
The logics:
1. Enter the infinite loop;
{
2. Get from user an input;
3. Try to convert the input to double;
4. If success then return value (breaks the loop & exits from the function);
5. If convertation error occures then inform the user about it;
6. Goto step 2 (due to infinite loop)
}
mafflePosted Nov 28, 2006, 6:53 AM
but can you explain the 'while(true)'.. what is true? that inside the 'try' is true/correct? am i right?
Andrew FurtadoPosted Nov 27, 2006, 1:01 PM
mafflePosted Nov 27, 2006, 7:27 AM
it works but what i want to happen is whenever the user inputs an invalid answer, it will just erase what he has input, meaning the cursor will go back to its previous location.
Andrew FurtadoPosted Nov 26, 2006, 10:49 AM
Andrew FurtadoPosted Nov 26, 2006, 10:42 AM
Maybe the solution will be to create loop that gets user input repeatedly until "correct input" occured.
Example:
static double getQA()
{
while (true)
{
try {
double qA = Convert.ToDouble(Console.ReadLine());
return qA * 0.10;
}
catch {
Console.WriteLine("Invalid Input!");
}
}
}
mafflePosted Nov 26, 2006, 10:23 AM
but how will i setfocus for invalid input?
i mean, how will i erase his input, setfocus the cursor after informing for the invalid input
Andrew FurtadoPosted Nov 25, 2006, 2:05 PM
Your catch block really does not return value. The function must return a value even if an error occures. In your case it will be solution to return double.NaN value (see docs for more info) after informing user about "invalid input" :)
Example:
static double getQA()
{
try {
double qA = Convert.ToDouble(Console.ReadLine());
return qA * 0.10;
}
catch {
Console.WriteLine("Invalid Input!");
return double.NaN;
}
}
Reena SajithPosted Nov 25, 2006, 9:05 AM