Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 20th, 2007, 6:32 AM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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.
357mag is offline   Reply With Quote
Old May 20th, 2007, 6:55 AM   #2
cloud-
Hobbyist Programmer
 
Join Date: Jan 2005
Posts: 110
Rep Power: 4 cloud- is on a distinguished road
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)
  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

Last edited by cloud-; May 20th, 2007 at 7:22 AM.
cloud- is offline   Reply With Quote
Old May 20th, 2007, 7:21 AM   #3
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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.
357mag is offline   Reply With Quote
Old May 20th, 2007, 1:07 PM   #4
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 381
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
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 !
xavier is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:59 PM.

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