Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
so my problem is that i have the output answer for the question i was given but i don't know how to write the code for the question to show the output answer i was given

What I have tried:

C#
using System;
public static class assignment_2
{
    public static void Main()
    {

     // declare the variables and constants
     int
     double markval;
     string

     // loop to read in a valid mark or the sentinel value
        do
        {
            // Read initial mark (seed the loop)
            Console.Write("Enter a mark between 0 and 100 ('Q' or 'q' to stop): ");
            mark = Convert.ToDouble(Console.ReadLine());
        } while (mark > 100);
        // if the inputted mark is not the sentinel value, process it
    }
Posted
Updated 13-Oct-21 20:17pm
v2
Comments
Richard MacCutchan 13-Oct-21 11:37am    
That will not even compile for a start. You have two declarations without variable names, and an undeclared variable in the loop. Your loop is just accepting any value greater than 100 but doing nothing with it.

You need to go back to your course notes and read through them again.
Dave Kreskowiak 13-Oct-21 13:09pm    
On top of what Richard said, look what happens when the user types 'Q' to quit. You don't quit. You immediate try to convert that Q to a double value. That won't work.

1 solution

To add to what Richard and Dave have said, mark isn't declared at all - and markval is a totally different variable, just as ajbloke jarikpe is not the same as aj jarikpe!

And never assume user input is accurate - they make mistakes, just like everyone else. So using Convert methods of user input just means that you app crashes when they mistype, and you know how annoying that is: it's probably happened to you.
Instead, use the <type>.TryParse methods instead:
C#
double mark = double.MaxValue;
while (mark > 100)
   {
   string input = Console.ReadLine();
   if (input.ToLower() == "q")
      {
      break;
      }
   double asValue;
   if (double.TryParse(input, out asValue))
      {
      ... process the data ...
      }
   else
      {
      ... report a problem ...
      }
   }
 
Share this answer
 

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