Another if statement question in C# [SOLVED]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Another if statement question in C# [SOLVED]

Post by mattykins »

So I was working on my life sim game and programming the school option.

Basically its setup like this

Code: Select all

if(Player.Money > 75)
{
     Show options avaliable to players with more than 75$
}
if(Player.Money > 30 & Player.Money < 75)
{
     Show options avaliable to players with less than 75$ but more than 30$
}
if(Player.Money < 30)
{
     Show options avaliable to players with less than 30$
}
This displays no errors in the error box. But when i test it in game if I have less than 30$ The game basically skips over the if statements and goes back to the game loop.
Last edited by mattykins on Sat Jan 21, 2012 1:53 am, edited 1 time in total.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Another if statement question in C#

Post by Jackolantern »

You are missing some currency amounts. You only have > or <. If someone has exactly $30 or $75, none of those IF statements apply to them. You need to add >= and/or <= to add "...or equal to..." to the conditions. Decide which condition you want $30 and $75 to apply to, and change them to >= or <=.
The indelible lord of tl;dr
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: Another if statement question in C#

Post by mattykins »

Thanks, I added those, however the problem persists...
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Another if statement question in C#

Post by Jackolantern »

This isn't enough code to solve any other problems. The problem must be elsewhere.
The indelible lord of tl;dr
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: Another if statement question in C#

Post by mattykins »

Here i'll post my full school class, maybe this will help.
Sorry there is a LOT of Console.WriteLines but its a text based game...hahaha :)

Code: Select all

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

namespace ConsoleApplication1
{
    class School
    {
        static public void atschool()
        {
            if (Player.Money >= 75)
            {
                Console.Clear();
                Console.WriteLine("================================================================================");
                Console.WriteLine("Welcome to your local community college.");
                Console.WriteLine("What would you like to do?");
                Console.WriteLine("You can 'study' for free,");
                Console.WriteLine("You can 'attend' a class for 30$");
                Console.WriteLine("You can take a 'private' lesson for 75$");
                Console.WriteLine("\n================================================================================");
                string action = Convert.ToString(Console.ReadLine());
                if (action == "study")
                {
                    Console.Clear();
                    Console.WriteLine("================================================================================");
                    Console.WriteLine("You enter the library, pull a book off the shelf, and begin to stufy.");
                    Console.WriteLine("Smarts + 2");
                    Console.WriteLine("\n================================================================================");
                    Console.WriteLine("(Press any key to continue)");
                    Player.Smarts += 2;
                    Console.ReadKey();

                }
                if (action == "attend")
                {
                    Console.Clear();
                    Console.WriteLine("================================================================================");
                    Console.WriteLine("You find your seat among other students, some younger, some older.");
                    Console.WriteLine("You get out your pencil and paper and look up right as the professor enters the");
                    Console.WriteLine("room. Everyone falls silent and the class begins.");
                    Console.WriteLine("Smarts + 5");
                    Console.WriteLine("Money - 30");
                    Console.WriteLine("\n================================================================================");
                    Console.WriteLine("(Press any key to continue.");
                    Player.Smarts += 5;
                    Player.Money -= 30;
                    Console.ReadKey();

                }
                if (action == "private")
                {
                    Console.Clear();
                    Console.WriteLine("================================================================================");
                    Console.WriteLine("The outgoing class walks right by you as you walk into the empty classroom.");
                    Console.WriteLine("The professor looks to be in a rather good mood today, and asks if you are");
                    Console.WriteLine("ready to begin your private lesson. You nod yes, sit down, and begin.");
                    Console.WriteLine("Smarts + 10");
                    Console.WriteLine("Money - 75");
                    Console.WriteLine("\n================================================================================");
                    Console.WriteLine("(Press any key to continue)");
                    Player.Smarts += 10;
                    Player.Money -= 75;
                    Console.ReadKey();
                }
            }
            if (Player.Money >= 30 & Player.Money < 75)
            {
                Console.Clear();
                Console.WriteLine("================================================================================");
                Console.WriteLine("Welcome to your local community college.");
                Console.WriteLine("What would you like to do?");
                Console.WriteLine("You can 'study' for free,");
                Console.WriteLine("You can 'attend' a class for 30$");
                Console.WriteLine("You cannot afford to take a private lesson for 75$");
                Console.WriteLine("\n================================================================================");
                string action = Convert.ToString(Console.ReadLine());
                if (action == "study")
                {
                    Console.Clear();
                    Console.WriteLine("================================================================================");
                    Console.WriteLine("You enter the library, pull a book off the shelf, and begin to stufy.");
                    Console.WriteLine("Smarts + 2");
                    Console.WriteLine("\n================================================================================");
                    Console.WriteLine("(Press any key to continue)");
                    Player.Smarts += 2;
                    Console.ReadKey();

                }
                if (action == "attend")
                {
                    Console.Clear();
                    Console.WriteLine("================================================================================");
                    Console.WriteLine("You find your seat among other students, some younger, some older.");
                    Console.WriteLine("You get out your pencil and paper and look up right as the professor enters the");
                    Console.WriteLine("room. Everyone falls silent and the class begins.");
                    Console.WriteLine("Smarts + 5");
                    Console.WriteLine("Money - 30");
                    Console.WriteLine("\n================================================================================");
                    Console.WriteLine("(Press any key to continue.");
                    Player.Smarts += 5;
                    Player.Money -= 30;
                    Console.ReadKey();

                }
                if (Player.Money < 30)
                {
                    {
                        Console.Clear();
                        Console.WriteLine("================================================================================");
                        Console.WriteLine("Welcome to your local community college.");
                        Console.WriteLine("What would you like to do?");
                        Console.WriteLine("You can 'study' for free,");
                        Console.WriteLine("You cannot afford to attend a class for 30$");
                        Console.WriteLine("You cannot afford to take a private lesson for 75$");
                        Console.WriteLine("\n================================================================================");
                        action = Convert.ToString(Console.ReadLine());
                        if (action == "study")
                        {
                            Console.Clear();
                            Console.WriteLine("================================================================================");
                            Console.WriteLine("You enter the library, pull a book off the shelf, and begin to stufy.");
                            Console.WriteLine("Smarts + 2");
                            Console.WriteLine("\n================================================================================");
                            Console.WriteLine("(Press any key to continue)");
                            Player.Smarts += 2;
                            Console.ReadKey();


                        }
                    }


                }
            }
        }
    }
}

PKDemon
Posts: 91
Joined: Tue Nov 15, 2011 5:51 pm

Re: Another if statement question in C#

Post by PKDemon »

couldn't u just combine all the if statements into one instead of three like this

Code: Select all


public static void atschool()
{
	if (Player.Money >= 75) {
		Console.Clear();
		Console.WriteLine("================================================================================");
		Console.WriteLine("Welcome to your local community college.");
		Console.WriteLine("What would you like to do?");
		Console.WriteLine("You can 'study' for free,");
		Console.WriteLine("You can 'attend' a class for 30$");
		Console.WriteLine("You can take a 'private' lesson for 75$");
		Console.WriteLine(Constants.vbLf + "================================================================================");
		string action = Convert.ToString(Console.ReadLine());
		if (action == "study") {
			Console.Clear();
			Console.WriteLine("================================================================================");
			Console.WriteLine("You enter the library, pull a book off the shelf, and begin to stufy.");
			Console.WriteLine("Smarts + 2");
			Console.WriteLine(Constants.vbLf + "================================================================================");
			Console.WriteLine("(Press any key to continue)");
			Player.Smarts += 2;

			Console.ReadKey();
		}
		if (action == "attend") {
			Console.Clear();
			Console.WriteLine("================================================================================");
			Console.WriteLine("You find your seat among other students, some younger, some older.");
			Console.WriteLine("You get out your pencil and paper and look up right as the professor enters the");
			Console.WriteLine("room. Everyone falls silent and the class begins.");
			Console.WriteLine("Smarts + 5");
			Console.WriteLine("Money - 30");
			Console.WriteLine(Constants.vbLf + "================================================================================");
			Console.WriteLine("(Press any key to continue.");
			Player.Smarts += 5;
			Player.Money -= 30;

			Console.ReadKey();
		}
		if (action == "private") {
			Console.Clear();
			Console.WriteLine("================================================================================");
			Console.WriteLine("The outgoing class walks right by you as you walk into the empty classroom.");
			Console.WriteLine("The professor looks to be in a rather good mood today, and asks if you are");
			Console.WriteLine("ready to begin your private lesson. You nod yes, sit down, and begin.");
			Console.WriteLine("Smarts + 10");
			Console.WriteLine("Money - 75");
			Console.WriteLine(Constants.vbLf + "================================================================================");
			Console.WriteLine("(Press any key to continue)");
			Player.Smarts += 10;
			Player.Money -= 75;
			Console.ReadKey();
		}
	} else if (Player.Money >= 30 & Player.Money < 75) {
		Console.Clear();
		Console.WriteLine("================================================================================");
		Console.WriteLine("Welcome to your local community college.");
		Console.WriteLine("What would you like to do?");
		Console.WriteLine("You can 'study' for free,");
		Console.WriteLine("You can 'attend' a class for 30$");
		Console.WriteLine("You cannot afford to take a private lesson for 75$");
		Console.WriteLine(Constants.vbLf + "================================================================================");
		string action = Convert.ToString(Console.ReadLine());
		if (action == "study") {
			Console.Clear();
			Console.WriteLine("================================================================================");
			Console.WriteLine("You enter the library, pull a book off the shelf, and begin to stufy.");
			Console.WriteLine("Smarts + 2");
			Console.WriteLine(Constants.vbLf + "================================================================================");
			Console.WriteLine("(Press any key to continue)");
			Player.Smarts += 2;

			Console.ReadKey();
		}
		if (action == "attend") {
			Console.Clear();
			Console.WriteLine("================================================================================");
			Console.WriteLine("You find your seat among other students, some younger, some older.");
			Console.WriteLine("You get out your pencil and paper and look up right as the professor enters the");
			Console.WriteLine("room. Everyone falls silent and the class begins.");
			Console.WriteLine("Smarts + 5");
			Console.WriteLine("Money - 30");
			Console.WriteLine(Constants.vbLf + "================================================================================");
			Console.WriteLine("(Press any key to continue.");
			Player.Smarts += 5;
			Player.Money -= 30;

			Console.ReadKey();

		} else if (Player.Money < 30) {
			if (true) {
				Console.Clear();
				Console.WriteLine("================================================================================");
				Console.WriteLine("Welcome to your local community college.");
				Console.WriteLine("What would you like to do?");
				Console.WriteLine("You can 'study' for free,");
				Console.WriteLine("You cannot afford to attend a class for 30$");
				Console.WriteLine("You cannot afford to take a private lesson for 75$");
				Console.WriteLine(Constants.vbLf + "================================================================================");
				action = Convert.ToString(Console.ReadLine());
				if (action == "study") {
					Console.Clear();
					Console.WriteLine("================================================================================");
					Console.WriteLine("You enter the library, pull a book off the shelf, and begin to stufy.");
					Console.WriteLine("Smarts + 2");
					Console.WriteLine(Constants.vbLf + "================================================================================");
					Console.WriteLine("(Press any key to continue)");
					Player.Smarts += 2;


					Console.ReadKey();
				}


			}
		}
	}
}

User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Another if statement question in C#

Post by Jackolantern »

I can't see the problem here. It is probably somewhere else. You should be using && (logical AND) instead of & (bit-wise AND) in your IF statements, though. For general IF statements, you always use 2 of the symbols, like && or ||. But I don't think that is the problem. I think the issue is somewhere else, but we could be here all day hunting through class files without finding it, all the while you have the most powerful tool to solve the problem on your side.

Do you know how to debug in Visual Studio? It is an awesome tool, and one I sorely miss when writing PHP. First you need to set some breakpoints. To the left of your code, on the very edge of the editor window, you will see a gray bar going down the side. If you click in that gray bar, a red "breakpoint" dot gets added to the screen. When you debug the application (by pressing F5 to run it), your application will run as normal, but then when it gets to one of the lines with a breakpoint, it will stop and go back into the editor. You can move your mouse over any of the variables in your code to see what value they currently contain (this is an amazing tool, and is boss at solving logic errors). The VS screen will also change a bit, as you are now in debug mode. On the toolbars, you should see a toolbar that looks a bit like a DVD player, with a play button, stop, etc. The play button continues on with the program as normal, at least until it hits another breakpoint. Stop stops the execution of the program and returns you to the editor. But to the right of those buttons are some very cool options. These are "Step Into", "Step Over" and "Step Out". These let you step through your program one line of code at a time! Step Into is commonly the one you will press to just go to the next line of code. However, the difference between these 3 buttons is how they treat method calls. Step Into will take you directly into the method and stops at the first line of the method body. Step Over runs the whole method without breaking, and stops the program again at the first line after the method's completion. Step Out will take you out of a method if you are already in it, letting the rest of it complete as normal and breaking again at the first line after the method completes.

All of the variable values are updated in real-time as the debugger runs every line of code individually. So this is an excellent way to see what your variable values are, and to pinpoint where the issue is. I highly suggest trying it out to solve this issue, as the debugger is your best friend, and will be the tool you use the most in Visual Studio, so you should get familiar with it now!
The indelible lord of tl;dr
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: Another if statement question in C#

Post by mattykins »

I did not even know about that! Thanks so much :) I will make a post saying if i found the error or not.

EDIT:The debug tool worked awesomely, seeing my program run line by line made me see that the 3rd if statement was actually inside of the 2nd if statement, so the program skipped over the 2nd statement, thus also skipping the 3rd. Thanks guys! :)
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Another if statement question in C#

Post by Jackolantern »

Your most welcome! :) Getting to know the debugging capabilities in Visual Studio early and being comfortable with it will help you immensely in your coding career!
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”