-7

The Following :

if (parseok == false)
{
    Console.WriteLine("Error : Please enter valid numeric Value ");
    Console.ReadLine();
    Environment.Exit(0);
}
//Is currently working fine, however, I am wanting to change the if statement to a while loop - for error checking purposes. When I change the code to this :

while (parseok == false)
{
    Console.WriteLine("Error : Please enter valid numeric Value ");
    Console.ReadLine();
    Console.Write("Please enter minutes for your first run:    ");
    parseok = int.TryParse(Console.ReadLine(), out run1m);
}
//I am recieveing an error message saying that I'm missing a close bracket, when I add the close bracket the whole program becomes riddled with errors. What am I forgetting to do?

//I will add the entire project's code : 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
        class Program
        {
            //Ajdin Karahodzic 
            //ID : KARAD1301

        static void Main(string[] args)
        {
            String membertype = "";

            //Declaration of Variables 
            string name = "";
            string member = "";
            string gender = "";
            int run1m = 0;
            int run1s = 0;
            int run2m = 0;
            int run2s = 0;
            int run3m = 0;
            int run3s = 0;
            int run1total = 0;
            int run2total = 0;
            int run3total = 0;
            int totalsecs = 0;
            int avgsecs = 0;
            int hours = 0;
            int minutes = 0;
            int seconds = 0;
            bool parseok = false;
            string menu = "";
            int run1tempS = 0;
            int run1tempM = 0;
            int run1tempH = 0;
            int run2tempS = 0;
            int run2tempM = 0;
            int run2tempH = 0;
            int run3tempS = 0;
            int run3tempM = 0;
            int run3tempH = 0;

            while (menu != "x")
            {
                Console.WriteLine("Please choose from the following : ");
                Console.WriteLine("Enter Runner Details (R) : ");
                Console.WriteLine("Enter run times (T) : ");
                Console.WriteLine("Display runner results (D) : ");
                Console.WriteLine("Exit the program (X) ");
                menu = Console.ReadLine();

                switch (menu)
                {
                    //CLOSE PROGRAM SWITCH
                    case "x":
                    case "X":
                        Environment.Exit(0);
                        break;

                    //ENTER RUNNER DATA SWITCH
                    case "r":
                    case "R":
                        Console.Write("Please enter your name:               ");
                        name = Console.ReadLine();

                    while (string.IsNullOrEmpty(name))
                    {
                        Console.WriteLine("Error : Please ensure you have entered your name, press enter to continue ");
                        Console.ReadLine();
                        Console.Write("Please enter your name:               ");
                        name = Console.ReadLine();
                    }

                    Console.Write("Please enter your  membership number: ");
                    member = Console.ReadLine();
                    while (member.Length != 5)
                    {
                        Console.WriteLine("Error : Membership number must be 5 characters long, press enter to continue ");
                        Console.ReadLine();
                        Console.Write("Please enter your  membership number: ");
                        member = Console.ReadLine();
                    }
                    char first = member[0];
                    while (first != 'o' && first != 'O' && first != 's' && first != 'S' && first != 'j' && first != 'J' && first != 'C' && first != 'c')
                    {
                        Console.WriteLine("Error : Membership number must begin with O, S, J or C, press enter to continue ");
                        Console.ReadLine();
                        Console.Write("Please enter your  membership number: ");
                        member = Console.ReadLine();
                    }
                    if (first == 'o' || first == 'O')
                    {
                        membertype = "Ordinary Member";
                    }
                    else if (first == 's' || first == 'S')
                    {
                        membertype = "Student Member";
                    }
                    else if (first == 'j' || first == 'J')
                    {
                        membertype = "Junior Member";
                    }
                    else if (first == 'c' || first == 'C')
                    {
                        membertype = "Child Member";
                    }
                    string response = "";
                    Console.Write("Please enter your gender (m) or (f) : ");
                    response = Console.ReadLine();
                    gender = response;
                    while (gender != "m" && gender != "M" && gender != "f" && gender != "F")
                    {
                        Console.WriteLine("Error : Gender must be either : M / m (For Male) or F / f (For Female), press enter to continues ");
                        Console.ReadLine();
                        Console.Write("Please enter your gender (m) or (f) : ");
                        response = Console.ReadLine();
                        gender = response;
                    }
                    break;

                    //ENTER RUN TIMES - SWITCH
                    case "T":
                    case "t":
                    //Prompt for user input; collect and store data.
                    /*---------RUN 1 INPUT---------
                      -----------------------------*/
                    //MINUTES
                    Console.Write("Please enter minutes for your first run:    ");
                    parseok = int.TryParse(Console.ReadLine(), out run1m);
                    while (parseok == false)
                    {
                        Console.WriteLine("Error : Please enter valid numeric Value ");
                        Console.ReadLine();
                        Console.Write("Please enter minutes for your first run:    ");
                        parseok = int.TryParse(Console.ReadLine(), out run1m);
                    }
                    else if (run1m < 15 || run1m > 180)
                    {
                        Console.WriteLine("Error : Minutes cannot be less than 15 or greater than 180");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    //SECONDS
                    Console.Write("Please enter seconds for your first run:    ");
                    parseok = int.TryParse(Console.ReadLine(), out run1s);
                    if (parseok == false)
                    {
                        Console.WriteLine("Error : Please enter valid numeric Value ");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    else if (run1s < 0 || run1s > 59)
                    {
                        Console.WriteLine("Error : Seconds must be between 0 and 59 ");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    Console.WriteLine();
                    /*---------RUN 2 INPUT---------
                      ------------------------------*/
                    Console.Write("Please enter minutes for your second run:   ");
                    parseok = int.TryParse(Console.ReadLine(), out run2m);
                    if (parseok == false)
                    {
                        Console.WriteLine("Error : Please enter valid numeric Value ");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    else if (run2m < 15 || run2m > 180)
                    {
                        Console.WriteLine("Error : Minutes cannot be less than 15 or greater than 180");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    Console.Write("Please enter seconds for your second run:   ");
                    run2s = int.Parse(Console.ReadLine());
                    if (parseok == false)
                    {
                        Console.WriteLine("Error : Please enter valid numeric Value ");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    else if (run2s < 0 || run2s > 59)
                    {
                        Console.WriteLine("Error : Seconds must be between 0 and 59 ");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    Console.WriteLine();
                    /*---------RUN 3 INPUT---------
                     ------------------------------*/
                    Console.Write("Please enter minutes for your third run:    ");
                    parseok = int.TryParse(Console.ReadLine(), out run3m);
                    if (parseok == false)
                    {
                        Console.WriteLine("Error : Please enter valid numeric Value ");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    else if (run3m < 15 || run3m > 180)
                    {
                        Console.WriteLine("Error : Minutes cannot be less than 15 or greater than 180");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    Console.Write("Please enter seconds for your third run:    ");
                    run3s = int.Parse(Console.ReadLine());

                    if (parseok == false)
                    {
                        Console.WriteLine("Error : Please enter valid numeric Value ");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    else if (run3s < 0 || run3s > 59)
                    {
                        Console.WriteLine("Error : Seconds must be between 0 and 59 ");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    Console.WriteLine();
                    break;
                    case "d":
                    case "D":
                    // CALCULATIONS
                    //Converting individual run times to seconds
                    run1total = (run1m * 60) + run1s;
                    run2total = (run2m * 60) + run2s;
                    run3total = (run3m * 60) + run3s;
                    //Convert individual times to hours, mins, secs.
                    // RUN1
                    run1tempS = (run1total % 60);
                    run1tempM = ((run1total / 60) % 60);
                    run1tempH = ((run1total / 3600) % 60);
                    // RUN2
                    run2tempS = (run2total % 60);
                    run2tempM = ((run2total / 60) % 60);
                    run2tempH = ((run2total / 3600) % 60);
                    // RUN3
                    run3tempS = (run3total % 60);
                    run3tempM = ((run3total / 60) % 60);
                    run3tempH = ((run3total / 3600) % 60);
                    //Calculate average time
                    totalsecs = (run1total + run2total + run3total);
                    avgsecs = (totalsecs / 3);
                    seconds = (avgsecs % 60);
                    minutes = ((avgsecs / 60) % 60);
                    hours = ((avgsecs / 3600) % 60);
                    //Display results
                    Console.WriteLine();
                    Console.WriteLine("==========================================================================");
                    Console.WriteLine("10 Km results for: ");
                    Console.WriteLine("{0} [{1}] - {2}, {3}  ", name, member, membertype, gender.ToUpper());
                    Console.WriteLine("Run 1 - {0} hr(s) {1} min(s) {2} sec(s). ", run1tempH, run1tempM, run1tempS);
                    Console.WriteLine("Run 2 - {0} hr(s) {1} min(s) {2} sec(s). ", run2tempH, run2tempM, run2tempS);
                    Console.WriteLine("Run 3 - {0} hr(s) {1} min(s) {2} sec(s). ", run3tempH, run3tempM, run3tempS);
                    Console.WriteLine();
                    Console.WriteLine("Average 10km run time is : {0} hours {1} minutes {2} seconds. ", hours, minutes, seconds);
                    break;
                    default:
                      Console.WriteLine("Incorrect input, please try again ");
                      break;
                }
                Console.ReadLine();
            }
        }
    }
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
Ajdin
  • 5
  • 1
  • 1
  • 3

2 Answers2

4

On line 161 you are doing an else if but there fore you did'nt specify any if.

enter image description here

I don't know what your doing in your code. But either remove the else in the else if statement or create an if statement before the else if.

An else if statement comes after an if statement or else if statement and can not be the first one in a comparison.

Solution example enter image description here So by adding an if before else if will solve the problem. (Notice I don't now what your program needs to do so you need to check what condition check you have to do.)

Timon Post
  • 2,779
  • 1
  • 17
  • 32
2

Your problem is the left over

else

on line 163. Removing that allows the code to compile, but I'm not going to verify the behaviour.

Matt Hogan-Jones
  • 2,981
  • 1
  • 29
  • 35