🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.c++.leda
4 messages
4 total messages Started by Ki Hong Park Sat, 30 Sep 1995 22:17
Help me please...
#109
Author: Ki Hong Park
Date: Sat, 30 Sep 1995 22:17
47 lines
1623 bytes
I was trying to write a simple program and ran into a problem that
I didn't know why the compiler was giving error.
I am using AT&T C++ 3.0 compiler and am working on DEC stations
with Unix on it.

Here is my problem.
I have a input file called INPUT which contains some numbers.
ex.  5 10 15 20
The program is suppose to read first input number and create an array
with that length.
ex.  if first input = 5, then length = input and Array[length].

This is what I have.

#include <fstream.h>
int main ()
{
   ifstream input("INPUT"); // the file INPUT contains:  5 10

   int a;
   int b; 

   input >> a;
   input >> b;

   int ArrayA[a];  // line 12
   int ArrayB[b];  // line 13

// and I will have some inputs for those Arrays.

   return 0;
}

When I try to compile this I get an error like this:
% CC test.C  // compile command at the prompt
CC  test.C:
"test.C", line 12: error: cannot evaluate constant
"test.C", line 13: error: cannot evaluate constant
2 errors


What is wrong with this simple program?
What can I do so that I can create arrays with the input from the file?

Please help me and reply to:  khpark@eos.ncsu.edu
Thanks.
Re: Help me please...
#113
Author: Wei-ju Wu
Date: Mon, 02 Oct 1995 11:09
75 lines
2565 bytes
In article <44kfkv$mf5@taco.cc.ncsu.edu>,
	khpark@eos.ncsu.edu (Ki Hong Park) writes:
>
>I was trying to write a simple program and ran into a problem that
>I didn't know why the compiler was giving error.
>I am using AT&T C++ 3.0 compiler and am working on DEC stations
>with Unix on it.
>
>Here is my problem.
>I have a input file called INPUT which contains some numbers.
>ex.  5 10 15 20
>The program is suppose to read first input number and create an array
>with that length.
>ex.  if first input = 5, then length = input and Array[length].
>
>This is what I have.
(...)
>   int a;
>   int b; 
>   input >> a;
>   input >> b;
>   int ArrayA[a];  // line 12
>   int ArrayB[b];  // line 13
>   return 0;
>When I try to compile this I get an error like this:
>% CC test.C  // compile command at the prompt
>CC  test.C:
>"test.C", line 12: error: cannot evaluate constant
>"test.C", line 13: error: cannot evaluate constant
>2 errors
>
>
>What is wrong with this simple program?
>What can I do so that I can create arrays with the input from the file?
>
>Please help me and reply to:  khpark@eos.ncsu.edu
>Thanks.


You can't initialize the arrays using variables, at least, not this way.
My suggestion is, if you really want to allocate them with variables
(Why not using constants ?), do it dynamically, or use the LEDA class
"array" which is in <LEDA/array.h>
If you have this then you can do something semilar to:

#include <iostream.h>
#include <LEDA/array.h>

int main() {
	int a,b,i;

	cin >> a; cin >> b; // replace with file input

	array<int> ArrayA(a);
	array<int> ArrayB(b);
	// Read the values for the arrays
}

I tried this and it worked on our university system without problems
using the GNU-C++ compiler

If you don't have LEDA (why do you post it in this group then ?)
you might also use your C++ compiler's own array class or
make your ArrayB/ArrayA pointers to ints and
try:
	ArrayA=new int[a];
This is a less elegant way, but anyway, it works.

Ciao, Wei-ju

------------------------
Wei-ju Wu
Fachbereich Informatik
Freie Universitaet Berlin
wu@inf.fu-berlin.de
Re: Help me please...
#126
Author: maite
Date: Tue, 10 Oct 1995 11:10
134 lines
3724 bytes
wu@inf.fu-berlin.de (Wei-ju Wu) wrote:
>
> In article <44kfkv$mf5@taco.cc.ncsu.edu>,
> 	khpark@eos.ncsu.edu (Ki Hong Park) writes:
> >
> >I was trying to write a simple program and ran into a problem that
> >I didn't know why the compiler was giving error.
> >I am using AT&T C++ 3.0 compiler and am working on DEC stations
> >with Unix on it.
> >
> >Here is my problem.
> >I have a input file called INPUT which contains some numbers.
> >ex.  5 10 15 20
> >The program is suppose to read first input number and create an array
> >with that length.
> >ex.  if first input = 5, then length = input and Array[length].
> >
> >This is what I have.
> (...)
> >   int a;
> >   int b; 
> >   input >> a;
> >   input >> b;
> >   int ArrayA[a];  // line 12
> >   int ArrayB[b];  // line 13
> >   return 0;
> >When I try to compile this I get an error like this:
> >% CC test.C  // compile command at the prompt
> >CC  test.C:
> >"test.C", line 12: error: cannot evaluate constant
> >"test.C", line 13: error: cannot evaluate constant
> >2 errors
> >
> >
> >What is wrong with this simple program?
> >What can I do so that I can create arrays with the input from the file?
> >
> >Please help me and reply to:  khpark@eos.ncsu.edu
> >Thanks.
> 
> 
> You can't initialize the arrays using variables, at least, not this way.
> My suggestion is, if you really want to allocate them with variables
> (Why not using constants ?), do it dynamically, or use the LEDA class
> "array" which is in <LEDA/array.h>
> If you have this then you can do something semilar to:
> 
> #include <iostream.h>
> #include <LEDA/array.h>
> 
> int main() {
> 	int a,b,i;
> 
> 	cin >> a; cin >> b; // replace with file input
> 
> 	array<int> ArrayA(a);
> 	array<int> ArrayB(b);
> 	// Read the values for the arrays
> }
> 
> I tried this and it worked on our university system without problems
> using the GNU-C++ compiler
> 
> If you don't have LEDA (why do you post it in this group then ?)
> you might also use your C++ compiler's own array class or
> make your ArrayB/ArrayA pointers to ints and
> try:
> 	ArrayA=new int[a];
> This is a less elegant way, but anyway, it works.
> 
> Ciao, Wei-ju
> 
> ------------------------
> Wei-ju Wu
> Fachbereich Informatik
> Freie Universitaet Berlin
> wu@inf.fu-berlin.de
> 


Hello, I´ve got a similar problem.

I want to read an array structure from a file in the same way,
the first number of the line will be the size of the array.
I use the leda type array, but my problem is that I use a class
and I don´t know how to implement the defenition of the " >> "
and "<<" operators.

Simplifying, my class is like that:


class ruta {

	int number_of_nodes;
	array<int> nodes; // array with empty index set

	public:

	ruta () { --- }
	ruta (const ruta& r) { ---}

	friend istream& operator >> (istream& is, ruta& r)
	{
	---
	I suppose I need to read the size (first number) and resize
	the array in order to read it.
	¿?
	}

	friend ostream& operator << (ostream& os, const ruta& r)
	{
	¿?
	}

	friend int compare (const ruta& r, const ruta& r)
	{
	---
	}
}

The data input file would be something like:

3; 1 2 3
2; 2 4
2; 4 1
3; 1 4 2
..

Any help will be appreciated.

Thanks

Luis Bernaldez
luis@mat.upc.es
Re: Help me please...
#131
Author: Wei-ju Wu
Date: Fri, 13 Oct 1995 11:50
31 lines
1218 bytes
In article <45dkb7$d0v@diable.upc.es>,
	maite <luis@mat.upc.es> writes:

>Hello, I´ve got a similar problem.
>
>I want to read an array structure from a file in the same way,
>the first number of the line will be the size of the array.
>I use the leda type array, but my problem is that I use a class
>and I don´t know how to implement the defenition of the " >> "
>and "<<" operators.
>
>The data input file would be something like:
>
>3; 1 2 3
>2; 2 4
>2; 4 1
>3; 1 4 2

Perhaps you should do the following after reading in the size
of the array

your_array=array<int>(size);

and then read in the values.

Wei-ju
------------------------
Wei-ju Wu
Fachbereich Informatik
Freie Universitaet Berlin
wu@inf.fu-berlin.de
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads