Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
C# - Comparisons (easy for anybody who knows what they're doing)
Old 02-27-2012, 04:25 PM C# - Comparisons (easy for anybody who knows what they're doing)
Physicsguy's Avatar
404 - Title not found

Posts: 1,061
Name: Scott Kaye
Location: Ontario
Trades: 0
So, I'm starting C# in class. *Removed*

I'm trying to make a simple console application that asks the user 'do you like cheese?' and if the user types 'yes' it says 'Good, I like cheese too', and if the user types 'no', it continues and asks 'why not?'. Useless, but I usually do this kind of crap to learn the basics.

So far, I have:

Code:
using System;

namespace SampleNamespace
{
    public class SampleClass
    {
        public static void Main()
        {
            // Write hello world to the console
            System.Console.WriteLine("What is your name?");
            string Name = System.Console.ReadLine();
            System.Console.WriteLine("Hello " + Name + "!");
            System.Console.WriteLine("Do you like cheese?");
            string Answer = System.Console.ReadLine();
            
            /*COMPARE?
switch (Answer.ToLower){
  case 'yes':
    --More questions here, like what kind of cheese and such
  break;
  case 'no':
    --The usual
  break;
  default:
    --"What did you say?  (Prompt the previous question)
  break;
}
            */
            System.Console.WriteLine("Press any key to exit the application");
            System.Console.Read();
          
        } // End of Main function (program statup)
    } // End of SampleClass
} // End of SampleNamespace
That's all fine and dandy, except I have no idea how to do the part commented out. That styling of (?) code would work in PHP, but it doesn't in C#. I get errors like 'too many characters in character literal', pointing to the 'case's.

Also, is there a way to restrict the user from only choosing "yes" or "no"?

I've looked, but this higher-level programming is confusing, when I'm used to PHP, which is turning out to be very different. With a quick look, C# looks like JavaScript (.Properties), but it's obviously more than that.

Thank you!
-PG

By the way, this isn't my homework or anything. I just hate learning at the speed of everybody else when I'm known in my classroom as the 'programmer' (oddly enough must be the PHP). While the rest of my class 'learns' what variables are, I want to be doing something a little more advanced. I just don't like learning slowly , I don't mean to sound arrogant or anything.
__________________

Please login or register to view this content. Registration is FREE

Last edited by Physicsguy; 04-24-2012 at 04:26 PM..
Physicsguy is offline
Reply With Quote
View Public Profile Visit Physicsguy's homepage!
 
 
Register now for full access!
Old 02-27-2012, 09:36 PM Re: C# - Comparisons (easy for anybody who knows what they're doing)
NullPointer's Avatar
Will Code for Food

Posts: 2,883
Name: Matt
Location: Irvine, CA
Trades: 0
It's been a while since I've written any C#, but from what I recall, literal characters are encased in single quotes whereas strings are encased in double quotes. The expression 'yes' is invalid because C# is expecting a char, but getting a string.

I don't remember if you can use a switch statement with strings in C#. In Java you can only use primitive types (int, double, char, etc..). You may need to use an if..elseif..else instead.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 04-19-2012, 06:54 AM Re: C# - Comparisons (easy for anybody who knows what they're doing)
Junior Talker

Posts: 1
Name: Basem
Location: Egypt
Trades: 0
Here is the code buddy .

Code:
        static void Main(string[] args)
        {
            // Write hello world to the console
            System.Console.WriteLine("What is your name?");
            string Name = System.Console.ReadLine();
            System.Console.WriteLine("Hello " + Name + "!");
            System.Console.WriteLine("Do you like cheese?");
            string Answer = System.Console.ReadLine();

            if (Answer.ToLower() == "yes")
            {
                // more questions
                Console.WriteLine("Great i love cheese too");
            }
            else if (Answer.ToLower() == "no")
            {
                // The usual
                Console.WriteLine("How come you do not like cheese?");
            }
            else
            {
                Console.WriteLine("What did you say?");
            }
        }
And please remember : Take it easy when you are coding , you do not have to do some complex function while it could only take 4 lines of coding

Last edited by rvector; 04-19-2012 at 07:00 AM..
rvector is offline
Reply With Quote
View Public Profile Visit rvector's homepage!
 
Reply     « Reply to C# - Comparisons (easy for anybody who knows what they're doing)
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13638 seconds with 11 queries