简体   繁体   中英

Creating c++ code in visual studio and running it in g++

I have created c++ code in visual studio but the university specification is that it needs to run in g++ compiler.

The code works perfectly in visual studio but does not work at all in g++. Why is this? Is there an easy way to make the code work in g++?

This is the code I have.

#include <iostream> 
#include <vector> 
#include <cmath>
#include <iomanip>
#include <fstream> 

using namespace std;

int main ()
{
    vector<long double> G;
    vector<long double> R;
    int n=1;
    long double result =1;

    R.assign(2,0);
    G.assign(2,123);
    G.at(1)=321;

    while (result >= 0.0000000000000001)
    {

    n++;    
    G.push_back((G.at(n-2) - G.at(n-1)));
    R.push_back(G.at(n)/G.at(n-1));

    result = abs(R.at(n) - R.at(n-1));

    ofstream file;
    file.open("series.txt", std ::ios_base ::app);
    file << n <<"\t" << setw(14) << G.at(n)<<"\t<<setprecision(15)<<R.at(n)<<endl;
    //cout << n <<"\t" <<setw(14)<<G.at(n)<<"\t"<<setprecision(15)<<R.at(n)<<endl;
    file.close();
    }

    system("pause");
    }

There is no issue with cross compiling in my opinion, although to the best of my knowledge there is no system("pause") on linux systems.

The error is missing terminating character. Which is due to miss quotation marks at the second \\t

This

file << n <<"\t" << setw(14) << G.at(n)<<"\t<<setprecision(15)<<R.at(n)<<endl;

should be this

file << n <<"\t" << setw(14) << G.at(n)<<"\t"<<setprecision(15)<<R.at(n)<<endl;

for use of system() function you must add #include <stdlib.h> to your project and change

file << n <<"\t" << setw(14) << G.at(n)<<"\t<<setprecision(15)<<R.at(n)<<endl;

to

file << n <<"\t" << setw(14) << G.at(n)<<"\t"<<setprecision(15)<<R.at(n)<<endl;

then it compile 100 percentage with g++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM