Article View: comp.lang.c++.leda
Article #113Re: Help me please...
From: Wei-ju Wu
Date: Mon, 02 Oct 1995 11:09
Date: Mon, 02 Oct 1995 11:09
75 lines
2565 bytes
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
Technical Details
Message-ID:
<44oh8v$svi@fu-berlin.de>Path:
arc1-ca.pugleaf.net!hai.inf.fu-berlin.DE!not-for-mailReferences:
<44kfkv$mf5@taco.cc.ncsu.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!hai.inf.fu-berlin.DE!not-for-mail From: wu@inf.fu-berlin.de (Wei-ju Wu) Newsgroups: comp.lang.c++.leda Subject: Re: Help me please... Date: 2 Oct 1995 11:09:19 GMT Organization: Freie Universitaet Berlin Message-ID: <44oh8v$svi@fu-berlin.de> References: <44kfkv$mf5@taco.cc.ncsu.edu> NNTP-Posting-Host: hai.inf.fu-berlin.de (160.45.112.20) X-Access: 16 17 19 X-Newsreader: knews 0.9.2