C# coding error- Learning

josh.gillespie1998

Prominent
Aug 30, 2017
10
0
560
0
I have highlighted the error. I can't see the issue with it. Please help! :)



___________________code____________________________

using System.Text;
using System.Threading.Tasks;

namespace FirstConsoleProject
{
class Program
{
static void Main(string[] args) //this is a method called Main. It is called when the program starts.
{
Start:

Console.WriteLine("Welcome to the quiz! This test has a series of questions to be answered.");
Console.WriteLine("\rGet any wrong, you have to start again.");
Console.WriteLine("\rPress enter to begin!");
Console.ReadKey();


//Question 1
Console.WriteLine("What is 10 divided by 2?"); //This is text that can be seen.
Console.ReadKey(); //This lets the player type into the console.

int answer = Convert.ToInt32(Console.ReadLine());
if (answer == 5) //Checks if the answer is correct
{
Console.WriteLine("You have answered 5, This is correct");
}

else if (answer != 5) // if it is not correct it will output this message
{
Console.WriteLine("You have answered " + answer + ", this is incorrect. Try again!");
Console.ReadKey();

goto Start; //This will send the player back to the start.
}

}

}







 

josh.gillespie1998

Prominent
Aug 30, 2017
10
0
560
0
using System;
namespace FirstConsoleProject

{

class MainClass

{

public static void Main(string[] args) // this is a method called "Main". It is called when the program starts.

{

int num01 = 16;

int num02 = 4;
Console.WriteLine("What is " + num01 + " times " + num02 + "?");
int answer = Convert.ToInt32(Console.ReadLine());
if (answer == num01 * num02)
{

Console.WriteLine("Well done! Your answer is correct.");

}
else
{

Console.WriteLine("Are you even trying?");

}
Console.ReadKey();

}

}

}
 
1. One of the ways to process console input where numbers-only are expected is to enclose string-to-number conversion in "try / catch " block.
2. Using "goto" for simple loop is not the proper way to write software in 2018.
3. The prompt to the user suggests that if it provides "correct" answer, he/she won't have to start over, which is not the case.
 
Thread starter Similar threads Forum Replies Date
David___Only Apps General Discussion 0
R Apps General Discussion 1
L Apps General Discussion 7
S Apps General Discussion 6
T Apps General Discussion 2
J Apps General Discussion 1
ExtreamChaozZ Apps General Discussion 6
Darkmatterx Apps General Discussion 3
L Apps General Discussion 1
A Apps General Discussion 6
M Apps General Discussion 11
A Apps General Discussion 2
P Apps General Discussion 5
N Apps General Discussion 2
R Apps General Discussion 1
D Apps General Discussion 2
P Apps General Discussion 8
T Apps General Discussion 10
K Apps General Discussion 4
Mr Davo Apps General Discussion 2

ASK THE COMMUNITY