Convert to C# //Price Check.cpp //Uses a two-dimensional array to store a price
ID: 3722060 • Letter: C
Question
Convert to C#
//Price Check.cpp
//Uses a two-dimensional array to store a price list;
//displays the price associated with a product ID
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;
int main()
{ //declare variable and array
string searchForId = "";
string products[5][2] = {{"BX35", "13.00"},
{"CR20", "10.00"},
{"FE15", "12.00"},
{"KW10", "24.00"},
{"MM67", "4.00"}};
//get ID to search for, then convert to uppercase
cout << "Enter ID (X to exit): ";
getline(cin, searchForId);
transform(searchForId.begin(), searchForId.end(), searchForId.begin(), toupper);
while (searchForId != "X")
{
//locate position of product ID in the first column in the array
int row = 0; //keeps track of array subscripts
while (row < 5 && products[row][0] != searchForId)
{
row = row + 1;
}//end while
//if ID was found, display price from the second column in the array
//otherwise, display error message
if (row < 5)
{
cout << setiosflags(ios::fixed) << setprecision(2)
<< "Price for product ID " << products[row][0]
<< ": $" << products[row][1] << endl << endl;
}
else
{
cout << "Invalid product ID" << endl << endl;
}//end if
//get ID to search for, then convert to uppercase
cout << "Enter ID (X to exit): ";
getline(cin, searchForId);
transform(searchForId.begin(), searchForId.end(), searchForId.begin(), toupper);
}//end while
return 0;
}//end of main function
Explanation / Answer
// Here is the converted code for the already give C++ code.
// P.S I hope this works upto your expectations, do give feedback for my improvements, Thanks.
// Also not adding extra lines of comments to explain code as this is similar to C++ code.
using System;
namespace Product{
class ProductInfo{
static int Main()
{ //declare variable and array
System.String searchForId = "";
System.String[,] products =
{
{"BX35", "13.00"},
{"CR20", "10.00"},
{"FE15", "12.00"},
{"KW10", "24.00"},
{"MM67", "4.00"}
};
//get ID to search for, then convert to uppercase
Console.Write("Enter ID (X to exit): ");
searchForId = Console.ReadLine();
searchForId = searchForId.ToUpper();
while (searchForId != "X")
{
//locate position of product ID in the first column in the array
int row = 0; //keeps track of array subscripts
while (row < 5 && products[row, 0] != searchForId)
{
row = row + 1;
} //end while
//if ID was found, display price from the second column in the array
//otherwise, display error message
if (row < 5)
{
Console.Write("{0:2}", "Price for product ID ");
Console.Write("{0:2}", products[row, 0]);
Console.Write("{0:2}", ": $");
Console.Write("{0:2}", products[row, 1]);
Console.Write("{0:2}", " ");
Console.Write("{0:2}", " ");
}
else
{
Console.Write("{0:2}", "Invalid product ID");
Console.Write("{0:2}", " ");
Console.Write("{0:2}", " ");
} //end if
//get ID to search for, then convert to uppercase
Console.Write("{0:2}", "Enter ID (X to exit): ");
searchForId = Console.ReadLine();
searchForId = searchForId.ToUpper();
} //end while
return 0;
} //end of main function
}
}