Greetings everyone I am having a little trouble with this one. I bolded my switch case which should work but its currently not. If a user selected course option 1 twice it just says you enrolled in course 1 and course 1. It should stop it by executing case -2. Need a new set of eyes. - Thanks
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleRegisterStudent
{
class Program
{
static void Main(string[] args)
{
(new Program()).run();
}
void run()
{
int choice;
int firstChoice = 0, secondChoice = 0, thirdChoice = 0;
int totalCredit = 0;
string yesOrNo = "";
System.Console.WriteLine("Ayala's Copy");
do
{
WritePrompt();
choice = Convert.ToInt32(Console.ReadLine());
switch (ValidateChoice(choice, firstChoice, secondChoice,
thirdChoice, totalCredit))
{
case -1:
Console.WriteLine("Your entered selection {0} is not a recognized
course.", choice);
break;
case -2:
Console.WriteLine("You have already registerd for this {0}
course.", ChoiceToCourse(choice));
break;
case -3:
Console.WriteLine("You can not register for more than 9 credit
hours.");
break;
case 0:
Console.WriteLine("Registration Confirmed for course {0}.",
ChoiceToCourse(choice));
totalCredit += 3;
if (firstChoice == 0)
firstChoice = choice;
else if (secondChoice == 0)
secondChoice = choice;
else if (thirdChoice == 0)
thirdChoice = choice;
break;
}
WriteCurrentRegistration(firstChoice, secondChoice,
thirdChoice);
Console.Write("\nDo you want to try again? (Y|N)? : ");
yesOrNo = (Console.ReadLine()).ToUpper();
} while (yesOrNo == "Y");
Console.WriteLine("Thank you for registering with us");
}
void WritePrompt()
{
Console.WriteLine("Please select a course for which you want to
register by typing the number inside []");
Console.WriteLine("[1]IT 145\n[2]IT 200\n[3]IT 201\n[4]IT
270\n[5]IT 315\n[6]IT 328\n[7]IT 330");
Console.Write("Enter your choice : ");
}
int ValidateChoice(int choice, int firstChoice, int
secondChoice, int thirdChoice, int totalCredit)
{
//changed from 70 to 7 and return -4 to 0
if (choice < 1 || choice > 7)
return -1;
else if (choice == firstChoice && choice == secondChoice
&& choice == thirdChoice)
return -2;
else if (totalCredit > 9)
return -3;
return 0;
}
void WriteCurrentRegistration(int firstChoice, int secondChoice,
int thirdChoice)
{
if (secondChoice == 0)
Console.WriteLine("You are currently registered for {0}",
ChoiceToCourse(firstChoice));
else if (thirdChoice == 0)
Console.WriteLine("You are currently registered for {0}, {1}",
ChoiceToCourse(firstChoice), ChoiceToCourse(secondChoice));
else
Console.WriteLine("You are currently registered for {0}, {1}, {2}",
ChoiceToCourse(firstChoice), ChoiceToCourse(secondChoice),
ChoiceToCourse(thirdChoice));
}
string ChoiceToCourse(int choice)
{
string course = "";
switch (choice)
{
case 1:
course = "IT 145";
break;
case 2:
course = "IT 200";
break;
case 3:
course = "IT 201";
break;
case 4:
course = "IT 270";
break;
case 5:
course = "IT 315";
break;
case 6:
course = "IT 328";
break;
case 7:
course = "IT 330";
break;
default:
break;
}
return course;
}
}
}
Here there is only a simple mistake , && operation is used in the place where || operation is needed.
Corrected code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleRegisterStudent
{
class Program
{
static void Main(string[] args)
{
(new Program()).run();
}
void run()
{
int choice;
int firstChoice = 0, secondChoice = 0, thirdChoice = 0;
int totalCredit = 0;
string yesOrNo = "";
System.Console.WriteLine("Ayala's Copy");
do
{
WritePrompt();
choice = Convert.ToInt32(Console.ReadLine());
switch (ValidateChoice(choice, firstChoice, secondChoice,
thirdChoice, totalCredit))
{
case -1:
Console.WriteLine("Your entered selection {0} is not a recognized
course.", choice);
break;
case -2:
Console.WriteLine("You have already registerd for this {0}
course.", ChoiceToCourse(choice));
break;
case -3:
Console.WriteLine("You can not register for more than 9 credit
hours.");
break;
case 0:
Console.WriteLine("Registration Confirmed for course {0}.",
ChoiceToCourse(choice));
totalCredit += 3;
if (firstChoice == 0)
firstChoice = choice;
else if (secondChoice == 0)
secondChoice = choice;
else if (thirdChoice == 0)
thirdChoice = choice;
break;
}
WriteCurrentRegistration(firstChoice, secondChoice,
thirdChoice);
Console.Write("\nDo you want to try again? (Y|N)? : ");
yesOrNo = Console.ReadLine();
yesOrNo=yesOrNo.ToUpper();
} while (yesOrNo == "Y");
Console.WriteLine("Thank you for registering with us");
}
void WritePrompt()
{
Console.WriteLine("Please select a course for which you want to
register by typing the number inside []");
Console.WriteLine("[1]IT 145\n[2]IT 200\n[3]IT 201\n[4]IT
270\n[5]IT 315\n[6]IT 328\n[7]IT 330");
Console.Write("Enter your choice : ");
}
int ValidateChoice(int choice, int firstChoice, int
secondChoice, int thirdChoice, int totalCredit)
{
//changed from 70 to 7 and return -4 to 0
if (choice < 1 || choice > 7)
return -1;
else if (choice == firstChoice || choice == secondChoice || choice
== thirdChoice) //here && is replaced with ||
return -2;
else if (totalCredit > 9)
return -3;
return 0;
}
void WriteCurrentRegistration(int firstChoice, int secondChoice,
int thirdChoice)
{
if (secondChoice == 0)
Console.WriteLine("You are currently registered for {0}",
ChoiceToCourse(firstChoice));
else if (thirdChoice == 0)
Console.WriteLine("You are currently registered for {0}, {1}",
ChoiceToCourse(firstChoice), ChoiceToCourse(secondChoice));
else
Console.WriteLine("You are currently registered for {0}, {1}, {2}",
ChoiceToCourse(firstChoice), ChoiceToCourse(secondChoice),
ChoiceToCourse(thirdChoice));
}
string ChoiceToCourse(int choice)
{
string course = "";
switch (choice)
{
case 1:
course = "IT 145";
break;
case 2:
course = "IT 200";
break;
case 3:
course = "IT 201";
break;
case 4:
course = "IT 270";
break;
case 5:
course = "IT 315";
break;
case 6:
course = "IT 328";
break;
case 7:
course = "IT 330";
break;
default:
break;
}
return course;
}
}
}
Screenshot of the code
Output
If you find this answer useful , please rate positive,thankyou
Greetings everyone I am having a little trouble with this one. I bolded my switch case...
[C#] I need to convert this if/else statements into Switch I have the code for the if/else but now need to turn it into a switch. The Console.WriteLine and all that is fine. I just need to convert the if/else. int x1, x2, y1, y2; Console.WriteLine("What is smallest value of x:"); x1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("What is largest value of x:"); x2 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("What is smallest value of y:"); y1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("What is largest value of y:"); y2 =...
Hello in C#. I need help understanding and knwoing what i missed in my program. The issue is, the program is supposed to have user input for 12 family members and at the end of the 12 it asks for user to input a name to search for. When i put a correct name, it always gives me a relative not found message. WHat did i miss. And can you adjust the new code so im able to see where...
c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...
C#, I am getting error placing them all in one file pls set them in the order that all 3 work in one file. Thanks //Program 1 using System; class MatrixLibrary { public static int[,] Matrix_Multi(int[,] a, int [,]b){ int r1= a.GetLength(0); int c1 = a.GetLength(1); int c2 = b.GetLength(1); int r2 = b.GetLength(0); if(r2 != c2){ Console.WriteLine("Matrices cannot be multiplied together.\n"); return null; }else { int[,] c = new int[r1, c2]; for (int i = 0; i <...
i need help fixing my code. here is the assignment: Make a class that contains the following functions: isLong, isDouble, stringToLong, and stringToDouble. Each receives a string and returns the appropriate type (bool, bool, long, and double).During the rest of the semester you will paste this class into your programs and will no longer use any of the "standard" c# functions to convert strings to numbers. here is my code. it works for the long method but not the double:...
in a c# program I am trying to allow the area to be divided by a set choice. the area is set as an int in another method and I am unable to call it into the method I need it in. Is there a way to call Int? I also need to set each case to have an integer value. For example if they select 1 it is set at a value of 5. This is what I have...
C# Hey I am having trouble implementing additonal function to this assignment: Problem: Implement three IComparer classes on Employee - NameComparer, AgeComparer, and PayComparer - that allow Employees to be compared by the Name, Age, and Pay, respectively. Code: Employee.Core.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Employees { partial class Employee { // Field data. private string empName; private int empID; private float currPay; private int empAge; private string empSSN = ""; private string empBene...
==Modify the M8B by using C#== using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace M8B { class Magic8Ball { static void Main(string[] args) { string question; Console.WriteLine("I am the Magic 8-ball! Ask me a question and I will give you an answer."); Console.Write("Your question: "); question = Console.ReadLine(); Console.WriteLine("\nLet me part the mists of the time for you."); Random rnd = new Random(); int roll =...
I'm attempting to convert the Board and CheWin sections into classes. Is there a way to do just that? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace TicTacToe { class TicTacToe { static char[] arr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; static int player = 1; static int choice; static int flag = 0; static void Main(string[] args) { do { Console.Clear(); Console.WriteLine("Player 1 : 'X' \nPlayer 2...
Hey guys I am having trouble getting my table to work with a java GUI if anything can take a look at my code and see where I am going wrong with lines 38-49 it would be greatly appreciated! Under the JTableSortingDemo class is where I am having erorrs in my code! Thanks! :) import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.List; import javax.swing.*; public class JTableSortingDemo extends JFrame{ private JTable table; public JTableSortingDemo(){ super("This is basic sample for your...