Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help debugging C# program // Program asks user to enter a stock number // If the

ID: 3793861 • Letter: H

Question

Help debugging C# program

// Program asks user to enter a stock number // If the stock number is not 209, 312, or 414 the user must reneter the number // The program displays the correct price using static System.Console; class DebugFive2 { static void Main() { const string ITEM209 = "209"; const string ITEM312 = "312"; const string ITEM414 = "414"; const double PRICE209 = 12.99, PRICE312 = 16.77, PRICE414 = 109.07; double price; string stockNum; Write("Please enter the stock number of the item you want "); stockNum = ReadLine(); while(stockNum != ITEM209 || stockNum != ITEM312 || stockNum != ITEM414) { WriteLine("Invalid stock number. Please enter again. "); stockNum = ConsoleReadLine(); } if(stockNum == ITEM209) price = PRICE209; else if(stockNum == ITEM312) price = PRICE414; else price = PRICE312; WriteLine("The price for item # {0} is {1}}", stockNum, price.ToString("C")); } }

Explanation / Answer

Hi

I have fixed the issues and highlighted the code chanes below.

using System.IO;
using System;

using static System.Console;
class DebugFive2 {
static void Main() {
const string ITEM209 = "209";
const string ITEM312 = "312";
const string ITEM414 = "414";
const double PRICE209 = 12.99, PRICE312 = 16.77, PRICE414 = 109.07;
double price;
string stockNum;
Write("Please enter the stock number of the item you want ");
stockNum = ReadLine();
while(!(stockNum.Equals(ITEM209) || stockNum.Equals(ITEM312) || stockNum.Equals(ITEM414))) {
WriteLine("Invalid stock number. Please enter again. ");
stockNum = Console.ReadLine();
  
}
if(stockNum == ITEM209)
price = PRICE209;
else if(stockNum == ITEM312)
price = PRICE312;
else price = PRICE414;
WriteLine("The price for item # {0} is {1}", stockNum, price.ToString("C"));
  
}
  
}

Output:

sh-4.3$ mcs *.cs -out:main.exe                                                                                                                                                                                                                                         

sh-4.3$ mono main.exe                                                                                                                                                                                                                                                  

Please enter the stock number of the item you want 111                                                                                                                                                                                                                 

Invalid stock number. Please enter again.                                                                                                                                                                                                                              

312                                                                                                                                                                                                                                                                    

The price for item # 312 is ?16.77