-2

I'm trying to write a simple program to calculate numerical approximations with Euler's method and every complier I've used hasn't printed anything. Codeblocks is running an error but I think that is because the compiler isn't set up right. xCode will build it but nothing happens. When I run g++ Euler.cpp I get:

Euler.cpp:1: error: expected constructor, destructor, or type conversion before ‘<’ token
Euler.cpp: In function ‘int main()’:
Euler.cpp:13: error: ‘cin’ was not declared in this scope
Euler.cpp:19: error: ‘cout’ was not declared in this scope
Euler.cpp:19: error: ‘endl’ was not declared in this scope

I usually never have problems with simple c++ programs and fear it's something very obvious.

//
//  Euler.cpp
//  Numerical Approximations (Euler's Method)
//
//  Created by XXXXXXXXXXXX on 6/18/12.
//  Copyright (c) 2012 University of Kansas Department of Mathematics. All rights     reserved.
//

#include <iostream>
using namespace std;

int main ()
{

    int N=4;
    //cout<<"Number of steps (N):";
    //cin>>t;

    float h=0.1;
    //cout<<endl<<" Step size (h):";
    cin>>h;

    float y0=1;
    //cout<<endl<<"y(0)=";
    //cin>>y0;

    cout<<"test!"<<endl;

    float data[N][4];

    int n=0;

    data[0][2] = y0;

    while (n<N){
        data[n][0]=n;
        if(n>0){
             data[n][2]=data[n-1][3];
        }
        data[n][1]=h*n;
        data[n][3] = data[n][2] + ((3 + data[n][1] - data[n][2])*h);
        n++;
        cout<<"n="<<n<<". tn="<<data[n][1]<<". y(n)="<<data[n][2]<<". y(n+1)="<<data[n][3] <<"."<<endl;
    }




 return 0;
 }

It's probably something obvious but I don't see it.

Jesse Beder
  • 33,081
  • 21
  • 109
  • 146

1 Answers1

2

It's not finding the iostream header. Do you see an error message reading something like "couldn't find header iostream" ?

chetan
  • 933
  • 4
  • 11
  • hmm... actually looks like this line is complaining about your #include directive: Euler.cpp:1: error: expected constructor, destructor, or type conversion before ‘<’ token Euler.cpp: In function ‘int main()’: This is very strange. The #include line looks perfectly well-formed to me. – chetan Jun 19 '12 at 00:00
  • no. Now nothing is happening. I type in g++ Euler.cpp it just goes to the next line, but no errors – Jackson Young Jun 19 '12 at 00:01
  • I cut/pasted your program and it compiled perfectly on my linux box. You're right it's a compiler setup problem on your machine but it will be hard to diagnose without actual access to your machine. – chetan Jun 19 '12 at 00:06
  • It even executes and prints out some numbers (which I didn't bother checking for correctness). But here they are for what it's worth (my input is 10 in the following paste):
    $ ./a.out 10 test! n=1. tn=4.57482e-41. y(n)=3.96915e+06. y(n+1)=4.57482e-41. n=2. tn=0. y(n)=8.10533e+06. y(n+1)=4.57482e-41. n=3. tn=0. y(n)=2.45631e+38. y(n+1)=4.59163e-41. n=4. tn=4.59163e-41. y(n)=5.88152e-39. y(n+1)=0.
    – chetan Jun 19 '12 at 00:09
  • I didn't comment out the cin< – Jackson Young Jun 19 '12 at 00:12
  • you mean you had cin>>h line commented out ? Could you figure out why G++ won't compile the program. Also, if your problem has been fixed, and my answer helped, you might want to accept the answer (mentioning this since you seem to be new to the site). – chetan Jun 19 '12 at 00:29