tags:

views:

86

answers:

5

I'm having a little trouble with this code im writing for a simple program. I get tons of errors saying "invalid token" The program basically asks for 2 integers and sums them up, but the program needs to be called in another method.

Thanks in advance :D

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

namespace AustinPDangeloJPA03
{
    class Add
    {
        static void Main(string[] args)
        {
            double num1,
                   num2,
                   sum;

            Console.Write("Enter the First integer: ");
            num1 = int.Parse(Console.ReadLine());
            //First Integer entered and storred 
            Console.Write("Enter the Second integer: ");
            num2 = int.Parse(Console.ReadLine());
            //Second Integer entered and storred
            sum = Display(double a, double b);
            //First and second numbers added together

            Console.WriteLine(" {0} + {1} = {2} ",num1,num2,sum); 
            //displays the sum

            //Instructs the user to press the Enter key to end the program
            Console.WriteLine("Press the Enter key to terminate the program...");
            Console.ReadLine();

            }//Closes Main Method

        static enum Display(a,b)
        {
            double c = a + b;
            return c;
        }//closes display method

    }//Closes Class Add
}
A: 

Your Display method is not declared correctly.

You need to declare a method that takes two numbers and returns a third number.
Consult your textbook and assignment for more information on how to declare a method and which types to use.

You're also not calling it correctly; method calls do not take types.

SLaks
+2  A: 

This is not correct:

static enum Display(a,b)
{
    double c = a + b;
    return c;

 }

The enum keyword is used to declare an enumeration. In order to define a method, you need a valid return type (such as int or double), and you need to provide the proper types for the individual arguments. You can optionally add static if you want it to be a static method, but that depends on its purpose.

I suspect you want to use something more like:

 double Add(double a, double b)
 {
     // ...

If you then correct the line that called this method:

 sum = Display(double a, double b);

This should compile and give you what you expect.

Reed Copsey
Please do not finish his homework assignment for him.
SLaks
@Slaks: Wasn't trying to provide a full solution - just enough hints, and reasoning why, to get the OP there...
Reed Copsey
A: 

While it is not the source of your errors, it does indicate a misunderstanding of types:

double num1, num2,sum;
[...]
num1 = int.Parse(Console.ReadLine());

The first line declares some double variables.
The second line tries to parse int variables.

While the int will get auto-converted to a double, your code will be better if it is consistent with the use of types. You should switch to either int types, or to Double.Parse().

abelenky
A: 

The enum keyword is for creating enumerations, for example:

public enum Color { Red, Green, Blue };

You have to specify a data type as return type for your Display method, and data types for the parameters:

static double Display(double a, double b) {
  double c = a + b;
  return c;
}

Also, you don't specify data types when you call the method, so change that to:

sum = Display(a, b);
Guffa
A: 

Change this line to:

    double sum = Display(num1, num2);

And change your Display method to be a method.

    private static double Display(double a, double b)
    {
        double c = a + b;
        return c;
    }
Chris Martin
Please do not finish his homework assignment for him.
SLaks
ditto what Reed said. As this is a QA site.
Chris Martin