Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old May 21st, 2007, 4:28 AM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
Does not exist in the current context

Another program I'm working on is a program that uses a loop to output the value of the loop in the first column, that value squared in the second column, and that value cubed in the third column. But the compiler is saying that my square()method and my cube()method do not exist in the current context. I sense that it's got something to do with maybe the compiler can't access the methods because they are placed in the body of the for loop. But I don't know how to fix it. Here is the code:

class Program
{
    static void Main()
    {
        Console.Write("Integer       Square       Cube");

        for (int i = 1; i <= 5; i++)
        {
            Console.Write("{0}", i);
            Console.Write(Square(i));
            Console.Write(Cube(i));
            Console.WriteLine();
        }
    }

    static int square(int x)
    {
        return x * x;
    }

    static int cube(int y)
    {
        return y * y * y;
    }
}
357mag is offline   Reply With Quote
Old May 21st, 2007, 5:30 AM   #2
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
Ah, capitalization counts. I'm defining the method as square but calling it with Square. So that fixes that problem. Now I just need to get the output looking the way I want.
357mag is offline   Reply With Quote
Old May 21st, 2007, 6:36 AM   #3
Ghost
Man Bear Pig Hunter
 
Ghost's Avatar
 
Join Date: Jul 2005
Location: NorCal, USA
Posts: 310
Rep Power: 4 Ghost is on a distinguished road
This is starting to sound a lot like homework...
__________________
People who click "images" that end with .exe shouldn't have computers.
Ghost is offline   Reply With Quote
Old May 21st, 2007, 6:57 AM   #4
Ghost
Man Bear Pig Hunter
 
Ghost's Avatar
 
Join Date: Jul 2005
Location: NorCal, USA
Posts: 310
Rep Power: 4 Ghost is on a distinguished road
Even thou this seems to be homework, it is 3am and I'm bored so here is how I'd do it. Hope it helps you discover what you were missing before.

Program.cs

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

namespace DemoApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string IntVal, SquareVal, CubeVal;

            Console.Write("Integer   Square    Cube");
            Console.WriteLine(); // New Line
            // Start the fun loop of math
            for (int i = 1; i <= 5; i++)
            {
                IntVal = i.ToString();
                SquareVal = Square(i).ToString();
                CubeVal = Cube(i).ToString();
                Console.Write(IntVal.PadRight(10)); // Write Int
                Console.Write(SquareVal.PadRight(10)); // Write Squared
                Console.Write(CubeVal.PadRight(10)); // Write Cubed
                Console.WriteLine(); // New Line

            }
        }

        private static string Square(int x)
        {
            return Convert.ToString(x * x);
        }

        private static string Cube(int y)
        {
            return Convert.ToString(y * y * y);
        }
    }
}

I added the padding for formatting purposes.... I like things to line up.

I recoded it all, but your code is fine (now that you corrected your cap problem), maybe you should create a "displayformat" function.
__________________
People who click "images" that end with .exe shouldn't have computers.
Ghost 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
linked list problems bl00dninja C++ 6 Feb 17th, 2008 11:30 AM
singly-linked list templaste class in C++ w/ example driver bl00dninja Show Off Your Open Source Projects 0 Sep 11th, 2006 2:05 AM
dev c++ software, template problem cairo C++ 11 Jun 2nd, 2006 1:42 PM
How to get the URL of current IE window librallguy C++ 3 Feb 12th, 2006 3:07 AM
Context Sensitive Help [How to]WIN32 hemanth.balaji C++ 1 May 22nd, 2005 1:57 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:43 AM.

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