0
votes

I'm translating a Fortran 77 code to C++ and the Fortran 77 uses common blocks. I am trying to replace the common blocks with structs which I will then fill with values from a function and then call both to be used in my main. At the moment my code looks like:

#include <iostream> 
#include <cmath>
using namespace std;

// data_list 
struct data_list {
    double g, dw, Vel, M, dt, N;
    int Ioutp1, Ioutp2; 
    } values;

void data (data_list& val) {
    val.g = 9.80665;
    val.dw = 0.05; 
    val.Vel = 20.0;
    val.M = 128; 
    val.dt = 0.05; 
    val.N = 4000;
    val.Ioutp1 = 1;
    val.Ioutp2 = 1;
    }   

void Pierson_Moskowitz(data_list& val) {

/*
* Calculation of properties of Pierson_Moskowitz Spectrum
*/
    double Ug, Hs, A, B, Std;
    cout << values.Vel << "\t\t" << values.g; 
    Ug = values.Vel/values.g;
    cout << Ug << endl;

}   
int main() {
  data(values);
  //float dw = values.dw = 0.05;
  cout << values.dw << endl; 

}

This is just a test as at the moment my main isn't doing anything except printing a value. What I want is for the variables that are given values in my data function to be able to be used throughout the code. At the moment, there are two things I'm confused about:

1) My Pierson-Moskowitz function isn't printing anything for Ug. I don't understand why not? 2) I'm not sure that I even need the data function. Essentially the Fortran code that I'm translating uses a subroutine to assign values to variables in a common block. I am trying to do something similar by using a struct and then a function to fill it with values.

This code is quite short and I plan to keep everything in one file. Any help is really appreciated!!

1
You're passing your data_list by value. None of the changes are making it into the original values. - dlf
C++ is pass by value, so the data function has its own copy of the vlaues object. - juanchopanza
Ok but how would I fix this? I want to be able to access the values given in my void data function.. what's the correct way to do this? - user3460758
@user3460758 "Ok but how would I fix this?" void data (data_list& val) note the & - πάντα ῥεῖ
Always use tag fortran and only add the version when necessary to distinguish that your question is specific. For example that you cannot use Fortran 2008 but only Fortran 90. - Vladimir F

1 Answers

4
votes

You want to pass in a reference to your common block:

void data (data_list& val) {

then you're changes will be to the common block rather than a copy.