none
Console.WriteLine is causing errors with my if and else statements RRS feed

  • Question

  • i'm new to c# so i've been attempting to make a simple calculator but the command Console.WriteLine("please enter a valid sum type"); keeps creating an error just after the next } bracket. anyone know how to fix this?

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

            class Class1
            {
            static void Main(string[] args)
            {
                int num1 = 0;
                int num2 = 0;
                double answer = 0.00;
                string sumType = " ";
                Console.WriteLine("please enter your first value");
                num1 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("please enter your second value");
                num2 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("would you like to add, subtract, multiply or divide?");
                sumType = Console.ReadLine();           
                    if (sumType == "add")
                    {
                        answer = (num1 + num2);
                        Console.WriteLine("{0:0.00}", answer);
                    }
                    else
                        Console.WriteLine("please enter a valid sum type");                               
                    if (sumType == "subtract")
                    {
                        answer = (num1 - num2);
                        Console.WriteLine("{0:0.00}", answer);
                    }
                    else
                    {
                        Console.WriteLine("please enter a valid sum type");
                    }
                else
                    if (sumType == "multiply")
                    {
                        answer = (num1 * num2);
                        Console.WriteLine("{0:0.00}", answer);
                    }
                    else
                    {
                        Console.WriteLine("please enter a valid sum type");
                    }
                else           
                    if (sumType == "divide")
                    {
                        answer = (num1 / num2);
                        Console.WriteLine("{0:0.00}", answer);
                    }
                    else
                    {
                        Console.WriteLine("please enter a valid sum type");
                    }
                    Console.ReadLine();
                }
            }
        }

    Monday, October 22, 2018 7:14 PM

Answers

  • Hi JackMcCann,

    Thank you for posting here.

    For your question, you could try to use if/ else if/ else. And us While loop tp continue the application when you input the invalid string.

    Here is the code for your reference.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Threading;
    namespace Calculator
    {
        class Class1
        {
            static void Main(string[] args)
            {
                int num1 = 0;
                int num2 = 0;
                double answer = 0.00;
                string sumType = " ";
                Console.WriteLine("please enter your first value");
                num1 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("please enter your second value");
                num2 = Convert.ToInt32(Console.ReadLine());
    
                while (true)
                {
                    Console.WriteLine("would you like to add, subtract, multiply or divide?");
                    Console.WriteLine("If you want to stop, please enter quit.");
                    sumType = Console.ReadLine();
                    if (sumType == "add")
                    {
                        answer = (num1 + num2);
                        Console.WriteLine("{0:0.00}", answer);
                    }
                    else if (sumType == "subtract")
                    {
                        answer = (num1 - num2);
                        Console.WriteLine("{0:0.00}", answer);
                    }
                    else if (sumType == "multiply")
                    {
                        answer = (num1 * num2);
                        Console.WriteLine("{0:0.00}", answer);
                    }
                    else if (sumType == "divide")
                    {
                        answer = (num1 / num2);
                        Console.WriteLine("{0:0.00}", answer);
                    }
                    else if (sumType == "quit")
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine("please enter a valid sum type");
                    }
    
                }
                Console.ReadLine();
    
            }
        }
    }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by JackMcCann Tuesday, October 23, 2018 2:04 PM
    Tuesday, October 23, 2018 2:35 AM
    Moderator

All replies

  • Hi,

    if(...){}

    else{}

    else if{}

    is not working. You can not use else if after else. Else is the ending of an if,else if, ... ,else block.

    Greetings, Chris 

    Monday, October 22, 2018 7:36 PM
  • using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;
                using System.Threading.Tasks;
                using System.Threading;
    namespace Calculator
    { 
            class Class1
            {
            static void Main(string[] args)
            {
                int num1 = 0;
                int num2 = 0;
                double answer = 0.00;
                string sumType = " ";
                Console.WriteLine("please enter your first value");
                num1 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("please enter your second value");
                num2 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("would you like to add, subtract, multiply or divide?");
                sumType = Console.ReadLine();           
                    if (sumType == "add")
                    {
                        answer = (num1 + num2);
                        Console.WriteLine("{0:0.00}", answer);
                    }
                    else
                        Console.WriteLine("please enter a valid sum type");                               
                    if (sumType == "subtract")
                    {
                        answer = (num1 - num2);
                        Console.WriteLine("{0:0.00}", answer);
                    }
                    else
                    {
                        Console.WriteLine("please enter a valid sum type");
                    }
                    if (sumType == "multiply")
                    {
                        answer = (num1 * num2);
                        Console.WriteLine("{0:0.00}", answer);
                    }
                    else
                    {
                        Console.WriteLine("please enter a valid sum type");
                    }          
                    if (sumType == "divide")
                    {
                        answer = (num1 / num2);
                        Console.WriteLine("{0:0.00}", answer);
                    }
                    else
                    {
                        Console.WriteLine("please enter a valid sum type");
                    }
                    Console.ReadLine();
                }
            }
        }

    Monday, October 22, 2018 7:46 PM
  • It's running now but is it really  the workflow you expected?
    Monday, October 22, 2018 7:49 PM
  • Hi JackMcCann,

    Thank you for posting here.

    For your question, you could try to use if/ else if/ else. And us While loop tp continue the application when you input the invalid string.

    Here is the code for your reference.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Threading;
    namespace Calculator
    {
        class Class1
        {
            static void Main(string[] args)
            {
                int num1 = 0;
                int num2 = 0;
                double answer = 0.00;
                string sumType = " ";
                Console.WriteLine("please enter your first value");
                num1 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("please enter your second value");
                num2 = Convert.ToInt32(Console.ReadLine());
    
                while (true)
                {
                    Console.WriteLine("would you like to add, subtract, multiply or divide?");
                    Console.WriteLine("If you want to stop, please enter quit.");
                    sumType = Console.ReadLine();
                    if (sumType == "add")
                    {
                        answer = (num1 + num2);
                        Console.WriteLine("{0:0.00}", answer);
                    }
                    else if (sumType == "subtract")
                    {
                        answer = (num1 - num2);
                        Console.WriteLine("{0:0.00}", answer);
                    }
                    else if (sumType == "multiply")
                    {
                        answer = (num1 * num2);
                        Console.WriteLine("{0:0.00}", answer);
                    }
                    else if (sumType == "divide")
                    {
                        answer = (num1 / num2);
                        Console.WriteLine("{0:0.00}", answer);
                    }
                    else if (sumType == "quit")
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine("please enter a valid sum type");
                    }
    
                }
                Console.ReadLine();
    
            }
        }
    }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by JackMcCann Tuesday, October 23, 2018 2:04 PM
    Tuesday, October 23, 2018 2:35 AM
    Moderator
  • @Wendy This looks like an homework. Should we help? Yes! Should we complete the whole homework? NO! ... 
    Tuesday, October 23, 2018 6:52 AM
  • If you are interested, consider the next approach too:

    switch(sumType.ToLower())

    {

    case "add":

       . . .

       break;

    case "subtract":

       . . .

       break;

    case "multiply":

       . . .

       break;

    case "divide":

       . . .

       break;

    default:

       Console.WriteLine("Please enter a valid sum type");

       break;

    }


    Tuesday, October 23, 2018 7:36 AM
  • not a homework just a console program i decided to make to challenge myself. thanks for the concern anyways
    Tuesday, October 23, 2018 1:50 PM
  • not a homework just a console program i decided to make to challenge myself. 

    Even so, you're robbed of much of the learning experience from the
    challenge when someone gives you the complete program. Unfortunately
    some contributors to these forums can't seem to resist the urge to do
    that even when an OP has not asked for it. As in this case where you
    only asked for help with a particular problem in your own code, not 
    a complete rewrite.

    - Wayne

    Tuesday, October 23, 2018 2:04 PM
  • not a homework just a console program i decided to make to challenge myself. thanks for the concern anyways

    Ok, I'm sorry. It looked a bit like a homework.

    I would use switch case instead of using if/else, like Viorel_ mentioned.

    By using switch you need to know that it will execute all the following cases if you are not using break or return... 

    Greetings, Chris

    Tuesday, October 23, 2018 2:13 PM