I'm a beginner in C# and I'd like to know if there a better way to write this 3+3 math problem by using loops, like maybe a do while loop ?
My code is the following :
static void Main (string[] args) {
Console.WriteLine ("What is 3+3?");
int answer = int.Parse (Console.ReadLine ());
int counter = 1;
if (answer == 6) {
Console.WriteLine ("Great! thats Correct! you've got it in the first try");
} else {
while (answer != 6) {
Console.WriteLine (" Wrong, please try again");
answer = int.Parse (Console.ReadLine ());
counter++;
if (answer == 6) {
Console.WriteLine ("Correct! Well done! you got it in {0} tries", counter);
}
}
}
Console.ReadLine ();
}
The aim of this program is to ask the user a question, check the answer and output a statement that says how many tries the user took to get the answer right.
If you could provide me suggestions.
If you're just wanting less code / a more concise option you could go for either of the following.
Note I've ignored the use of a slightly different error message for the first case and other cases. If this is important then you can of course have an if (counter == 1) statement.
This first example uses Do/While which will always execute the loop at least once and then check the exit condition (answer == 6) at the end of every loop.
static void Main(string[] args)
{
Console.WriteLine("What is 3+3?");
int answer;
int counter = 0;
do
{
answer = int.Parse(Console.ReadLine());
counter++;
if (answer == 6)
{
Console.WriteLine("Correct! Well done! you got it in {0} tries", counter);
}
else
{
Console.WriteLine(" Wrong, please try again");
}
}
while (answer != 6);
Console.ReadLine();
}
This second example uses a while loop that loops forever and the break keyword which breaks out of a loop once a certain condition is met. This prevents the need for extra if statements to get rid of pesky extra messages etc.
static void Main(string[] args)
{
Console.WriteLine("What is 3+3?");
int answer;
int counter = 0;
while (true)
{
answer = int.Parse(Console.ReadLine());
counter++;
if (answer == 6)
{
Console.WriteLine("Correct! Well done! you got it in {0} tries", counter);
break;
}
Console.WriteLine(" Wrong, please try again");
}
Console.ReadLine();
}
I don't know very much about this, but at least, in C++, when you fail, I invoked again the main method with Main();, adding the error message.
Hope this solves your problem.
Using do/while will be significantly shorter:
static void Main(string[] args)
{
int counter = 1;
boolean hasAnswer = false;
do {
Console.WriteLine("What is 3+3?");
int answer = int.Parse(Console.ReadLine());
if (answer == 6)
{
Console.WriteLine("Great! thats Correct! You've got it in " + counter + " attempt(s)");
hasAnswer = true
}
else
{
Console.WriteLine(" Wrong, please try again");
counter++;
}
} while(!hasAnswer);
}
I haven't rewritten C# in a while so the syntax might be off, but that is the gist of how to do it.
Related
This question already has answers here:
check for valid number input - console application
(5 answers)
Allow To Only Input A Number - C#
(1 answer)
Closed 2 years ago.
I have just started a table programme. I am just a trainee learning C# from internet, so I am not so good in this.
I just wanted the programme to run according to the user. I want that if the user hits enter simply, the programme should not crash. That is I just wanted to know how to prevent null enter. This is the code is used:
The "______" which used if for writing a line
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace tables
{
class Program
{
static void Main(string[] args)
{
goto found;
found:
Console.WriteLine("");
string textToEnter = "MULTIPLATION TABLES";
Console.WriteLine(String.Format("{0," + ((Console.WindowWidth / 2) + (textToEnter.Length / 2)) + "}", textToEnter));
Console.WriteLine("");
Console.WriteLine("________________________________________________________________________________");
Console.WriteLine("");
int num, j, i;
Console.Write("enter the number of which table u need ? :- ");
num = Convert.ToInt32( Console.ReadLine());
while (num == 0)
{
Console.WriteLine("please enter a valid input");
Console.Write("enter the number of which table u need ? :- ");
num = Convert.ToInt32(Console.ReadLine());
}
Console.Write("enter the number till which the table need to be ? :- ");
j = Convert.ToInt32(Console.ReadLine());
while (j == 0)
{
Console.WriteLine("please enter a valid input");
Console.Write("enter the number till which the table need to be ? :- ");
j = Convert.ToInt32(Console.ReadLine());
}
i = Convert.ToInt32(j);
for (j=1; ; j++)
{
if (j > i)
{
break;
}
Console.WriteLine(num + " * " + j + " = " + num * j);
}
string str;
Console.Write("do you want to continue? (y/n) :- " );
str= Console.ReadLine();
foreach (char ch in str)
{
if (ch == 'y')
{
goto found;
}
else if (ch=='n' )
{
Console.WriteLine("");
Console.WriteLine("THANK YOU FOR USING MY PRODUCT");
}
else
{
Console.WriteLine("please enter a valid input");
}
}
Console.ReadKey();
}
}
}
As suggested in the comments, I'd use int.TryParse(), but inside a do...while() loop. Use a separate flag (boolean) to track whether the user should keep trying again:
bool invalid;
int num, j, i;
do
{
invalid = true;
Console.Write("enter the number of which table u need ? :- ");
String response = Console.ReadLine();
if (int.TryParse(response, out num))
{
invalid = false;
}
else
{
Console.WriteLine("Invalid input. Please try again.");
}
} while (invalid);
// ...repeat the above do...while() block for "j" and "i"...
When you're accepting user input, it's important to perform validation on it. You can't assume that the user will always enter correctly formatted data that your program will be able to work with. As you discovered, a user who hits enter will give you an empty string (""), which can't be parsed to anything.
C# has several ways of attempting parsing. The first, which you're using, is Convert.ToInt32(), which throws an exception if the input it receives is not, in fact, a number. You have to catch the exception with a try/catch block, like so:
try
{
num = Convert.ToInt32(Console.ReadLine());
}
catch(FormatException ex)
{
Console.WriteLine("You didn't enter a proper number!");
}
However, in general, exceptions should be, well, exceptional. They should only be relied upon when rare failures occur, because unwinding the call stack can be expensive.
I would argue that C# has a better method for you to use in this instance: Int32.TryParse()
You can see the documentation here.
TryParse takes two parameters, the thing you're trying to parse (convert), and then a number to store the value in. It returns true or false, indicating if it succeeded or failed in converting the number.
You might use it like this:
var success = Int32.TryParse(Console.ReadLine(), num);
if (success)
{
// do something with 'num' -- it has a valid value now.
}
else
{
// Warn the user, perhaps prompt them to try again
Console.WriteLine("That wasn't a valid number!");
}
You can use a methode to get a safe int value:
private static int ReadIntValue(string psMessage)
{
int lnInt;
string lsValue = string.Empty;
do
{
Console.Write(psMessage);
lsValue = Console.ReadLine();
} while (!int.TryParse(lsValue, out lnInt));
return lnInt;
}
And then use this:
num = ReadIntValue("enter the number of which table u need ? :- ");
So I recently started my first coding course for C#. I am currently trying to write a short program that prints out Yay! as many times as I indicate. Now to prevent any format exceptions, I've tried to add a try and catch to it. But, for some reason this isn't working and I can't seem to figure out why. The code is below:
Console.Write("Enter the number of times to print \"Yay!\": ");
string entry = Console.ReadLine();
var number = int.Parse (entry);
bool print = true;
while(print)
{
try
{
if(number <= 0)
{
print = false;
}
else
{
Console.WriteLine("Yay!");
number -= 1;
}
}
catch(FormatException)
{
Console.WriteLine("You must enter a whole number.");
}
}
Now to my knowledge, I have everything I need to make this work. Can anyone see where I went wrong with this?
Thanks a lot for reading!
It's
var number = int.Parse (entry);
that should throw the exception, and since it's beyond try {} scope, the
exception has not been caught. Move the fragment into the scope:
Console.Write("Enter the number of times to print \"Yay!\": ");
string entry = Console.ReadLine();
bool print = true;
try {
// int.Parse is within the try {} scope now
var number = int.Parse (entry);
while(print) {
...
}
}
catch(FormatException) {
Console.WriteLine("You must enter a whole number.");
}
Or convert int.Parse into int.TryParse and drop try {...} catch {...} at all (a better solution)
Console.Write("Enter the number of times to print \"Yay!\": ");
int number;
if (!int.TryParse(Console.ReadLine(), out number)) {
Console.WriteLine("You must enter a whole number.");
return;
}
bool print = true;
// There's no need in try {} catch {} now
while(print) {
...
}
You need to place your int.Parse inside your try-catch block.
Here's a revised version of your code.
static void Main(string[] args)
{
bool print = true;
while (print)
{
Console.Write("Enter the number of times to print \"Yay!\": ");
string entry = Console.ReadLine();
try
{
var number = int.Parse(entry);
for (int i = 0; i < number; i++)
{
Console.WriteLine("Yay!");
}
}
catch (FormatException)
{
Console.WriteLine("You must enter a whole number.");
}
}
}
I want to do this because I would like to block inputs that will crash my program. I tried doing it like this but I get the errors use of unassigned parameter total and the out parameter totalstring and total must be assigned before control leaves current method.
private static void Start(out String totalString, out int total)
{
Console.WriteLine("How Many ? (2-4)");
Console.WriteLine("");
try
{ totalString = Console.ReadLine();
total = int.Parse(totalString);
}
catch
{
Console.WriteLine("");
}
bool flag = false;
if ((total <= 1) || (total > 4)) //loop to reject invaid input
while (flag == false)
{
if ((total <= 1) || (total > 4))
{
Console.WriteLine("Invalid input. How many?");
totalString = Console.ReadLine();
total = int.Parse(totalString);
Console.Clear();
}
else if ((total >= 2) || (total <= 4))
{
flag = true;
}
}
Console.Clear();
Console.WriteLine("Player Numbers :" + total);
Console.WriteLine();
players = new Player[total];
}
}
}
Sorry about that :)
I'd rather use TryParse instead of Parse:
Terse:
int total;
do {
Console.WriteLine("How Many ? (2-4)");
}
while (!(int.TryParse(Console.ReadLine(), out total) && (total >= 2) && (total <= 4)))
Talkative:
int total;
Console.WriteLine("How Many ? (2-4)");
while (true) {
if (!int.TryParse(Console.ReadLine(), out total)) {
Console.WriteLine("Invalid input. How many?");
else if ((total < 2) || (total > 4)) {
Console.WriteLine("Invalid range. How many?");
else
break;
}
When you have a method that has out parameters, then you must always assign a value to them, whatever path your code takes. This is what that error message is trying to tell you.
When you have
try
{ totalString = Console.ReadLine();
total = int.Parse(totalString);
and "Console.ReadLine()" throws an error, then both totalString and total are not assigned. Similarly, when the ReadLine succeeds, but the int.Parse fails, then total is not assigned.
Simple solution: assign default values at the start of the method:
totalString = null;
total = 0;
These will be overwritten when everything works out.
There are two improvements you can make here:
1 Get rid of the out parameters and return the player array, rather than having a void method.
2 Use a do while loop instead of the current code:
private static IEnumerable<Player> Start()
{
do
{
Console.WriteLine("How Many ? (2-4)");
Console.WriteLine("");
try
{
var totalString = Console.ReadLine();
var total = int.Parse(totalString);
if (total >= 1 && total <= 4)
{
Console.WriteLine("Number of players :" + total);
return new Player[total];
}
}
catch
{
Console.WriteLine("Invalid input.");
}
} while (true)
}
You're getting this error because total is never assigned to if int.Parse() throws an exception but you are using it anyways after the catch block.
To avoid this, check total for null.
Dmitry Bychenko provided you with a pretty neat alternative so I'm just going to point out some small things in your code.
Console.ReadLine() already is a string, this would work:
total = int.Parse(Console.ReadLine);
You check total twice, once in the outer if block and again in the while loop. You don't have to do that.
Once you get to the second parse attempt within the loop and enter something invalid, an exception will be thrown but you don't handle it.
For your own sake, format your code.
Try to avoid unnecessary exceptions. If an exception is thrown, the application freezes for a few seconds which neither looks good nor is it fun to use. TryParse for example returns false if the parse attempt fails.
I have created a quiz program which goes through 5 questions. If you get a question wrong you are forced to start again from the beginning, I added this loop by
placing the code goto start; and placed start: where I wanted it to loop from.
My question is: I want to add a part at the end where it says how many times the program was looped so I can add a writeline saying "well done you took X times to complete the quiz"
here is the main part of the program:(without the namespaces and using statements)
/*----------------------------------------Declaration----------------------------------------- */
string q1, q2, q3, q4, q5;
/*----------------------------------------TITLE----------------------------------------- */
Console.WriteLine("Welcome to the Ultimate quiz!");
Console.WriteLine();
/*----------------------------------------QUESTION 1----------------------------------------- */
start:
Console.WriteLine("The JavaScript Language is not object oriented (True/False)");
Console.WriteLine();
q1 = Console.ReadLine();
q1 = q1.ToUpper();
if (q1 == "TRUE")
{
Console.WriteLine();
Console.WriteLine("Well Done, you may move on to the next question");
}
else
{
Console.WriteLine("Sorry you got the answer wrong, you have to start again");
goto start;
}
Console.WriteLine();
/*----------------------------------------QUESTION 2----------------------------------------- */
Console.WriteLine("What is the age range to qualify for an apprenticeship in the uk? Please type in the following format xx-yy");
Console.WriteLine();
q2 = Console.ReadLine();
if (q2 == "16-24")
{
Console.WriteLine();
Console.WriteLine("Well Done, you may move on to the next question");
}
else
{
Console.WriteLine("Sorry you got the answer wrong, you have to start again");
goto start;
}
Console.WriteLine();
/*----------------------------------------QUESTION 3----------------------------------------- */
Console.WriteLine("Is HTML a programming language (Yes or No)");
Console.WriteLine();
q3 = Console.ReadLine();
q3 = q3.ToUpper();
if (q3 == "NO")
{
Console.WriteLine();
Console.WriteLine("Well Done, you may move on to the next question");
}
else
{
Console.WriteLine("Sorry you got the answer wrong, you have to start again");
goto start;
}
Console.WriteLine();
/*----------------------------------------QUESTION 4----------------------------------------- */
Console.WriteLine("In JavaScript, What are the 2 charecters used to symbolise a single line comment?");
Console.WriteLine();
q4 = Console.ReadLine();
if (q4 == "//")
{
Console.WriteLine();
Console.WriteLine("Well Done, you may move on to the next question");
}
else
{
Console.WriteLine("Sorry you got the answer wrong, you have to start again");
goto start;
}
Console.WriteLine();
/*----------------------------------------QUESTION 5----------------------------------------- */
Console.WriteLine("500 < 600 && 700 < 600");
Console.WriteLine();
Console.WriteLine("Is the above statement true or false ?");
Console.WriteLine();
q5 = Console.ReadLine();
q5 = q5.ToUpper();
if (q5 == "FALSE")
{
Console.WriteLine();
Console.WriteLine("Well Done, you may move on to the next question");
Console.WriteLine();
Console.WriteLine("Congratulations You have passed the quiz!");
}
else
{
Console.WriteLine("Sorry you got the answer wrong, you have to start again");
goto start;
}
Console.WriteLine();
}
}
}
Thanks for all the help.
Simply add three line in your code -->
Before Start: --> int count = 0;
After Start: --> count++;
Last line of your code. -->
Console.WriteLine("well done you took " + count + " times to complete the quiz");
Ok, so you might already have your answer, and this is a solution too BUT (and i hope you don't take this as a negative thing) i remade the whole thing, to me at least it is simpler and less line consuming (at least i think so)
string q;
int retryCount = 1;
Console.WriteLine("Welcome to the Ultimate quiz!");
Console.WriteLine();
start:
const int numberOfQuestions = 5;
for (int i = 1; i <= numberOfQuestions; i++) {
if (i > 1) {Console.WriteLine();}
switch (i) {
case 1:
Console.WriteLine("The JavaScript Language is not object oriented (True/False)");
break;
case 2:
Console.WriteLine("What is the age range to qualify for an apprenticeship in the uk? Please type in the following format xx-yy");
break;
case 3:
Console.WriteLine("Is HTML a programming language (Yes or No)");
break;
case 4:
Console.WriteLine("In JavaScript, What are the 2 charecters used to symbolise a single line comment?");
break;
case 5:
Console.WriteLine("500 < 600 && 700 < 600");
Console.WriteLine();
Console.WriteLine("Is the above statement true or false ?");
break;
}
Console.WriteLine();
q = Console.ReadLine();
q = q.ToUpper();
switch (i) {
case 1:
if (q == "TRUE") {
Console.WriteLine();
Console.WriteLine("Well Done, you may move on to the next question");
} else { goto restart; }
break;
case 2:
if (q == "16-24") {
Console.WriteLine();
Console.WriteLine("Well Done, you may move on to the next question");
} else { goto restart; }
break;
case 3:
if (q == "NO") {
Console.WriteLine();
Console.WriteLine("Well Done, you may move on to the next question");
} else { goto restart; }
break;
case 4:
if (q == "//") {
Console.WriteLine();
Console.WriteLine("Well Done, you may move on to the next question");
} else { goto restart; }
break;
case 5:
if (q == "FALSE") {
Console.WriteLine();
goto end;
} else { goto restart; }
break;
}
}
restart:
Console.WriteLine("Sorry you got the answer wrong, you have to start again");
retryCount += 1;
goto start;
end:
Console.WriteLine("Congratulations You have passed the quiz!");
Console.WriteLine(String.Format("Well done!, you took {0} times to complete the quiz.", retryCount));
Console.ReadKey();
I hope this is what you were trying to acheive and you don't need to use all the code, was just showing you dont need to create q1,q2,q3 etc....
just add more cases at will.
put
count++
in the loop you want to know the number of loops it went, then for example if you want to check if the loop have gone 15 then
if(count = 15)
then add your code
declare
int count = 0;
at the begginging and everytime someone fails send them to repeat:
repeat:
count++;
goto start;
Like this:
void a() {
string q, q1;
int count = 0;
start:
int a = 2;
if (a != 3) goto repeat;
else goto end;
repeat:
count++;
goto start;
end:
string congrats = "you have repeated: " + count.ToString() + "times!";
}
Though others have answered the specific goto question I want to offer a way without using gotos, which might prove better in the long run if you want to count things while you loop.
Add a count:
string q1, q2, q3, q4, q5;
int count = 0;
Then let's use a while loop.
/*------------------------------QUESTION 1-------------------------------- */
while (true)
{
++count;
Console.WriteLine("The JavaScript Language is not object oriented (True/False)");
//... other questions as they were....
// with one small change e.g.
if (q1 == "TRUE")
{
Console.WriteLine();
Console.WriteLine("Well Done, you may move on to the next question");
}
else
{
Console.WriteLine("Sorry you got the answer wrong, you have to start again");
continue; // <-----------instead of goto start;
}
//... other questions as they were....
//with one small change
if (q5 == "FALSE")
{
Console.WriteLine();
Console.WriteLine("Well Done, you may move on to the next question");
Console.WriteLine();
Console.WriteLine("Congratulations You have passed the quiz!");
break; //<--------- if they get the final question right,
// break out of the while loop
}
} //<- end of while loop
//Output message you suggested
Console.WriteLine("You took {0} attempts", count);
Realistically, the goto and while/continue/break have the same effect, but give the program a bit more structure.
If you want to give them a maximum number of tries you can start considering using a for loop instead.
Starting to learn about loops. It seems like these things go on forever because I am not telling it to stop. Problem is I don't know how to tell it to stop. I am assuming a statement such as != but I really do not know.
Anyone care to explain how loops stop?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication326
{
class Program
{
static void Main(string[] args)
{
bool triLoop = true;
while (triLoop)
{
Console.WriteLine("Please enter the first integer...");
int firstInt = int.Parse(Console.ReadLine());
Console.WriteLine("Please enter the second integer...");
int secondInt = int.Parse(Console.ReadLine());
Console.WriteLine("Please enter the third integer...");
int thirdInt = int.Parse(Console.ReadLine());
if ((firstInt + secondInt > thirdInt) && (secondInt + thirdInt > firstInt) && (firstInt + thirdInt > secondInt))
{
Console.WriteLine("The numbers {0}, {1}, and {2} CAN represent sides of the same triangle.", firstInt, secondInt, thirdInt);
}
else
{
Console.WriteLine("The numbers {0}, {1}, and {2} CANNOT represent the sides of the same triangle.", firstInt, secondInt, thirdInt);
}
}
}
}
}
The break statement will "break out" of a loop.
Alternatively, you can just set your boolean value (triLoop) to false.
Anyone care to explain how loops stop
While loop will run for as long as the condition within the loop is true. In order to break it you need to set the expression in the while loop to false.
it will stop when you set triLoop to false. You should read the documentation.
while(triLoop)
{
if(somecondition)
triLoop = false; //loop will not run after this
}
a basic example of this. This loop will run till 5.
int n = 1;
while (n < 6)
{
Console.WriteLine("Current value of n is {0}", n);
n++;
}
Set triLoop to false. Or use break;.
Other answers have explained how the condition works, but if you want to ask the user whether they want to continue, you could add this to the end of your loop:
Console.WriteLine("Would you like to continue? (Y/N)");
if(Console.ReadLine() == "Y")
triLoop = false;
Then the condition will evaluate to false if the user types "Y", and the loop will end.
There are multiple answers.
break; //exits the loop and continues with the code
return; //stops the loop and doesn't proceed with the rest of the code
In your case, you can also set triloop to false.
use this in loops to stop loops
break;
as you are using while loop
while (triLoop)
{
}
this loop runs while triLoop variable is true
you need to set it to false somewhere within while loop
like
while (triLoop)
{
//your code
// on some condition
triLoop = false;
}
or
while (triLoop)
{
//your code
// on some condition
break;
}
try this:
while (triLoop)
{
Console.WriteLine("Please enter the first integer...");
int firstInt = int.Parse(Console.ReadLine());
Console.WriteLine("Please enter the second integer...");
int secondInt = int.Parse(Console.ReadLine());
Console.WriteLine("Please enter the third integer...");
int thirdInt = int.Parse(Console.ReadLine());
if ((firstInt + secondInt > thirdInt) && (secondInt + thirdInt > firstInt) && (firstInt + thirdInt > secondInt))
{
Console.WriteLine("The numbers {0}, {1}, and {2} CAN represent sides of the same triangle.", firstInt, secondInt, thirdInt);
}
else
{
Console.WriteLine("The numbers {0}, {1}, and {2} CANNOT represent the sides of the same triangle.", firstInt, secondInt, thirdInt);
}
Console.WriteLine("press 0 if you want to continue...");
int flag = int.Parse(Console.ReadLine());
if(flag!=0) break;
}