Help with my C# homework?

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 2588 Views - Last Post: 11 February 2011 - 08:04 AM Rate Topic: **--- 2 Votes

#1 joemalola  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 07-February 11

Help with my C# homework?

Posted 07 February 2011 - 06:26 AM

Hello,

Earlier today I begun my C# program. My goal is to make an interactive bot that works on if else statements. Bear in mind, I am still very clueless about C#, and my teacher refuses to give clear answers. here is my code:

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

namespace ConsoleApplication12
{
    class Program
    {
        static string response1;
        static string response2;
        
        static void Main(string[] args)
        {
  

            response1=
             if (response1)
             {
                 Console.WriteLine("Hello");

             }
             else
             {
                 
                
             }

         

            


        }
    }
}



What should I do to make response1 mean "Hello," or "Hi,"? Any help would be much appreciated.

MOD EDIT: Please note the big PINK box at the top of posting a new thread. Also, please note the background of the text block that you are typing in.

Please post any code within the code tags


:code:

This post has been edited by eclipsed4utoo: 07 February 2011 - 06:29 AM
Reason for edit:: adding code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Help with my C# homework?

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Help with my C# homework?

Posted 07 February 2011 - 06:34 AM

Variables are assigned values like so:

bool someBooleanValue = false;



IF statements work like so:

bool someBooleanValue = false;

if (someBooleanValue)
{
    // value is true
    // do something
}
else
{
    // value is false
    // do something else
}



Don't you have a course book? I can understand that your professor is an asshat, but you still have a book to read, correct? Assigning a value to a variable should be Chapter 1.
Was This Post Helpful? 0
  • +
  • -

#3 Christos75  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 55
  • Joined: 06-October 09

Re: Help with my C# homework?

Posted 07 February 2011 - 07:06 AM

What do you want to do exactly ?
Was This Post Helpful? 0
  • +
  • -

#4 treesap526  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 06-December 10

Re: Help with my C# homework?

Posted 07 February 2011 - 09:22 AM

Are you trying to make a program that responds to what you type? Is that what you mean? If I were you, I'd go read up on different Console methods (Console.ReadLine(), etc.) to see how to get user input. Let me know if writing a program that responds to what you type is what you want to make
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5625
  • View blog
  • Posts: 12,064
  • Joined: 02-June 10

Re: Help with my C# homework?

Posted 07 February 2011 - 04:26 PM

Canned response #2

I am going to guess that you are trying to teach yourself C# without much guidance, a decent book or without knowing where to look. Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.

But honestly, just typing away and seeing what pops up in Intellisense is going to make your self-education take 20 years. You can learn by trying to reverse engineer the language through banging on the keyboard - or you can learn by doing the tutorials and following a good "How to learn C#" book.

May I suggest picking up a basic C# introductory book? It's not that people here don't want to be helpful, but there is a certain amount of basic learning work that one should really take upon themselves before asking for help. There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" books at your local book seller or even public library... Asking a forum, any forum, to hand-hold you through it is just redundant. In many ways it disrespects the people who have invested dozens of hours in the on-line tutorials and those that spent thousands of hours in authoring books. C# resources. Start here

Intro to C# online tutorial

Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Microsoft Visual Studio Tips, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Debugging tutorial
Working with environmental variables

Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.

These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio

Have you seen the MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.

  • Anonymous Delegates: Demonstrates the use of unnamed delegates to reduce application complexity.
  • Arrays: Shows how to use arrays.
  • Attributes: Shows how to create custom attribute classes, use them in code, and query them through reflection.
  • Collection Classes: Shows how to make non-generic collection classes that can be used with the foreach statement.
  • COM Interop Part I: Shows how to use C# to interoperate with COM objects.
  • COM Interop Part II: Shows how to a use a C# server together with a C++ COM client.
  • Commandline: Demonstrates simple command-line processing and array indexing.
  • Condiational Methods: Demonstrates conditional methods, which provide a powerful mechanism by which calls to methods can be included or omitted depending on whether a symbol is defined.
  • Delegates: Shows how delegates are declared, mapped to static and instance methods, and combined into multicast delegates.
  • Events: Shows how to declare, invoke, and configure events in C#.
  • Explicit Interface: Demonstrates how to explicitly implement interface members and how to access those members from interface instances.
  • Generics: Shows how to make generic collection classes that can be used with the foreach statement.
  • Hello World: A Hello World application.
  • Indexers Part I: Shows how C# classes can declare indexers to provide array-like access to objects.
  • Indexers Part II: Shows how to implement a class that uses indexed properties. Indexed properties enable you to use a class that represents an array-like collection.
  • Libraries: Shows how to use compiler options to create a DLL from multiple source files; also, how to use the library in other programs
  • Named and Optional (C# 4.0): Demonstrates Named and Optional parameters, an alternative to method overloads
  • Nullable: Demonstrates value types, such as double and bool, that can be set to null
  • Office Sample (C# 4.0): Demonstrates how Dynamic and COM Interop make it easy to call Microsoft Office in C# 4.0
  • OLEDB: Demonstrates how to use a Microsoft Access database from C# by creating a dataset and adding tables to it.
  • Operator Overloading: Shows how user-defined classes can overload operators
  • Partial Types: Demonstrates how classes and structures can be defined in multiple C# source-code files
  • PInvoke: Shows how to call exported DLL functions from C#
  • Properties: Shows how properties are declared and used; also demonstrates abstract properties
  • Python Sample (C# 4.0): Learn how to call a Python script by using the Dynamic feature in C# 4.0
  • Security: Discusses .NET Framework security and shows how to modify security permissions in C# by using permission classes and permission attributes
  • Simple Variance (C# 4.0): See how Covariance and Contravariance are supported in generic interfaces and delegates
  • Structs: Shows how to use structs in C#.
  • Threading: Demonstrates various thread activities such as creating and executing a thread, synchronizing threads, interacting between threads, and using a thread pool
  • Unsafe: Shows how to use unmanaged code (code that uses pointers) in C#
  • User Conversions: Shows how to define conversions to and from user-defined types
  • Versioning: Demonstrates versioning in C# by using the override and new keywords
  • XML Documents: Shows how to document code by using XML
  • Yield: Demonstrates how to use the yield keyword to filter items in a collection

Was This Post Helpful? 4
  • +
  • -

#6 joemalola  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 07-February 11

Re: Help with my C# homework?

Posted 08 February 2011 - 06:10 AM

Thank you for the help. I do have a course book, but I learn best when I have different opinions. You see can see me asking questions all the time. Anyway, here is my code, after making it a string value:

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

namespace ConsoleApplication13
{
    class Program
    {
        static void Main(string[] args)
        {
            string response3 = "Hello";

            for (int i = 0; i < 10000; i++)
            {
                 
                string response1;
                string response2;

                response1 = (Console.ReadLine());
                
                if (response1 == "hello")
                {

                    Console.WriteLine("Hello");

                }
                else
                {

                    Console.WriteLine("I'm sorry?");

                }

                response2 = (Console.ReadLine());


                
               
            }
        }
    }
}



SO far it is working correctly. If-else statements work nicely.

This post has been edited by insertAlias: 08 February 2011 - 07:45 AM

Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,972
  • Joined: 08-June 10

Re: Help with my C# homework?

Posted 08 February 2011 - 07:48 AM

I'm not sure its working the way you think it is. What do response2 and response3 do at all? Nothing. They should be removed, since you don't actually use them. The last Console.ReadLine is at the end of the loop, and the first is at the beginning. So for each iteration (and you're doing 10000 iterations) you'll read, then ignore that value and read again.
Was This Post Helpful? 0
  • +
  • -

#8 Bengie25  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 53
  • Joined: 07-July 10

Re: Help with my C# homework?

Posted 08 February 2011 - 11:36 AM

don't forget to add

if(response == "bye") break;
Was This Post Helpful? 0
  • +
  • -

#9 joemalola  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 07-February 11

Re: Help with my C# homework?

Posted 08 February 2011 - 07:49 PM

To clairify, response2 has no part now, but these will make up later if-else strings.

Here is the problem that I am faced with. I am trying to present more than one option for the user to pick when making a response. For instance, when the bot says
"hello!,"

I want there to be more than one response, such as

"good day, (or) hi,"


Here is my code, it says there is a syntax error when I use '||,' or '&&':
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	 
	namespace ConsoleApplication13
	{
	    class Program
	    {
	        static void Main(string[] args)
        {

            Console.WriteLine("Hello!\nPlease Respond by typing below");

                for (int i = 0; i < 10000; i++)
	            {
                   
	                string response1;
                        string response2;
	 
	                response1 = (Console.ReadLine());
	                 
	                if (response1 == "hello" || "hi" || "good day")
	                {
	 
	                    Console.WriteLine("How are you?");
	                }
	                else
	                {
	 	                Console.WriteLine("I'm sorry?");	 
	                }



                   
                    }


                    
	 
                 
	                
	            }
	        }
	    }



I would also like to thank everyone helping me with this. You are making this much easier to understand; in which I can safely say C# is better than Java.

Edited by Dogstopper: PLEASE NOTE THE BIG PINK BOX BEFORE POSTING AND PREVIOUS WARNINGS ON THIS MATTER. LOOK IN THE BACKGROUND OF THE POST! USE CODE TAGS!

:code:
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: Help with my C# homework?

Posted 09 February 2011 - 04:48 AM

if (response1 == "hello" || "hi" || "good day")


You can't do this. You must evaluate each boolean operation separately.

if (response1 == "hello" || response1 == "hi" || response1 == "good day")

Was This Post Helpful? 1
  • +
  • -

#11 joemalola  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 07-February 11

Re: Help with my C# homework?

Posted 09 February 2011 - 06:28 AM

Thank you,

A few more questions then I will wrap this thread up.

I improved the coding and made about 3 if-else statements. A concern has been lingering, and I do not know how to make it repeat a line. For instance, when it asks:

"What is your name?"

and the user says:

"bob"

I want it to say:

"bob. that is a good name."







Another question, how do I pause the responses? I want it to be as such:

"Hello."

Pause

"How are you?






Here is my code:

	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	  
	namespace ConsoleApplication13
	{
	    class Program
	    {
	        static void Main(string[] args)
	    {	 
	        Console.WriteLine("Hello!\nPlease respond by typing below");

            for (int i = 0; i < 10000; i++)
            {
                string response1;
                
               

                response1 = (Console.ReadLine());
                
                if (response1 == "hello" || response1 == "hi" || response1 == "good day")
                
                    
                    
                {
                    Console.WriteLine("How are you?");
                }
                else
                {
                    Console.WriteLine("I'm sorry?");
                }



                



               
      
                  string response2;

                  response2 = (Console.ReadLine());

                  if (response2 == "good" || response2 == "well" || response2 == "nice" || response2 == "i am well" || response2 == "i am good")
                    {

                        Console.WriteLine("That is good");
                    }
                 else
                    {
                        Console.WriteLine("Sorry to hear that.");
                    }

           
                

                Console.WriteLine("So what is your name?");


                System.Threading.Thread.Sleep(1000);

                
                
                if (response3 == "i don't want to tell you" || response3 == "i dont want to tell you" || response3 == "i do not want to tell you" || response3 == "i do not have a name"  || response3 == "i dont have a name" || response3 == "i don't have a name")
                {

                    Console.WriteLine("Ouch. Sorry for asking");
                }
                else
                {
                    Console.WriteLine("Good name");
                }


                
                
                
                string response4;

                response4 = (Console.ReadLine());

                if (response4 == "hello" || response4 == "hi" || response4 == "good day")
                {

                    Console.WriteLine("How are you?");
                }
                else
                {
                    Console.WriteLine("I'm sorry?");
                }
            
            
            }
            
            
            
            
            
            
            
            
            }
	 
	                 
	  
	              
	                 
	    }
	 }






Thank you very much.

This post has been edited by insertAlias: 09 February 2011 - 07:21 AM

Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: Help with my C# homework?

Posted 09 February 2011 - 08:33 AM

String.Format.

Get to know MSDN and USE it!
Was This Post Helpful? 0
  • +
  • -

#13 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,972
  • Joined: 08-June 10

Re: Help with my C# homework?

Posted 09 February 2011 - 08:41 AM

I have to say, there's a lot you have to learn about design. You're doing these comparisons on long strings of input. All the user needs to do is make one typo, and it will no longer match a condition.

Even something as simple as capitalization. If they type "Hello" instead of "hello", your conditions will not match it. At the very least, convert your inputs to lower case to do your comparisons.




Second, you don't need to make a new string for each input. You can reuse variables. Once you've already used the input and no longer need it, you can reuse that variable.




Third, Console.Write and Console.WriteLine automatically do what a string.Format does, so you don't have to explicitly use string.Format. But do read the link that Jack provided and use that for your output.
Was This Post Helpful? 1
  • +
  • -

#14 Guest_bertstare*


Reputation:

Re: Help with my C# homework?

Posted 09 February 2011 - 08:58 AM

View PosttlhIn, on 07 February 2011 - 04:26 PM, said:

Have you seen the MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.


Any idea why while browsing the MSDN code samples every time I try to check one out like the one you linked there 'Visual C# 2010 Samples' I get page does not exist?? Every damn sample in there does not work. Pretty annoying
Was This Post Helpful? 0

#15 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5625
  • View blog
  • Posts: 12,064
  • Joined: 02-June 10

Re: Help with my C# homework?

Posted 09 February 2011 - 09:25 AM

View Postbertstare, on 09 February 2011 - 08:58 AM, said:

View PosttlhIn, on 07 February 2011 - 04:26 PM, said:

Have you seen the MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.


Any idea why while browsing the MSDN code samples every time I try to check one out like the one you linked there 'Visual C# 2010 Samples' I get page does not exist?? Every damn sample in there does not work. Pretty annoying


Honestly, I think you need to learn to be a little more self reliant, instead of asking everyone else the most basic of things.
As for the samples, there is a link on the "page not found" that says "code gallery home". That seemed a natural place to go, to me anyway. Guess what I saw at the top of that page...

Quote

Preview the code samples that are already migrated to http://beta.code.msdn.microsoft.com/. Visit the FAQ for more information on how to migrate your code sample and what will happen to the other resources on this site.


So in other words, MS has moved their samples and they gave links and explanations about it for those that bothered to look.

I'm sorry if this sounds a little harsh... but really... take just a little responsibility for some of your own needs.
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2