L
lixiaoyao
hey all
I wrote the following code which run well in g++, but when I try to
compile it in C++ builder, it report a bunch of problems, the most
frustrating one is
"[C++ Error] Unit1.cpp(1): E2282 Namespace name expected"
I attached my code, anyone can help me run this code smoothly? I really
appreciate it.
Thanks a lot
B
using namespace std;
#include <iostream>
#include <fstream>
#include <cmath>
int main(){
int Nstep=500;//must be even
int Nstepoutput=50;
ofstream input[Nstepoutput];
char buf[256];
for(int i=0;i<Nstepoutput;i++)
{
sprintf(buf,"output%d",i);
input.open(buf);
}
double delta_t=1.0/Nstep;
double delta_x=1.0/Nstep;
double vel[Nstep][Nstep];
double density[Nstep][Nstep];
double denvel[Nstep][Nstep];
double energy[Nstep][Nstep];
double x[Nstep];
double time[Nstep];
ofstream haha("hehe");
for(int i=1;i<Nstep+1;i++)
{
x[i-1]=double((i-1))/Nstep;
time[i-1]=double((i-1))/Nstep;
vel[0][i-1]=0; // intial velocity at time=0;
vel[i-1][0]=0;
vel[i-1][Nstep-1]=0;
density[i-1][0]=1;
density[i-1][Nstep-1]=0;
energy[i-1][0]=2.5;
energy[i-1][Nstep-1]=2;
denvel[0][i-1]=0;
denvel[i-1][0]=0;
denvel[i-1][Nstep-1]=0;
}
for(int i=1;i<Nstep/2+1;i++)
{
density[0][i-1]=1;
energy[0][i-1]=2.5;
input[0] << x[i-1] << '\t' << density[0][i-1] << '\t' <<
energy[0][i-1] << endl;
}
for(int i=Nstep/2+1;i<Nstep+1;i++)
{
density[0][i-1]=0.125;
energy[0][i-1]=2;
input[0] << x[i-1] << '\t' << density[0][i-1] << '\t' <<
energy[0][i-1] << endl;
}
for(int i=0;i<Nstep-2;i++){
for(int j=1;j<Nstep-2;j++)
{
density[i+1][j]=-(density[j+1]*vel[j+1]-density[j-1]*vel[j-1])/2.0/delta_x*delta_t+(density[j+1]+density[j-1])/2.0;
//lax method for first different equation
denvel[i+1][j]=-(denvel[j+1]*vel[j+1]+0.4*density[j+1]*energy[j+1]-denvel[j-1]*vel[j-1]-0.4*density[j-1]*energy[j-1])/2.0/delta_x*delta_t+0.5*(
denvel[j+1]+denvel[j-1]);
energy[i+1][j]=(-(vel[j+1]*(1.4*density[j+1]*energy[j+1]+0.5*density[j+1]*vel[j+1]*vel[j+1])-vel[j-1]*(1.4*density[j-1]*energy[j-1]+0.5*density[j-1]*vel[j-1]*vel[j-1]))/2.0/delta_x*delta_t+0.5*(density[j+1]*(energy[j+1]+0.5*vel[j+1]*vel[j+1])+density[j-1]*(energy[j-1]+0.5*vel[j-1]*vel[j-1])))/density[i+1][j]-0.5*vel[i+1][j]*vel[i+1][j];
// density are not zero at all,but how to protect program from
potential zero bug
}
cout << i << "\t" << i/5 << "\t" << i/5.0 << endl;
getchar();
if(i/5==i/5.0){
for(int jj=0;jj<Nstep;jj++){
input[i/5] <<'\t' << i/100.0 << "\t" << x[i-1] << '\t' <<
density[jj-1] << endl;
}
}
}
for (int i=0;i<Nstepoutput;i++){
input.close();
}
return 0;
}
I wrote the following code which run well in g++, but when I try to
compile it in C++ builder, it report a bunch of problems, the most
frustrating one is
"[C++ Error] Unit1.cpp(1): E2282 Namespace name expected"
I attached my code, anyone can help me run this code smoothly? I really
appreciate it.
Thanks a lot
B
using namespace std;
#include <iostream>
#include <fstream>
#include <cmath>
int main(){
int Nstep=500;//must be even
int Nstepoutput=50;
ofstream input[Nstepoutput];
char buf[256];
for(int i=0;i<Nstepoutput;i++)
{
sprintf(buf,"output%d",i);
input.open(buf);
}
double delta_t=1.0/Nstep;
double delta_x=1.0/Nstep;
double vel[Nstep][Nstep];
double density[Nstep][Nstep];
double denvel[Nstep][Nstep];
double energy[Nstep][Nstep];
double x[Nstep];
double time[Nstep];
ofstream haha("hehe");
for(int i=1;i<Nstep+1;i++)
{
x[i-1]=double((i-1))/Nstep;
time[i-1]=double((i-1))/Nstep;
vel[0][i-1]=0; // intial velocity at time=0;
vel[i-1][0]=0;
vel[i-1][Nstep-1]=0;
density[i-1][0]=1;
density[i-1][Nstep-1]=0;
energy[i-1][0]=2.5;
energy[i-1][Nstep-1]=2;
denvel[0][i-1]=0;
denvel[i-1][0]=0;
denvel[i-1][Nstep-1]=0;
}
for(int i=1;i<Nstep/2+1;i++)
{
density[0][i-1]=1;
energy[0][i-1]=2.5;
input[0] << x[i-1] << '\t' << density[0][i-1] << '\t' <<
energy[0][i-1] << endl;
}
for(int i=Nstep/2+1;i<Nstep+1;i++)
{
density[0][i-1]=0.125;
energy[0][i-1]=2;
input[0] << x[i-1] << '\t' << density[0][i-1] << '\t' <<
energy[0][i-1] << endl;
}
for(int i=0;i<Nstep-2;i++){
for(int j=1;j<Nstep-2;j++)
{
density[i+1][j]=-(density[j+1]*vel[j+1]-density[j-1]*vel[j-1])/2.0/delta_x*delta_t+(density[j+1]+density[j-1])/2.0;
//lax method for first different equation
denvel[i+1][j]=-(denvel[j+1]*vel[j+1]+0.4*density[j+1]*energy[j+1]-denvel[j-1]*vel[j-1]-0.4*density[j-1]*energy[j-1])/2.0/delta_x*delta_t+0.5*(
denvel[j+1]+denvel[j-1]);
energy[i+1][j]=(-(vel[j+1]*(1.4*density[j+1]*energy[j+1]+0.5*density[j+1]*vel[j+1]*vel[j+1])-vel[j-1]*(1.4*density[j-1]*energy[j-1]+0.5*density[j-1]*vel[j-1]*vel[j-1]))/2.0/delta_x*delta_t+0.5*(density[j+1]*(energy[j+1]+0.5*vel[j+1]*vel[j+1])+density[j-1]*(energy[j-1]+0.5*vel[j-1]*vel[j-1])))/density[i+1][j]-0.5*vel[i+1][j]*vel[i+1][j];
// density are not zero at all,but how to protect program from
potential zero bug
}
cout << i << "\t" << i/5 << "\t" << i/5.0 << endl;
getchar();
if(i/5==i/5.0){
for(int jj=0;jj<Nstep;jj++){
input[i/5] <<'\t' << i/100.0 << "\t" << x[i-1] << '\t' <<
density[jj-1] << endl;
}
}
}
for (int i=0;i<Nstepoutput;i++){
input.close();
}
return 0;
}