Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Slight problem with my code (http://www.programmingforums.org/showthread.php?t=13175)

357mag May 20th, 2007 7:32 AM

Slight problem with my code
 
I'm writing a program where I'm writing my own method to calculate something. The user is asked to enter 3 integers, and then the program calls a method called Sum to compute the sum and display it. But I'm getting a compiler error. I don't know why since I'm taking a concept from my book and just applying it to my own program, but what I got written is pretty much exactly the same way the program looks in the book. Here is my program:

:

using System;

    class Program
    {
        static void Main()
        {
            int number1, number2, number3;

            Console.Write("Enter 3 integers: ");
            number1 = Convert.ToInt32(Console.ReadLine());
            number2 = Convert.ToInt32(Console.ReadLine());
            number3 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("The sum is ", Sum(number1, number2, number3));
            Console.WriteLine();
        }
    }

static int Sum(int a, int b, int c)
{
    int sumTotal;
   
    sumTotal = a + b + c;

    return sumTotal;
}


But the compiler flags the word "int" right after the word "static" in my method definition and says something about a class, delegate, enum, or
struct is expected.

So I'm kind of confused as to why there is a problem.

cloud- May 20th, 2007 7:55 AM

the Sum() function or any for that matter needs to be in the Program class or a new class/struct etc. ie...

:

  1. using System;
  2. class Program
  3. {
  4.  
  5. // ..... main block { }
  6.  
  7. static int Sum()
  8. {
  9. // .... code
  10. }
  11. }


P.S the code will compile then but there are a couple of things:
Console.WriteLine("The sum is ", Sum(number1, number2, number3)); // "," seperates the parameters passed so unless the WriteLine() function's second parameter also displays text it won't work
// should be
Console.WriteLine("The sum is " + Sum(number1, number2, number3)); // + to join the string and functions return value

^-- if that's how it was in the book and it works sorry, the book > me =P

* might want to think about adding a Console.ReadKey() so you can see the results :P

357mag May 20th, 2007 8:21 AM

Yeah, I just discovered that little error. My program works good now, but how do you split a long line of code into two lines? For example I have this line of code that is rather long:
:

Console.WriteLine("{0}{1}", "The largest is ", Maximum(number1, number2, number3));

Not only is it too long to print on one line when I print my code out, but even in the editor window it looks too long. I tried one method of breaking it up but the compiler wouldn't go for it.

xavier May 20th, 2007 2:07 PM

:

Console.WriteLine("{0}{1}", "The largest is ",
 Maximum(number1, number2, number3));


Just hit enter :P . Unless you're in a string , the compiler won't mind.


All times are GMT -5. The time now is 2:23 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC