Visual C question

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
1) That isn't Visual C, it's c#, and console based
2) It isn't going to do anything because your main method exits without doing anything. IIRC, visual studio will pop up a command box for a command line application and it will close as soon as the program finishes so you'll just see a quick flash of it.
3) To run it, hit the lovely green arrow directly under the "Window" menu
 

mOeeOm

Platinum Member
Dec 27, 2004
2,588
0
0
Originally posted by: kamper
1) That isn't Visual C, it's c#, and console based
2) It isn't going to do anything because your main method exits without doing anything. IIRC, visual studio will pop up a command box for a command line application and it will close as soon as the program finishes so you'll just see a quick flash of it.
3) To run it, hit the lovely green arrow directly under the "Window" menu

Ya, that green arrow is the debug button. So how do I fix it to show something? This is my first time working with this so...
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
You need to pause and wait for a key at the end, otherwise the console exits too fast for you to see anything. Try Console.ReadKey or Console.ReadLine. The first method will return as soon as the user pressed a key. The second method will return when the user presses enter to end a line.
 

mOeeOm

Platinum Member
Dec 27, 2004
2,588
0
0
Originally posted by: Markbnj
You need to pause and wait for a key at the end, otherwise the console exits too fast for you to see anything. Try Console.ReadKey or Console.ReadLine. The first method will return as soon as the user pressed a key. The second method will return when the user presses enter to end a line.

Where would you put that in the code? I tried after declaring the variables and after the return, it gives me this error: Error 1 Only assignment, call, increment, decrement, and new object expressions can be used as a statement C:\Documents and Settings\moe\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\Program.cs 15 13 ConsoleApplication1

Sorry to ask, but would you put it where its supposed to be, I just started this so...:(
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Put it before the return statement

Once the return statement executes; all code afterwards within the function will not execute within that call to the function.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
You'd better post that code. There's something more wrong with it then just the location of the statement.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
from the screenshot, it looks like you're not doing anything in your main function.
 

mOeeOm

Platinum Member
Dec 27, 2004
2,588
0
0
The first code worked after using Console.Realine(), but now I'm trying a different code and the method doesn't work:

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
}
int fun0()
{
int i;
for(i = 0; i < 100; i++)
if( i == 100 )
i = 0;
else
i *= 2;
Console.ReadLine();
return(i);
}
}
}

it compiles with no errors, but doesn't display anything.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
First it does not look like your function is ever called.

Second where in your function(s) do you output anything?

Your function returns an integer. What is done by which ever code area that calls the functions.

I looks like you have a basic building block - the overall skeleton needs to be fleshed out.
 

mOeeOm

Platinum Member
Dec 27, 2004
2,588
0
0
Originally posted by: EagleKeeper
First it does not look like your function is ever called.

Second where in your function(s) do you output anything?

Your function returns an integer. What is done by which ever code area that calls the functions.

I looks like you have a basic building block - the overall skeleton needs to be fleshed out.

I'm still a beginner so I really don't know how to call functions and all that, basically some tracing problems were posted and I was going through them, but to know if I'm right or wrong I need to run it. I'd love to learn this on my own, it sucks to ask but would you just fix this code so I could just keep putting in the different questions after trying them to check the answer if possible.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
You should learn how to call functions at the same time as learning to write them. Stop using fun0() and put everything in the Main method for now.
 

mOeeOm

Platinum Member
Dec 27, 2004
2,588
0
0
Originally posted by: kamper
You should learn how to call functions at the same time as learning to write them. Stop using fun0() and put everything in the Main method for now.

Main method...I don't know what to do with that. Change fun0() to main() like in normal C or remove namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
}

neither works. Sorry.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Fixing the code for you will npot help you learn from your mistakes.

The best that we should do to help you is to point out where you are making mistakes and let you figure out how to correct them.

You may wish to learn how to build a Hello World program and then expand from there in small steps.
 

beggerking

Golden Member
Jan 15, 2006
1,703
0
0
Originally posted by: mOeeOm
The first code worked after using Console.Realine(), but now I'm trying a different code and the method doesn't work:

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
}
int fun0()
{
int i;
for(i = 0; i < 100; i++)
if( i == 100 )
i = 0;
else
i *= 2;
Console.ReadLine();
return(i);
}
}
}

it compiles with no errors, but doesn't display anything.

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//caller
console.writeline(fun0)

}
int fun0()
{
int i;
for(i = 0; i < 100; i++)
if( i == 100 )
i = 0;
else
i *= 2;
Console.ReadLine();
return(i);
}
}
}
 

SoftwareEng

Senior member
Apr 24, 2005
553
4
81
Originally posted by: kamper
You should learn how to call functions at the same time as learning to write them. Stop using fun0() and put everything in the Main method for now.

what are u talkin,

int _ = 0;
int __ = 123;
int ___ = _ + __;

int ____(int _) {
return (___++) - _;
}

THERE U GO :)
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: beggerking
console.writeline(fun0)
Wtf? Assuming that even compiles, wouldn't it print something along the lines of the address of the method?
 

SoftwareEng

Senior member
Apr 24, 2005
553
4
81
Originally posted by: kamper
Originally posted by: beggerking
console.writeline(fun0)
Wtf? Assuming that even compiles, wouldn't it print something along the lines of the address of the method?

no, it will simply call the function, take the result, and pass it to the writeline function. If fun0 takes an int and returns an int, we can do:

console.writeline(fun0(fun0(fun0(fun0(1)))))
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: SoftwareEng
Originally posted by: kamper
Originally posted by: beggerking
console.writeline(fun0)
Wtf? Assuming that even compiles, wouldn't it print something along the lines of the address of the method?
no, it will simply call the function, take the result, and pass it to the writeline function. If fun0 takes an int and returns an int, we can do:
console.writeline(fun0(fun0(fun0(fun0(1)))))
So you can call methods in c# without using parentheses? :confused: I remember doing that in pascal but never anything else...