471,627 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,627 software developers and data experts.

Invalid token .... Very Very simply program, why do I get this?

I'm using 2002.Net, 1.0 framwork.

I am basically learning C# so I wrote this and get invalid token
errors. Does anyone see why?

using System;
namespace ConsoleApplication2
{
class Class1
{
static void Main(string[] args)
{
Stack stack = new Stack() ;
int i = 0 ;
for (; i<stack.maxSize; ++i)
{
Console.Write("Pushing [" + stack.Push(i+10)+ "]") ;
Console.WriteLine(" now Top becomes: " + stack._top) ;
Console.WriteLine() ;
}

int j = 0 ;
for (; j<stack.maxSize; ++j)
{
Console.Write("Popping [" + j + "] of " + stack.maxSize) ;
Console.WriteLine("Content: " + stack.Pop()) ;
}
}
}
}

using System;

namespace ConsoleApplication2
{

class stack
{

//ERRORS BEGIN HERE SUCH AS "Invalid
token ";" in the private _top; line.
private _top;
public int const maxsize = 10;
private _arr[maxSize];

public stack()
{
_top = 0;

}

void push (int val)
{
if (_top < maxSize)
{
_arr[_top] = val;
++_top;
}
else
{
break;
}
}

int pop()
{
if (_top > 0)
{
--_top;
return _arr[_top];
}
else
{
break;
}
}
{}
}
}

Feb 9 '06 #1
9 2599
You haven't defined the type of _top. The compiler doesn't know what it
is.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Ne****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
I'm using 2002.Net, 1.0 framwork.

I am basically learning C# so I wrote this and get invalid token
errors. Does anyone see why?

using System;
namespace ConsoleApplication2
{
class Class1
{
static void Main(string[] args)
{
Stack stack = new Stack() ;
int i = 0 ;
for (; i<stack.maxSize; ++i)
{
Console.Write("Pushing [" + stack.Push(i+10)+ "]") ;
Console.WriteLine(" now Top becomes: " + stack._top) ;
Console.WriteLine() ;
}

int j = 0 ;
for (; j<stack.maxSize; ++j)
{
Console.Write("Popping [" + j + "] of " + stack.maxSize) ;
Console.WriteLine("Content: " + stack.Pop()) ;
}
}
}
}

using System;

namespace ConsoleApplication2
{

class stack
{

//ERRORS BEGIN HERE SUCH AS "Invalid
token ";" in the private _top; line.
private _top;
public int const maxsize = 10;
private _arr[maxSize];

public stack()
{
_top = 0;

}

void push (int val)
{
if (_top < maxSize)
{
_arr[_top] = val;
++_top;
}
else
{
break;
}
}

int pop()
{
if (_top > 0)
{
--_top;
return _arr[_top];
}
else
{
break;
}
}
{}
}
}

Feb 9 '06 #2
A variable must have a type, as far as I know :'-(

Instead of:
private _top;
Use:
private int _top;

<Ne****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
I'm using 2002.Net, 1.0 framwork.

I am basically learning C# so I wrote this and get invalid token
errors. Does anyone see why?

using System;
namespace ConsoleApplication2
{
class Class1
{
static void Main(string[] args)
{
Stack stack = new Stack() ;
int i = 0 ;
for (; i<stack.maxSize; ++i)
{
Console.Write("Pushing [" + stack.Push(i+10)+ "]") ;
Console.WriteLine(" now Top becomes: " + stack._top) ;
Console.WriteLine() ;
}

int j = 0 ;
for (; j<stack.maxSize; ++j)
{
Console.Write("Popping [" + j + "] of " + stack.maxSize) ;
Console.WriteLine("Content: " + stack.Pop()) ;
}
}
}
}

using System;

namespace ConsoleApplication2
{

class stack
{

//ERRORS BEGIN HERE SUCH AS "Invalid
token ";" in the private _top; line.
private _top;
public int const maxsize = 10;
private _arr[maxSize];

public stack()
{
_top = 0;

}

void push (int val)
{
if (_top < maxSize)
{
_arr[_top] = val;
++_top;
}
else
{
break;
}
}

int pop()
{
if (_top > 0)
{
--_top;
return _arr[_top];
}
else
{
break;
}
}
{}
}
}

Feb 9 '06 #3
You get the error because in the cases of _top and _arr you don't say
what type they are.

You can't just say: "private _top;" you have to say something like
"private int _top;"

Feb 9 '06 #4
Additionally, your const statement should come before the type.
Finally, your array declaration should look like this:

private int[] _arr;
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Ne****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
I'm using 2002.Net, 1.0 framwork.

I am basically learning C# so I wrote this and get invalid token
errors. Does anyone see why?

using System;
namespace ConsoleApplication2
{
class Class1
{
static void Main(string[] args)
{
Stack stack = new Stack() ;
int i = 0 ;
for (; i<stack.maxSize; ++i)
{
Console.Write("Pushing [" + stack.Push(i+10)+ "]") ;
Console.WriteLine(" now Top becomes: " + stack._top) ;
Console.WriteLine() ;
}

int j = 0 ;
for (; j<stack.maxSize; ++j)
{
Console.Write("Popping [" + j + "] of " + stack.maxSize) ;
Console.WriteLine("Content: " + stack.Pop()) ;
}
}
}
}

using System;

namespace ConsoleApplication2
{

class stack
{

//ERRORS BEGIN HERE SUCH AS "Invalid
token ";" in the private _top; line.
private _top;
public int const maxsize = 10;
private _arr[maxSize];

public stack()
{
_top = 0;

}

void push (int val)
{
if (_top < maxSize)
{
_arr[_top] = val;
++_top;
}
else
{
break;
}
}

int pop()
{
if (_top > 0)
{
--_top;
return _arr[_top];
}
else
{
break;
}
}
{}
}
}

Feb 9 '06 #5
You have

private _top;

You seem to have forgotten to declare what type of field _top is. Try

private int _top;


"Ne****@gmail.com" wrote:
I'm using 2002.Net, 1.0 framwork.

I am basically learning C# so I wrote this and get invalid token
errors. Does anyone see why?

using System;
namespace ConsoleApplication2
{
class Class1
{
static void Main(string[] args)
{
Stack stack = new Stack() ;
int i = 0 ;
for (; i<stack.maxSize; ++i)
{
Console.Write("Pushing [" + stack.Push(i+10)+ "]") ;
Console.WriteLine(" now Top becomes: " + stack._top) ;
Console.WriteLine() ;
}

int j = 0 ;
for (; j<stack.maxSize; ++j)
{
Console.Write("Popping [" + j + "] of " + stack.maxSize) ;
Console.WriteLine("Content: " + stack.Pop()) ;
}
}
}
}

using System;

namespace ConsoleApplication2
{

class stack
{

//ERRORS BEGIN HERE SUCH AS "Invalid
token ";" in the private _top; line.
private _top;
public int const maxsize = 10;
private _arr[maxSize];

public stack()
{
_top = 0;

}

void push (int val)
{
if (_top < maxSize)
{
_arr[_top] = val;
++_top;
}
else
{
break;
}
}

int pop()
{
if (_top > 0)
{
--_top;
return _arr[_top];
}
else
{
break;
}
}
{}
}
}

Feb 9 '06 #6
Thanks, I new it was something simple. I got this example of the web
(http://www.c-sharpcorner.com/Languag...tionInCSDS.asp)
and thought I fool around with it. Too bad it had errors in it.

Feb 9 '06 #7
In fact...
"Csharper" <Ne****@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Thanks, I new it was something simple. I got this example of the web
(http://www.c-sharpcorner.com/Languag...tionInCSDS.asp)
and thought I fool around with it. Too bad it had errors in it.

Feb 9 '06 #8
Apparently there were other errors in the example code I used. The
follow code works now:

using System;

namespace ConsoleApplication2
{
class Class1
{
static void Main(string[] args)
{
Stack stack = new Stack() ;
int i = 0 ;
for (; i<stack.getSize(); ++i)
{
stack.push(i+10);
Console.Write("Pushing [" + (i+10) + "]") ;
Console.WriteLine(" now Top becomes: " + stack.getTop()) ;
Console.WriteLine() ;
}

int j = 0 ;
for (; j<stack.getSize(); ++j)
{
Console.Write("Popping [" + j + "] of " + stack.getSize()) ;
Console.WriteLine("Content: " + stack.pop()) ;
}
}
}

}
using System;

namespace ConsoleApplication2
{
public class Stack
{
private int _top;
public const int maxSize = 10;
private int[] _arr = new int[maxSize];

public Stack()
{
_top = 0;

}

public int getTop()
{
return _top;
}

public int getSize()
{
return maxSize;
}

public void push (int val)
{
if (_top < maxSize)
{
_arr[_top] = val;
++_top;
}
else
{
}
}

public int pop()
{
if (_top > 0)
{
--_top;
return _arr[_top];
}
else
{
return 0;
}
}
}
}

Feb 9 '06 #9
Ouch. They really should test their code before they post it in an
article. That's embarrassing, especially when they build the program up
line by line, with the same error persisting throughout the article.

Feb 9 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.