14 Replies - 739 Views - Last Post: 01 February 2010 - 07:20 AM Rate Topic: -----

#1 CaptKid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 16-August 09

errors in code

Post icon  Posted 30 January 2010 - 11:32 PM

 // Program asks user to enter password
// If password is not "candy", "sweet" or "sugar"
// the user must re-enter the password

using System;
public class DebugFive1
{
	 public static void Main()
	 {
		  const string pass1 = 'candy';
		  const string pass2 = 'sweet';
		  const string pass3 = 'sugar';
		  string password;
		  Console.Write(Please enter your password);
		  password = Console.ReadLine();
		  while(password != pass1 || password != pass2 || password != pass3)
				   password = Console.ReadLine();
		  Console.WriteLine("Invalid password. Please enter again. ");
		  Console.WriteLine("Valid password");
	 }
}
ok i know i have some errors in here any help finding them would be great i know it is late that might be why i have errors in my code.

Is This A Good Question/Topic? 0
  • +

Replies To: errors in code

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: errors in code

Posted 30 January 2010 - 11:35 PM

I think you asked this question already, and I answered, but I'll help you out again.

One error I see is that generally speaking Console Applications have the Main method declared like this:

static void Main(string[] args)

What errors are you getting?

I suggest creating a new console application, and then copy paste your current code to avoid unnecessary exceptions.


Edit: You are declaring string variables. When creating a string variable always use " and not the single '. Only use ' for declaring chars.


Edit 2: Try this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            const string pass1 = "candy";
            const string pass2 = "sweet";
            const string pass3 = "sugar";

            string password;

            Console.WriteLine("Please enter your password: ");
            password = Console.ReadLine();

            while (password != pass1 && password != pass2 && password != pass3)
	        {
                Console.WriteLine("Incorrect password. Try again: ");                    
                password = Console.ReadLine();                    

        	}

            Console.WriteLine("Valid password");
            Console.ReadLine();
        }
    }
}



Please take your time and read the solution so you can understand how some things work. You'll learn many more things! :)

This post has been edited by stapia.gutierrez: 30 January 2010 - 11:44 PM

Was This Post Helpful? 0
  • +
  • -

#3 CaptKid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 16-August 09

Re: errors in code

Posted 30 January 2010 - 11:46 PM

View Poststapia.gutierrez, on 30 Jan, 2010 - 10:35 PM, said:

I think you asked this question already, and I answered, but I'll help you out again.

One error I see is that generally speaking Console Applications have the Main method declared like this:

static void Main(string[] args)

What errors are you getting?

I suggest creating a new console application, and then copy paste your current code to avoid unnecessary exceptions.


Edit: You are declaring string variables. When creating a string variable always use " and not the single '. Only use ' for declaring chars.

thanks for the help you have been a good and helpful person to some one who is trying to learn something new
Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: errors in code

Posted 30 January 2010 - 11:47 PM

No problem, just remember to really read the solution and understand what's going on and you'll get better in no time. :) BABY STEPS!
Was This Post Helpful? 0
  • +
  • -

#5 CaptKid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 16-August 09

Re: errors in code

Posted 30 January 2010 - 11:52 PM

View Poststapia.gutierrez, on 30 Jan, 2010 - 10:47 PM, said:

No problem, just remember to really read the solution and understand what's going on and you'll get better in no time. :) BABY STEPS!
if (x > 10)
if (x <= 15)
Console.WriteLine("simple message"); ok so how would one go about Rewrite the following nested if statements as a single equivalent if statement if you could help if not no worries just trying to pick up as much as i can
Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: errors in code

Posted 30 January 2010 - 11:56 PM

It's best to create new threads with completely different questions because that way other people can find a similar solution to a similar problem. :D

The way you want to declare those in a single if statement do the following:

if (x > 10 && x <=15)
            {
                Console.WriteLine("It works!");
            }



It translates to: "If X is greater than 10 AND X is less/equal than 15, THEN you can write to the console."

This post has been edited by stapia.gutierrez: 30 January 2010 - 11:57 PM

Was This Post Helpful? 0
  • +
  • -

#7 CaptKid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 16-August 09

Re: errors in code

Posted 31 January 2010 - 12:04 AM

View Poststapia.gutierrez, on 30 Jan, 2010 - 10:56 PM, said:

It's best to create new threads with completely different questions because that way other people can find a similar solution to a similar problem. :D

The way you want to declare those in a single if statement do the following:

if (x > 10 && x <=15)
            {
                Console.WriteLine("It works!");
            }



It translates to: "If X is greater than 10 AND X is less/equal than 15, THEN you can write to the console."

ok do you have room for that group you are trying to teach for people that are just starting out in c#
Was This Post Helpful? 0
  • +
  • -

#8 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: errors in code

Posted 31 January 2010 - 12:06 AM

Yeah, room for 1 more person. We're starting tomorrow in the evening.

If you'd like to join, download Skype and have a microphone ready. We're starting with a bang!
Was This Post Helpful? 0
  • +
  • -

#9 CaptKid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 16-August 09

Re: errors in code

Posted 31 January 2010 - 12:09 AM

View Poststapia.gutierrez, on 30 Jan, 2010 - 11:06 PM, said:

Yeah, room for 1 more person. We're starting tomorrow in the evening.

If you'd like to join, download Skype and have a microphone ready. We're starting with a bang!

i would like to but i am on call for work so i dont know how much i would be able to make they have been working me none stop.
Was This Post Helpful? 0
  • +
  • -

#10 CaptKid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 16-August 09

Re: errors in code

Posted 31 January 2010 - 12:28 AM

10 % 3 would be 3 if i did my math right or am i worng
Was This Post Helpful? 0
  • +
  • -

#11 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: errors in code

Posted 31 January 2010 - 12:29 AM

% is the modulus symbol. It means: What's left after I divide these two numbers?

So:

10%3 = 1
Was This Post Helpful? 0
  • +
  • -

#12 CaptKid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 16-August 09

Re: errors in code

Posted 31 January 2010 - 12:33 AM

one more thing be for i go expressions is equivalent to X || Y && W || Z
i got this i am wrong i think X || (Y && W) || Z or could it be this X || Y || W || Z
Was This Post Helpful? 0
  • +
  • -

#13 sidoghosh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-February 10

Re: errors in code

Posted 01 February 2010 - 12:29 AM

Try this in the main program....

const string pass1 = "candy";
const string pass2 = "sweet";
const string pass3 = "sugar";
string password;
Console.Write("Please enter your password");
password = Console.ReadLine();
if (password == pass1 || password == pass2 || password ==pass3)
{
Console.WriteLine("Valid password");
}
else
{
Console.WriteLine("Invalid password. Please enter again. ");
}
Was This Post Helpful? 0
  • +
  • -

#14 Lava79  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 145
  • Joined: 31-January 10

Re: errors in code

Posted 01 February 2010 - 01:31 AM

That's damn near exactly what I did.. I think I had 2 less curly brackets, but it still ran fine.
Was This Post Helpful? 0
  • +
  • -

#15 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: errors in code

Posted 01 February 2010 - 07:20 AM

Hehehe, are you guys in the same class?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1