How to get a console to read through all conditions












2















I have a console application that asks the user to choose one of three options and to be able to open an inventory if desired. However, instead of checking to see if any of the other conditions are true are false, the console just reads the conditions linearly and waits till the first one has been satisfied. So for example, in the code below, it runs the first bit of text, presents the options, and then waits for the user to enter "inventory" without considering the other options. What's up? Why does this happen? and how do I get the console to run through and check whether or not all conditions have been satisfied?



Here's the code



using System;
using System.IO;

namespace Feed_de_monky
{
class Program
{

static void Main(string args)
{
Console.ForegroundColor = ConsoleColor.Green;
string one = "";
string two = "";
string inventory = "inventory";
int storyint = 0;
TextReader input = Console.In;
TextWriter output = Console.Out;
int options = 0;

if (storyint == 0)
{
Console.WriteLine("You are in a dark Jungle. You look into the darkness of the trees and see the silhouette of a tiger standing in front of you down the way.");
Console.WriteLine("");
Console.WriteLine("turn and run");
Console.WriteLine("pounce on tiger");
Console.WriteLine("climb a tree.");
options++;

if (input.ReadLine() == inventory)
{
output.WriteLine(one);
output.WriteLine(two);
return;
}

else if(input.ReadLine() == "turn and run" && options == 1)
{
output.WriteLine("");
output.WriteLine("The tiger chases you through the darkness. You never had a chance.");
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}

else if(input.ReadLine() == "pounce on tiger" && options == 1)
{
output.WriteLine("");
output.WriteLine("The tiger is caught by surprise. You overwhelm the beast and he dies of shock and surprise on the spot");
one = "tiger skin";
output.WriteLine("TIGER SKIN ADDED TO YOUR INVENTORY");
storyint++;
options++;
}

else if(input.ReadLine() == "climb a tree" && options == 1)
{
output.WriteLine("");
output.WriteLine("You climb the tree. But while you sit on the branches believing yourself to be safe, the tiger jumps through the air and bites your head clean off. You never had a chance.");
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}

Console.Write("Press any key to continue...");
Console.ReadKey(true);
}


}
}


}










share|improve this question


















  • 1





    is it because options != 1

    – JBoothUA
    Nov 13 '18 at 0:26











  • is " climb a tree. " always the last output before the issue?

    – JBoothUA
    Nov 13 '18 at 0:27











  • You should only call ReadLine() once, then test that result in all your if/else values. As it stands, the first if condition executes and reads a line. If you didn't enter "inventory", then the next else if executes and reads another line. Now if you enter anything except "turn and run", the next else if will read a line.

    – Rufus L
    Nov 13 '18 at 0:53


















2















I have a console application that asks the user to choose one of three options and to be able to open an inventory if desired. However, instead of checking to see if any of the other conditions are true are false, the console just reads the conditions linearly and waits till the first one has been satisfied. So for example, in the code below, it runs the first bit of text, presents the options, and then waits for the user to enter "inventory" without considering the other options. What's up? Why does this happen? and how do I get the console to run through and check whether or not all conditions have been satisfied?



Here's the code



using System;
using System.IO;

namespace Feed_de_monky
{
class Program
{

static void Main(string args)
{
Console.ForegroundColor = ConsoleColor.Green;
string one = "";
string two = "";
string inventory = "inventory";
int storyint = 0;
TextReader input = Console.In;
TextWriter output = Console.Out;
int options = 0;

if (storyint == 0)
{
Console.WriteLine("You are in a dark Jungle. You look into the darkness of the trees and see the silhouette of a tiger standing in front of you down the way.");
Console.WriteLine("");
Console.WriteLine("turn and run");
Console.WriteLine("pounce on tiger");
Console.WriteLine("climb a tree.");
options++;

if (input.ReadLine() == inventory)
{
output.WriteLine(one);
output.WriteLine(two);
return;
}

else if(input.ReadLine() == "turn and run" && options == 1)
{
output.WriteLine("");
output.WriteLine("The tiger chases you through the darkness. You never had a chance.");
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}

else if(input.ReadLine() == "pounce on tiger" && options == 1)
{
output.WriteLine("");
output.WriteLine("The tiger is caught by surprise. You overwhelm the beast and he dies of shock and surprise on the spot");
one = "tiger skin";
output.WriteLine("TIGER SKIN ADDED TO YOUR INVENTORY");
storyint++;
options++;
}

else if(input.ReadLine() == "climb a tree" && options == 1)
{
output.WriteLine("");
output.WriteLine("You climb the tree. But while you sit on the branches believing yourself to be safe, the tiger jumps through the air and bites your head clean off. You never had a chance.");
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}

Console.Write("Press any key to continue...");
Console.ReadKey(true);
}


}
}


}










share|improve this question


















  • 1





    is it because options != 1

    – JBoothUA
    Nov 13 '18 at 0:26











  • is " climb a tree. " always the last output before the issue?

    – JBoothUA
    Nov 13 '18 at 0:27











  • You should only call ReadLine() once, then test that result in all your if/else values. As it stands, the first if condition executes and reads a line. If you didn't enter "inventory", then the next else if executes and reads another line. Now if you enter anything except "turn and run", the next else if will read a line.

    – Rufus L
    Nov 13 '18 at 0:53
















2












2








2








I have a console application that asks the user to choose one of three options and to be able to open an inventory if desired. However, instead of checking to see if any of the other conditions are true are false, the console just reads the conditions linearly and waits till the first one has been satisfied. So for example, in the code below, it runs the first bit of text, presents the options, and then waits for the user to enter "inventory" without considering the other options. What's up? Why does this happen? and how do I get the console to run through and check whether or not all conditions have been satisfied?



Here's the code



using System;
using System.IO;

namespace Feed_de_monky
{
class Program
{

static void Main(string args)
{
Console.ForegroundColor = ConsoleColor.Green;
string one = "";
string two = "";
string inventory = "inventory";
int storyint = 0;
TextReader input = Console.In;
TextWriter output = Console.Out;
int options = 0;

if (storyint == 0)
{
Console.WriteLine("You are in a dark Jungle. You look into the darkness of the trees and see the silhouette of a tiger standing in front of you down the way.");
Console.WriteLine("");
Console.WriteLine("turn and run");
Console.WriteLine("pounce on tiger");
Console.WriteLine("climb a tree.");
options++;

if (input.ReadLine() == inventory)
{
output.WriteLine(one);
output.WriteLine(two);
return;
}

else if(input.ReadLine() == "turn and run" && options == 1)
{
output.WriteLine("");
output.WriteLine("The tiger chases you through the darkness. You never had a chance.");
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}

else if(input.ReadLine() == "pounce on tiger" && options == 1)
{
output.WriteLine("");
output.WriteLine("The tiger is caught by surprise. You overwhelm the beast and he dies of shock and surprise on the spot");
one = "tiger skin";
output.WriteLine("TIGER SKIN ADDED TO YOUR INVENTORY");
storyint++;
options++;
}

else if(input.ReadLine() == "climb a tree" && options == 1)
{
output.WriteLine("");
output.WriteLine("You climb the tree. But while you sit on the branches believing yourself to be safe, the tiger jumps through the air and bites your head clean off. You never had a chance.");
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}

Console.Write("Press any key to continue...");
Console.ReadKey(true);
}


}
}


}










share|improve this question














I have a console application that asks the user to choose one of three options and to be able to open an inventory if desired. However, instead of checking to see if any of the other conditions are true are false, the console just reads the conditions linearly and waits till the first one has been satisfied. So for example, in the code below, it runs the first bit of text, presents the options, and then waits for the user to enter "inventory" without considering the other options. What's up? Why does this happen? and how do I get the console to run through and check whether or not all conditions have been satisfied?



Here's the code



using System;
using System.IO;

namespace Feed_de_monky
{
class Program
{

static void Main(string args)
{
Console.ForegroundColor = ConsoleColor.Green;
string one = "";
string two = "";
string inventory = "inventory";
int storyint = 0;
TextReader input = Console.In;
TextWriter output = Console.Out;
int options = 0;

if (storyint == 0)
{
Console.WriteLine("You are in a dark Jungle. You look into the darkness of the trees and see the silhouette of a tiger standing in front of you down the way.");
Console.WriteLine("");
Console.WriteLine("turn and run");
Console.WriteLine("pounce on tiger");
Console.WriteLine("climb a tree.");
options++;

if (input.ReadLine() == inventory)
{
output.WriteLine(one);
output.WriteLine(two);
return;
}

else if(input.ReadLine() == "turn and run" && options == 1)
{
output.WriteLine("");
output.WriteLine("The tiger chases you through the darkness. You never had a chance.");
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}

else if(input.ReadLine() == "pounce on tiger" && options == 1)
{
output.WriteLine("");
output.WriteLine("The tiger is caught by surprise. You overwhelm the beast and he dies of shock and surprise on the spot");
one = "tiger skin";
output.WriteLine("TIGER SKIN ADDED TO YOUR INVENTORY");
storyint++;
options++;
}

else if(input.ReadLine() == "climb a tree" && options == 1)
{
output.WriteLine("");
output.WriteLine("You climb the tree. But while you sit on the branches believing yourself to be safe, the tiger jumps through the air and bites your head clean off. You never had a chance.");
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}

Console.Write("Press any key to continue...");
Console.ReadKey(true);
}


}
}


}







c# if-statement console condition multiple-conditions






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 0:23









Kelsey R.Kelsey R.

173




173








  • 1





    is it because options != 1

    – JBoothUA
    Nov 13 '18 at 0:26











  • is " climb a tree. " always the last output before the issue?

    – JBoothUA
    Nov 13 '18 at 0:27











  • You should only call ReadLine() once, then test that result in all your if/else values. As it stands, the first if condition executes and reads a line. If you didn't enter "inventory", then the next else if executes and reads another line. Now if you enter anything except "turn and run", the next else if will read a line.

    – Rufus L
    Nov 13 '18 at 0:53
















  • 1





    is it because options != 1

    – JBoothUA
    Nov 13 '18 at 0:26











  • is " climb a tree. " always the last output before the issue?

    – JBoothUA
    Nov 13 '18 at 0:27











  • You should only call ReadLine() once, then test that result in all your if/else values. As it stands, the first if condition executes and reads a line. If you didn't enter "inventory", then the next else if executes and reads another line. Now if you enter anything except "turn and run", the next else if will read a line.

    – Rufus L
    Nov 13 '18 at 0:53










1




1





is it because options != 1

– JBoothUA
Nov 13 '18 at 0:26





is it because options != 1

– JBoothUA
Nov 13 '18 at 0:26













is " climb a tree. " always the last output before the issue?

– JBoothUA
Nov 13 '18 at 0:27





is " climb a tree. " always the last output before the issue?

– JBoothUA
Nov 13 '18 at 0:27













You should only call ReadLine() once, then test that result in all your if/else values. As it stands, the first if condition executes and reads a line. If you didn't enter "inventory", then the next else if executes and reads another line. Now if you enter anything except "turn and run", the next else if will read a line.

– Rufus L
Nov 13 '18 at 0:53







You should only call ReadLine() once, then test that result in all your if/else values. As it stands, the first if condition executes and reads a line. If you didn't enter "inventory", then the next else if executes and reads another line. Now if you enter anything except "turn and run", the next else if will read a line.

– Rufus L
Nov 13 '18 at 0:53














1 Answer
1






active

oldest

votes


















2














I think you might need to set



var inputLine = input.ReadLine();


And then do your logic on the variable inputLine.



As you have it now I believe it will call ReadLine more times than you are expecting. But if you just call .ReadLine() one time and assign it to a variable that should act better than calling it repeatedly.






share|improve this answer



















  • 1





    Thanks. You were right. I added your line into the if(storyint == 0){} and it worked perfectly.

    – Kelsey R.
    Nov 13 '18 at 5:38











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272038%2fhow-to-get-a-console-to-read-through-all-conditions%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














I think you might need to set



var inputLine = input.ReadLine();


And then do your logic on the variable inputLine.



As you have it now I believe it will call ReadLine more times than you are expecting. But if you just call .ReadLine() one time and assign it to a variable that should act better than calling it repeatedly.






share|improve this answer



















  • 1





    Thanks. You were right. I added your line into the if(storyint == 0){} and it worked perfectly.

    – Kelsey R.
    Nov 13 '18 at 5:38
















2














I think you might need to set



var inputLine = input.ReadLine();


And then do your logic on the variable inputLine.



As you have it now I believe it will call ReadLine more times than you are expecting. But if you just call .ReadLine() one time and assign it to a variable that should act better than calling it repeatedly.






share|improve this answer



















  • 1





    Thanks. You were right. I added your line into the if(storyint == 0){} and it worked perfectly.

    – Kelsey R.
    Nov 13 '18 at 5:38














2












2








2







I think you might need to set



var inputLine = input.ReadLine();


And then do your logic on the variable inputLine.



As you have it now I believe it will call ReadLine more times than you are expecting. But if you just call .ReadLine() one time and assign it to a variable that should act better than calling it repeatedly.






share|improve this answer













I think you might need to set



var inputLine = input.ReadLine();


And then do your logic on the variable inputLine.



As you have it now I believe it will call ReadLine more times than you are expecting. But if you just call .ReadLine() one time and assign it to a variable that should act better than calling it repeatedly.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 0:28









JBoothUAJBoothUA

835424




835424








  • 1





    Thanks. You were right. I added your line into the if(storyint == 0){} and it worked perfectly.

    – Kelsey R.
    Nov 13 '18 at 5:38














  • 1





    Thanks. You were right. I added your line into the if(storyint == 0){} and it worked perfectly.

    – Kelsey R.
    Nov 13 '18 at 5:38








1




1





Thanks. You were right. I added your line into the if(storyint == 0){} and it worked perfectly.

– Kelsey R.
Nov 13 '18 at 5:38





Thanks. You were right. I added your line into the if(storyint == 0){} and it worked perfectly.

– Kelsey R.
Nov 13 '18 at 5:38


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272038%2fhow-to-get-a-console-to-read-through-all-conditions%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma