I am a c# beginner so I have a problem in my c# console calculator here is the code
Console.WriteLine("If you want to add click 1 , subtract click 2");
int userchoice = int.Parse(Console.ReadLine());
if (userchoice == 1)
{
Console.WriteLine("Enter a number");
int firstinput = int.Parse(Console.ReadLine());
Console.WriteLine("Enter another number");
int secondinput = int.Parse(Console.ReadLine());
Console.WriteLine("Click =");
string choice = Console.ReadLine();
if (choice == "=")
{
Console.WriteLine(firstinput + secondinput);
}
else if (userchoice == 2)
{
Console.WriteLine("Enter a number");
int firstinput1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter another number");
int secondinput1 = int.Parse(Console.ReadLine());
Console.WriteLine("Click =");
string choice2 = Console.ReadLine();
if (choice2 == "=")
{
Console.WriteLine(firstinput1 - secondinput1);
}
The problem is if I entered 1 it will make the addition operation but if I entered 2 it won't do anything why although I am writing its code as what is right answer.
you have mixed the else statements. Right now your else if (userchoice == 2) matches statement if (choice == "="). Try:
if (userchoice == 1)
{
Console.WriteLine("Enter a number");
int firstinput = int.Parse(Console.ReadLine());
Console.WriteLine("Enter another number");
int secondinput = int.Parse(Console.ReadLine());
Console.WriteLine("Click =");
string choice = Console.ReadLine();
if (choice == "=")
{
Console.WriteLine(firstinput + secondinput);
}
}
else if (userchoice == 2)
{
Console.WriteLine("Enter a number");
int firstinput1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter another number");
int secondinput1 = int.Parse(Console.ReadLine());
Console.WriteLine("Click =");
string choice2 = Console.ReadLine();
if (choice2 == "=")
{
Console.WriteLine(firstinput1 - secondinput1);
}
}
else
{
Console.WriteLine("Wrong Selection, either select 1 or 2");
}
#FatmaHamdy , You missed the } before the else part
Console.WriteLine("If you want to add click 1 , subtract click 2");
int userchoice = int.Parse(Console.ReadLine());
if (userchoice == 1)
{
Console.WriteLine("Enter a number");
int firstinput = int.Parse(Console.ReadLine());
Console.WriteLine("Enter another number");
int secondinput = int.Parse(Console.ReadLine());
Console.WriteLine("Click =");
string choice = Console.ReadLine();
if (choice == "=")
{
Console.WriteLine(firstinput + secondinput);
}
}
else if (userchoice == 2)
{
Console.WriteLine("Enter a number");
int firstinput1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter another number");
int secondinput1 = int.Parse(Console.ReadLine());
Console.WriteLine("Click =");
string choice2 = Console.ReadLine();
if (choice2 == "=")
{
Console.WriteLine(firstinput1 - secondinput1);
}
}
Related
I have a coffee ordering program. The issue is that the total bill gets reset after this loop.
Note: I didn't add the entire code so it doesn't get too much, but if
you want to get the reference then please tell me.
invalidChoice = true;
while (invalidChoice)
{
Console.WriteLine(" ");
Console.WriteLine(" Can i bring you the bill: y or n (yes or no)");
Console.Write(" ");
string Anything = Console.ReadLine();
string UpperCaseAnything = Anything.ToUpper();
if (UpperCaseAnything == "Y" || UpperCaseAnything == "YES")
{
invalidChoice = false;
}
else if (UpperCaseAnything == "N" || UpperCaseAnything == "NO")
{
return;
}
else
{
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
}
}
What's happening is that if I enter "no," it will bring the menu up again, but I don't want to reset the added total value. But in this, the total amount that was getting added up until here gets reset if I enter "no." I don't know what's wrong—why the amount is getting reset here.
Here's the amount method.
private int askForCoffee()
{
int totalCoffeeCost = 0;
bool invalidChoice = true;
while (invalidChoice)
{
Console.WriteLine("");
Console.WriteLine(" Please enter your coffee size : 1 - small, 2 - medium, 3 - large");
Console.Write(" ");
int CoffeeChoice = int.Parse(Console.ReadLine());
switch (CoffeeChoice)
{
case 1:
totalCoffeeCost += 2;
invalidChoice = false;
break;
case 2:
totalCoffeeCost += 5;
invalidChoice = false;
break;
case 3:
totalCoffeeCost += 7;
invalidChoice = false;
break;
default:
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
break;
}
}
return totalCoffeeCost;
}
Update
Here is the whole code
using System;
/* A simple coffee ordering program
* used switch,if else and lable*/
class Program
{
public static void Main()
{
var exiting = false;
while (!exiting)
{
Program p = new Program();
p.programLoop();
}
}
private int askForCoffee()
{
int totalCoffeeCost = 0;
bool invalidChoice = true;
while (invalidChoice)
{
Console.WriteLine("");
Console.WriteLine(" Please enter your coffee size : 1 - small, 2 - medium, 3 - large");
Console.Write(" ");
int CoffeeChoice = int.Parse(Console.ReadLine());
switch (CoffeeChoice)
{
case 1:
totalCoffeeCost += 2;
invalidChoice = false;
break;
case 2:
totalCoffeeCost += 5;
invalidChoice = false;
break;
case 3:
totalCoffeeCost += 7;
invalidChoice = false;
break;
default:
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
break;
}
}
return totalCoffeeCost;
}
private void programLoop()
{
int TotalCoffeeCost = 0;
TotalCoffeeCost += askForCoffee();
var invalidChoice = true;
while (invalidChoice)
{
Console.WriteLine("");
Console.WriteLine(" Do you want have another coffee : Y or N (Yes or No) ?");
Console.Write(" ");
string UserChoice = Console.ReadLine();
string upperCaseChoice = UserChoice.ToUpper();
if (upperCaseChoice == "Y" || upperCaseChoice == "YES")
{
TotalCoffeeCost += askForCoffee(); //Note here that we did not set invalidChoice to false, meaning it will loop again
}
else if (upperCaseChoice == "N" || upperCaseChoice == "NO")
{
invalidChoice = false;
}
else
{
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
}
}
invalidChoice = true;
while (invalidChoice)
{
Console.WriteLine(" ");
Console.WriteLine(" Can i bring you the bill: y or n (yes or no)");
Console.Write(" ");
string Anything = Console.ReadLine();
string UpperCaseAnything = Anything.ToUpper();
if (UpperCaseAnything == "Y" || UpperCaseAnything == "YES")
{
invalidChoice = false;
}
else if (UpperCaseAnything == "N" || UpperCaseAnything == "NO")
{
return;
}
else
{
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
}
}
invalidChoice = true;
while (invalidChoice)
{
Console.WriteLine("");
Console.WriteLine(" Your total bill amount is = {0}$", TotalCoffeeCost);
Console.WriteLine("");
Console.WriteLine(" Please pay the amount by entering the amount bellow");
Console.Write(" ");
int EnterdAmt = int.Parse(Console.ReadLine());
if (EnterdAmt != TotalCoffeeCost)
{
Console.WriteLine("");
Console.WriteLine(" Please pay the correct amount");
}
else
{
invalidChoice = false;
}
}
Console.WriteLine("");
Console.WriteLine(" Thank for buying coffee, Hope you got a amazing experience");
}
}
Update askForCoffee() to pass through the existing total:
private int askForCoffee(int totalCoffeeCost)
{
bool invalidChoice = true;
while (invalidChoice)
//...
}
then call it like this:
TotalCoffeeCost = askForCoffee(TotalCoffeeCost);
You also need to do a similar thing with programLoop:
private int programLoop(int TotalCoffeeCost)
{
TotalCoffeeCost = askForCoffee(TotalCoffeeCost);
//...
}
Update - adding the whole code to help the OP:
using System;
/* A simple coffee ordering program
* used switch,if else and lable*/
class Program
{
public static void Main()
{
int totalCoffeeCost = 0;
var exiting = false;
while (!exiting)
{
Program p = new Program();
totalCoffeeCost = p.programLoop(totalCoffeeCost);
}
}
private int askForCoffee(int totalCoffeeCost)
{
bool invalidChoice = true;
while (invalidChoice)
{
Console.WriteLine("");
Console.WriteLine(" Please enter your coffee size : 1 - small, 2 - medium, 3 - large");
Console.Write(" ");
int CoffeeChoice = int.Parse(Console.ReadLine());
switch (CoffeeChoice)
{
case 1:
totalCoffeeCost += 2;
invalidChoice = false;
break;
case 2:
totalCoffeeCost += 5;
invalidChoice = false;
break;
case 3:
totalCoffeeCost += 7;
invalidChoice = false;
break;
default:
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
break;
}
}
return totalCoffeeCost;
}
private int programLoop(int TotalCoffeeCost)
{
TotalCoffeeCost = askForCoffee(TotalCoffeeCost);
var invalidChoice = true;
while (invalidChoice)
{
Console.WriteLine("");
Console.WriteLine(" Do you want have another coffee : Y or N (Yes or No) ?");
Console.Write(" ");
string UserChoice = Console.ReadLine();
string upperCaseChoice = UserChoice.ToUpper();
if (upperCaseChoice == "Y" || upperCaseChoice == "YES")
{
TotalCoffeeCost = askForCoffee(TotalCoffeeCost); //Note here that we did not set invalidChoice to false, meaning it will loop again
}
else if (upperCaseChoice == "N" || upperCaseChoice == "NO")
{
invalidChoice = false;
}
else
{
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
}
}
invalidChoice = true;
while (invalidChoice)
{
Console.WriteLine(" ");
Console.WriteLine(" Can i bring you the bill: y or n (yes or no)");
Console.Write(" ");
string Anything = Console.ReadLine();
string UpperCaseAnything = Anything.ToUpper();
if (UpperCaseAnything == "Y" || UpperCaseAnything == "YES")
{
invalidChoice = false;
}
else if (UpperCaseAnything == "N" || UpperCaseAnything == "NO")
{
return TotalCoffeeCost;
}
else
{
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
}
}
invalidChoice = true;
while (invalidChoice)
{
Console.WriteLine("");
Console.WriteLine(" Your total bill amount is = {0}$", TotalCoffeeCost);
Console.WriteLine("");
Console.WriteLine(" Please pay the amount by entering the amount bellow");
Console.Write(" ");
int EnterdAmt = int.Parse(Console.ReadLine());
if (EnterdAmt != TotalCoffeeCost)
{
Console.WriteLine("");
Console.WriteLine(" Please pay the correct amount");
}
else
{
invalidChoice = false;
}
}
Console.WriteLine("");
Console.WriteLine(" Thank for buying coffee, Hope you got a amazing experience");
return TotalCoffeeCost;
}
}
It's because you set the totalCoffeCost to 0 when entering another programLoop. What you can do, is making the totalCoffeCost to a class variable, so you don't need to set the totalCoffeCost inside the Loop methode.
This should work fine:
using System;
/* A simple coffee ordering program
* used switch,if else and lable*/
class Program
{
private int totalCoffeCost = 0;
public static void Main()
{
var exiting = false;
while (!exiting)
{
Program p = new Program();
p.programLoop();
}
}
private int askForCoffee()
{
int CoffeeCost = 0;
bool invalidChoice = true;
while (invalidChoice)
{
Console.WriteLine("");
Console.WriteLine(" Please enter your coffee size : 1 - small, 2 - medium, 3 - large");
Console.Write(" ");
int CoffeeChoice = int.Parse(Console.ReadLine());
switch (CoffeeChoice)
{
case 1:
CoffeeCost += 2;
invalidChoice = false;
break;
case 2:
CoffeeCost += 5;
invalidChoice = false;
break;
case 3:
CoffeeCost += 7;
invalidChoice = false;
break;
default:
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
break;
}
}
return CoffeeCost;
}
private void programLoop()
{
totalCoffeeCost += askForCoffee();
var invalidChoice = true;
while (invalidChoice)
{
Console.WriteLine("");
Console.WriteLine(" Do you want have another coffee : Y or N (Yes or No) ?");
Console.Write(" ");
string UserChoice = Console.ReadLine();
string upperCaseChoice = UserChoice.ToUpper();
if (upperCaseChoice == "Y" || upperCaseChoice == "YES")
{
totalCoffeeCost += askForCoffee(); //Note here that we did not set invalidChoice to false, meaning it will loop again
}
else if (upperCaseChoice == "N" || upperCaseChoice == "NO")
{
invalidChoice = false;
}
else
{
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
}
}
invalidChoice = true;
while (invalidChoice)
{
Console.WriteLine(" ");
Console.WriteLine(" Can i bring you the bill: y or n (yes or no)");
Console.Write(" ");
string Anything = Console.ReadLine();
string UpperCaseAnything = Anything.ToUpper();
if (UpperCaseAnything == "Y" || UpperCaseAnything == "YES")
{
invalidChoice = false;
}
else if (UpperCaseAnything == "N" || UpperCaseAnything == "NO")
{
return;
}
else
{
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
}
}
invalidChoice = true;
while (invalidChoice)
{
Console.WriteLine("");
Console.WriteLine(" Your total bill amount is = {0}$", totalCoffeeCost);
Console.WriteLine("");
Console.WriteLine(" Please pay the amount by entering the amount bellow");
Console.Write(" ");
int EnterdAmt = int.Parse(Console.ReadLine());
if (EnterdAmt != totalCoffeeCost)
{
Console.WriteLine("");
Console.WriteLine(" Please pay the correct amount");
}
else
{
invalidChoice = false;
}
}
Console.WriteLine("");
Console.WriteLine(" Thank for buying coffee, Hope you got a amazing experience");
totalCoffeCost = 0;
}
}
Well, every method should do its work (say, provide an interface for a cup of coffee) and
doesn't interfere in other activity (computing total etc. ``)
First of all, let's extract model (don not hardcode it within UI routines):
static readonly IReadOnlyDictionary<int, (string name, int size)> s_Options =
new Dictionary<int, (string name, int price)>() {
{1, ("small", 2)},
{2, ("medium", 5)},
{3, ("large", 7)},
};
Then asking for a single cup can be written like this:
private static int askForCoffee() {
// Keep asking until correct value is provided
while (true) {
var optionsToSell = s_Options
.OrderBy(pair => pair.Key)
.Select(pair => $"{pair.Key} - {pair.Value.name}");
Console.WriteLine();
Console.WriteLine("Please enter your coffee size : {string.Join(" - ", optionsToSell)}}");
Console.Write();
// If entered value is a valid integer and we have such an option in s_Options
// just return it
if (int.TryParse(Console.ReadLine(), out int choice) &&
s_Options.TryGetValue(choice, out var option))
return option.price;
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
}
}
To ask for several cups, we can use a simple loop:
private static int askForManyCoffee() {
int total = askForCoffee();
while (true) {
Console.WriteLine("");
Console.WriteLine(" Do you want have another coffee : Y or N (Yes or No) ?");
Console.Write(" ");
var input = Console.ReadLine().Trim().ToUpper();
if (input == "Y" || input == "YES")
totsl += askForCoffee();
else if (input == "N" || input == "NO")
return total;
else {
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
}
}
}
Then in program loop you can operate with TotalCoffeeCost:
private void programLoop() {
int TotalCoffeeCost = askForManyCoffee();
...
}
please, note, that being static askForCoffee() and askForManyCoffee() can't ruin anything like TotalCoffeeCost
try this, move the variable to a property and rename the TotalCoffeeCost instances to totalCoffeeCost. That should get you the desired result.
using System;
class Program
{
private int totalCoffeeCost = 0;
public static void Main()
{
var exiting = false;
while (!exiting)
{
Program p = new Program();
p.programLoop();
}
}
private int askForCoffee()
{
bool invalidChoice = true;
while (invalidChoice)
{
Console.WriteLine("");
Console.WriteLine(" Please enter your coffee size : 1 - small, 2 - medium, 3 - large");
Console.Write(" ");
int CoffeeChoice = int.Parse(Console.ReadLine());
switch (CoffeeChoice)
{
case 1:
totalCoffeeCost += 2;
invalidChoice = false;
break;
case 2:
totalCoffeeCost += 5;
invalidChoice = false;
break;
case 3:
totalCoffeeCost += 7;
invalidChoice = false;
break;
default:
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
break;
}
}
return totalCoffeeCost;
}
private void programLoop()
{
totalCoffeeCost += askForCoffee();
var invalidChoice = true;
while (invalidChoice)
{
Console.WriteLine("");
Console.WriteLine(" Do you want have another coffee : Y or N (Yes or No) ?");
Console.Write(" ");
string UserChoice = Console.ReadLine();
string upperCaseChoice = UserChoice.ToUpper();
if (upperCaseChoice == "Y" || upperCaseChoice == "YES")
{
totalCoffeeCost += askForCoffee(); //Note here that we did not set invalidChoice to false, meaning it will loop again
}
else if (upperCaseChoice == "N" || upperCaseChoice == "NO")
{
invalidChoice = false;
}
else
{
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
}
}
invalidChoice = true;
while (invalidChoice)
{
Console.WriteLine(" ");
Console.WriteLine(" Can i bring you the bill: y or n (yes or no)");
Console.Write(" ");
string Anything = Console.ReadLine();
string UpperCaseAnything = Anything.ToUpper();
if (UpperCaseAnything == "Y" || UpperCaseAnything == "YES")
{
invalidChoice = false;
}
else if (UpperCaseAnything == "N" || UpperCaseAnything == "NO")
{
return;
}
else
{
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
}
}
invalidChoice = true;
while (invalidChoice)
{
Console.WriteLine("");
Console.WriteLine(" Your total bill amount is = {0}$", totalCoffeeCost);
Console.WriteLine("");
Console.WriteLine(" Please pay the amount by entering the amount bellow");
Console.Write(" ");
int EnterdAmt = int.Parse(Console.ReadLine());
if (EnterdAmt != totalCoffeeCost)
{
Console.WriteLine("");
Console.WriteLine(" Please pay the correct amount");
}
else
{
invalidChoice = false;
}
}
Console.WriteLine("");
Console.WriteLine(" Thank for buying coffee, Hope you got a amazing experience");
}
}
i wanna make it so if the user didn't input a number an error message would pop up and restart a program, how can I do this?
{
static void Main(string[] args)
{
int num1;
int num2 = 0;
bool is_var_int = false;
Console.Write("enter a number:");
num1 = int.Parse(Console.ReadLine());
Console.Write("enter a number:");
num2 = int.Parse(Console.ReadLine());
Console.ReadKey();
}
static int Count(int num_1, int num_2)
{
int output = num_1 + num_2;
return output;
}
}
}
Use int.TryParse. No need to restart the app if input errors are properly handled ;-)
void Main()
{
int num1 = 0;
int num2 = 0;
Console.WriteLine("Enter a number:");
while (!int.TryParse(Console.ReadLine(), out num1))
{
Console.WriteLine("Error");
}
Console.WriteLine(num1);
Console.WriteLine("Enter a number:");
while (!int.TryParse(Console.ReadLine(), out num2))
{
Console.WriteLine("Error");
}
Console.WriteLine(num1);
Console.WriteLine("Result = {0}", Count(num1, num2));
}
I have a method here that allows the user to enter any number except for 999. But I don't know how to create a validation when user enters a negative number or letter.
static int EnterScores(double[] scores, int maxScores)
{
double userInput;
int count = 0;
while (count < maxScores)
{
Console.Write("Enter a score(or 999 to quit): ");
userInput = double.Parse(Console.ReadLine());
if (userInput == 999 || userInput < 0)
break;
scores[count++] = userInput;
}
return count;
}
You can use double.TryParse. If it can convert string to float - it will be true
...
var str = Console.ReadLine();
if (double.TryParse(str, out userInput)){
if (userInput == 999 || userInput < 0)
break;
scores[count++] = userInput;
}
...
If user inputs a letter or something that can't be converted to double your this piece of code double.TryParse will throw an exception and program will fail.
So you should use try-catch block here:
try
{
userInput = double.Parse(Console.ReadLine());
if (userInput == 999)
{
break;
}
else if (userInput < 0)
{
Console.WriteLine("Invalid input");
}
else
{
scores[count++] = userInput;
}
}
catch(Exception e) // will take care of letters
{
Console.WriteLine("You enter an Invalid input !"):
}
Separate the tests for userInput == 999 and userInput < 0, like this:
...
if (userInput == 999)
{
break;
}
else if (userInput < 0)
{
Console.WriteLine("Invalid input");
}
else
{
scores[count++] = userInput;
}
...
This is a code which multiplies numbers, user enters.
string x;
double t, s = 1;
Console.WriteLine("Enter some numbers: ");
Console.WriteLine("To finish, press Enter");
while ((x = Console.ReadLine()) != "")
{
t = Convert.ToDouble(x);
s *= t;
}
Console.WriteLine("The result is: {0}", s);
Console.ReadLine();
It shows the result at the end, but how to make it show a total count of numbers entered?
For example: I enter 1, 2 and 3. So total count is 3.
string x;
double t, s = 1;
int count = 0;
Console.WriteLine("Enter some numbers: ");
Console.WriteLine("To finish, press Enter");
while ((x = Console.ReadLine()) != "")
{
t = Convert.ToDouble(x);
s *= t;
count++;
}
Console.WriteLine("The result is: {0}", s);
Console.WriteLine("The count is: {0}", count);
Console.ReadLine();
It is good to use TryParse to avoid FormatException
Char.IsDigit returns true is current char is digit (obviously)
Example:
string x;
double t, s = 1;
int digitCount = 0;
Console.WriteLine("Enter some numbers: ");
Console.WriteLine("To finish, press Enter");
while ((x = Console.ReadLine()) != "")
{
if (!Double.TryParse(x, out t))
continue;
foreach (var c in x)
if (Char.IsDigit(c))
digitCount++;
s *= t;
}
Console.WriteLine("The result is: {0}", s);
Console.WriteLine("The count of digits is: {0}", digitCount);
Console.ReadLine();
Why not a variable for counter?
Console.WriteLine("Enter some numbers: ");
Console.WriteLine("To finish, press Enter");
int i=0;
while ((x = Console.ReadLine()) != "")
{
i++;
t = Convert.ToDouble(x);
s *= t;
}
Console.WriteLine("The result is: {0}", s);
Console.ReadLine();
You have to count the loop iteration for that use a counter variable:
int loopCounter=0;
while ((x = Console.ReadLine()) != "")
{
t = Convert.ToDouble(x);
s *= t;
loopCounter++;
}
Console.WriteLine("The count is: {0}", loopCounter);
You could try counting the number of characters in the string input (x).
int temp=x.Length();
i need help getting this code to display the arrays but the code structure has to stay the same. i have created two arrays and i need to display both from individual methods, and i also need to search and display the marks of the students entered
this is my first code that i have done and really need help, thanks
class Program
{
static void Main()
{
collectStudentDetails();
promptForStudentQuery();
printStudentsmarks();
Console.ReadLine();
}
public static void collectStudentDetails()
{
Console.WriteLine("Please Specifiy How Many Student Details You Wish To Enter");
Console.WriteLine("");
int n = SafeReadInteger(0);
int[] StudentMarks = new int[n];
string[] StudentNames = new string[n];
for (int i = 0; i < StudentNames.Length; i++)
{
Console.WriteLine("Enter Name for student {0}", i + 1);
StudentNames[i] = SafeReadString(null);
Console.WriteLine("Enter Mark for Student {0}: ", i + 1);
StudentMarks[i] = SafeReadInteger(0);
}
}
static void findStudentmark()
{
bool foundStudent = false;
Console.WriteLine("Please Enter The Students Name To Find Their Marks");
Console.WriteLine("Please Press Enter To Continue");
Console.ReadLine();
}
static void printStudentsmarks()
{
Console.WriteLine("\nStudent Mark List");
Console.WriteLine("Please Press Enter To Continue");
Console.ReadLine();
promptForStudentQuery();
}
static bool promptForStudentQuery()
{
bool promptAgain = true;
Console.WriteLine();
Console.WriteLine(" 1. find a student's mark ");
Console.WriteLine(" 2. print all student marks");
Console.WriteLine(" 3. exit ");
Console.WriteLine();
int choice = SafeReadInteger(0);
if (choice == 1)
{
findStudentmark();
}
else if (choice == 2)
{
printStudentsmarks();
}
else if (choice == 3)
{
Environment.Exit(0);
}
else if (choice == 0)
{
Console.WriteLine("you entered an invalid option try again");
}
return promptAgain;
}
public static int SafeReadInteger(int defaultVal)
{
try
{
return int.Parse(System.Console.ReadLine());
}
catch
{
return defaultVal;
}
}
public static string SafeReadString(string defaultVal)
{
string temp = "";
temp = Console.ReadLine();
while (temp == "")
{
Console.WriteLine("You have entered nothing. Please enter a correct value.");
temp = Console.ReadLine();
}
return temp;
}
static void DisplayArray(int[] inputarray)
{
foreach (int x in inputarray)
{
Console.Write(" {0} ", x);
}
Console.WriteLine("");
}
static void DisplayArray2(string[] inputarray)
{
foreach (string x in inputarray)
{
Console.Write(" {0} ", x);
}
Console.WriteLine("");
}
}
Below is probably a rough way of doing it. First define the StudentName and StudentMarks at class level.
class Program
{
static int[] StudentMarks;
static string[] StudentNames;
static void Main()
{
collectStudentDetails();
promptForStudentQuery();
printStudentsmarks();
Console.ReadLine();
}
You need to update method printStudentsmarks() and findStudentmark() as:
static void findStudentmark()
{
Console.WriteLine("Please Enter The Students Name To Find Their Marks");
Console.WriteLine("Please Press Enter To Continue");
string stdName = Console.ReadLine();
int i = 0;
for (i = 0; i < StudentNames.Length; i++)
{
if (string.Compare(stdName, StudentNames[i], true)==0)
{
break;
}
}
Console.WriteLine(StudentNames[i]);
Console.WriteLine(StudentMarks[i]);
}
static void printStudentsmarks()
{
Console.WriteLine("\nStudent Mark List");
for (int i = 0; i < StudentNames.Length; i++)
{
Console.Write("Name: ");
Console.WriteLine(StudentNames[i]);
Console.WriteLine("Marks: ");
Console.WriteLine(StudentMarks[i]);
Console.WriteLine("______________________________");
}
Console.WriteLine("Please Press Enter To Continue");
Console.ReadLine();
promptForStudentQuery();
}