![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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;
}
} |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#3 |
|
Man Bear Pig Hunter
Join Date: Jul 2005
Location: NorCal, USA
Posts: 310
Rep Power: 4
![]() |
This is starting to sound a lot like homework...
__________________
People who click "images" that end with .exe shouldn't have computers. |
|
|
|
|
|
#4 |
|
Man Bear Pig Hunter
Join Date: Jul 2005
Location: NorCal, USA
Posts: 310
Rep Power: 4
![]() |
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. |
|
|
|
![]() |
| 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 |
| 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 |