![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jan 2005
Posts: 110
Rep Power: 4
![]() |
the Sum() function or any for that matter needs to be in the Program class or a new class/struct etc. ie...
c# Syntax (Toggle Plain Text)
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 ![]() Last edited by cloud-; May 20th, 2007 at 7:22 AM. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#4 |
|
Professional Programmer
|
Console.WriteLine("{0}{1}", "The largest is ",
Maximum(number1, number2, number3));Just hit enter . Unless you're in a string , the compiler won't mind.
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem Associated with Vector Source code | buggytoast | Java | 3 | Apr 2nd, 2006 5:41 AM |
| Problem with porting of Linux code to Windows, FORTRAN/C. | stormlab | C | 1 | Oct 15th, 2005 10:18 PM |
| How to post a question | nnxion | C++ | 10 | Jun 3rd, 2005 11:53 AM |
| How to post a question | nnxion | C++ | 0 | Jun 3rd, 2005 8:55 AM |
| How to post a question | nnxion | C | 0 | Jun 3rd, 2005 8:55 AM |