Reputation: 25
I have a question about the following code I have written for a C# Programming course assignment. Everything seems to be functioning correctly except for the if statement on line 31 which checks if the input string fed into a conversion method matches the requirements given in the if statement.
The logic chain of the if statements is as follows:
For some reason, the logic is failing at step 7 and remaining in the loop, continuously demanding the user input a valid value even though it has received a valid value. Perhaps there is something wrong with how I am comparing the strings? Obviously, there is something wrong here, but I'm unable to put my finger on it.
Here is the source code:
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
public class Tables {
static int chairs = 0;
static string wood = string.Empty;
static bool run = true;
const double CHAIR_PRICE = 50.00;
static Dictionary<string, double> PRICES = new Dictionary<string, double> {
{ "pine", 250.00 },
{ "maple", 300.00 },
{ "oak", 350.00 }
};
public static void Main() {
while (run == true) {
if (chairs < 2) {
Console.WriteLine("Enter the number of chairs: ");
chairs = chairCount(Console.ReadLine());
//Console.WriteLine("{0}", chairs);//Debug
if (chairs < 2) {
Console.WriteLine("You must order at least two chairs.");
}
} else if (chairs >= 2 && (wood != "pine" || wood != "maple" || wood != "oak")) {
Console.WriteLine("Enter the type of wood - [p]ine, [m]aple, or [o]ak: ");
wood = woodType(Console.ReadLine());
//Console.WriteLine("{0}", wood);//Debug
if (wood != "pine" || wood != "maple" || wood != "oak") {
Console.WriteLine("You must enter p, m, or o for wood type.");
}
} else if (chairs >= 2 && (wood == "pine" || wood == "maple" || wood == "oak")) {
Console.WriteLine("You have ordered a {0} table with {1} chairs.", wood, chairs);
Console.WriteLine("Total price is {0}", costCalc(wood, chairs).ToString("C", new CultureInfo("en-us")));
run = false;
}
}
}
static int chairCount(string input) {
return Convert.ToInt32(input);
}
static string woodType(string input) {
if (input == "p") { return "pine"; }
else if (input == "m") { return "maple"; }
else if (input == "o") { return "oak"; }
else { return "invalid"; }
}
static double costCalc(string wood, int chairs) {
return PRICES[wood] + (chairs * CHAIR_PRICE);
}
}
Upvotes: 2
Views: 238
Reputation: 50100
This condition
(chairs >= 2 && (wood != "pine" || wood != "maple" || wood != "oak"))
is always going to be true if chairs >= 2
you mean
(chairs >= 2 && (wood != "pine" && wood != "maple" && wood != "oak"))
But this code can be made much much simpler
while(true){
Console.WriteLine("Enter the number of chairs: ");
chairs = chairCount(Console.ReadLine());
if (chairs < 2) {
Console.WriteLine("You must order at least two chairs.");
} else {
break
}
}
while(true){
Console.WriteLine("Enter the type of wood - [p]ine, [m]aple, or [o]ak: ");
wood = woodType(Console.ReadLine());
if (wood != "pine" && wood != "maple" && wood != "oak") {
Console.WriteLine("You must enter p, m, or o for wood type.");
} else{
break;
}
}
no big outer loop needed
Upvotes: 3