So in my program I have to use specific values obtained from an inputfile in different functions, and I couldn't figure out a good way to do this. I don't want to have to have the user input the file name for every function. I tried it this way, but it doesn't work. Could this possibly work, or is there a better way?

#include <iostream>
#include <cmath>
#include <fstream>

using namespace std;
double deflectioni (double, double, double, double, double, double, char);
double deflectionc (double, double, char);
double slopei (double, double, double, double, double, double, char);
double slopec (double, double, char);


int main()
{
    cout<<"Program to calculate deflection and rotation at incremental points" <<endl;
    cout<<"along the length of a beam" <<endl;
    cout<<"Kelli Harding - September 29, 2012" <<endl;
    cout<<endl;
    char inputfilename[50];
    char outputfilename[50];
    double span, load, modulus, deltax, b1, b2, b3, d1, d2, d3;
    double def, x, slope, dinner, douter;
    string type;
    ofstream outputfile;
    ifstream inputfile;
    cout <<"Enter the name of the file you wish to open." <<endl;
    cin >> inputfilename;
    inputfile.open(inputfilename);
        if (!inputfile.eof())
        { 
         inputfile >>deltax;
         inputfile >>type;
         }
    cout <<"Enter the name of the file you wish to write to." <<endl;
    cin >> outputfilename;
    outputfile.open(outputfilename);
    if (!outputfile)
    {
    cout <<"Error opending output file: " <<outputfilename <<endl;
    cout <<"Check if file already exists." <<endl;
    return 0;
    }
    cout <<"Output file successfully opened." <<endl;  
    outputfile <<"Distance x (m) \t";
    outputfile <<"Deflection (m) \t";
    outputfile <<"Rotation" <<endl; 

    if (type=="I-section")
    {
    inputfile >>b1;
    inputfile >>b2;
    inputfile >>b3;
    inputfile >>d1;
    inputfile >>d2;
    inputfile >>d3;
    inputfile >>type; 
    int i=1;
    do
    {
    x=deltax*i++; 
    outputfile<<x;
    outputfile<<"\t \t";
    def=deflectioni (b1, b2, b3, d1, d2, d3, inputfilename);
    outputfile<<def;
    outputfile<<"\t";
    slope=slopei (b1, b2, b3, d1, d2, d3,inputfilename);
    outputfile<<slope <<endl; 
    } while (x<=span-deltax);
    } 
    else if (type=="Circular")
    {
    inputfile >>dinner;
    inputfile >>douter;
    int i=1;
    do
    {
    x=deltax*i++; 
    outputfile<<x;
    outputfile<<"\t \t";
    def=deflectionc(dinner, douter, inputfilename);
    outputfile<<def;
    outputfile<<"\t";
    slope=slopec(dinner, douter, inputfilename);
    outputfile<<slope <<endl; 
    } while (x<=span-deltax);
    } 

    inputfile.close();
    outputfile.close();

    system ("PAUSE");
    return 0;
}    

    double deflectioni (double b1, double b2, double b3, double d1, double d2, double d3, char inputfilename) {
        double c1, c2, moi, I1, I2, I3, def;
        c2=((b1*d1)*(d1/2)+(b2*d2*(d1+(d2/2)))+(b3*d3*(d1+d2+(d3/2))))/((b1*d1)+(b2*d2)+(b3*d3));
        c1=(d1+d2+d3)-c2;
        I1=(pow(d1,3)*b1)/12+(pow(c2-(d1/2),2)*b1*d1);
        I2=(pow(d2,3)*b2)/12+(pow((d1+(d2/2)-c2),2)*b2*d2);
        I3=(pow(d3,3)*b3)/12+(pow((d1+d2+(d3/2)-c2),2)*b3*d3);
        moi=I1+I2+I3; 
    double span, load, modulus, deltax;
    double x, slope;
    ifstream inputfile;
    inputfile.open(inputfilename);
        if (!inputfile.eof())
        { 
         inputfile >>span;
         inputfile >>load;
         inputfile >>modulus;
         }
        def=((load*pow(x,2))/(24*modulus*moi))*(6*pow(span,2)-(4*span*x)+pow(x,2));
        return (def); }

    double deflectionc (double dinner, double douter, char inputfilename) {
        double moi, def, x;
        moi=(3.1416/64)*(pow(douter,4)-(pow(dinner,4)));
        double span, load, modulus, deltax;
        ifstream inputfile;
        inputfile.open(inputfilename);
        if (!inputfile.eof())
        { 
         inputfile >>span;
         inputfile >>load;
         inputfile >>modulus;
         }
        def=((load*pow(x,2))/(24*modulus*moi))*(6*pow(span,2)-(4*span*x)+pow(x,2));
        return (def); }

    double slopei (double b1, double b2, double b3, double d1, double d2, double d3, char inputfilename) {
        double c1, c2, moi, I1, I2, I3, slope;
        c2=((b1*d1)*(d1/2)+(b2*d2*(d1+(d2/2)))+(b3*d3*(d1+d2+(d3/2))))/((b1*d1)+(b2*d2)+(b3*d3));
        c1=(d1+d2+d3)-c2;
        I1=(pow(d1,3)*b1)/12+(pow(c2-(d1/2),2)*b1*d1);
        I2=(pow(d2,3)*b2)/12+(pow((d1+(d2/2)-c2),2)*b2*d2);
        I3=(pow(d3,3)*b3)/12+(pow((d1+d2+(d3/2)-c2),2)*b3*d3);
        moi=I1+I2+I3; 
    double span, load, modulus, deltax, x;
    ifstream inputfile;
    inputfile.open(inputfilename);
        if (!inputfile.eof())
        { 
         inputfile >>span;
         inputfile >>load;
         inputfile >>modulus;
         }
        slope=((load*x)/(6*modulus*moi))*(3*pow(span,2)-(3*span*x)+pow(x,2));
        return (slope); }

    double slopec (double dinner, double douter, char inputfilename) {
        double moi, slope;
        moi=(3.1416/64)*(pow(douter,4)-(pow(dinner,4)));
    double span, load, modulus, deltax, x;
    ifstream inputfile;
    inputfile.open(inputfilename);
        if (!inputfile.eof())
        { 
         inputfile >>span;
         inputfile >>load;
         inputfile >>modulus;
         }
        slope=((load*x)/(6*modulus*moi))*(3*pow(span,2)-(3*span*x)+pow(x,2));
        return (slope); }

Recommended Answers

All 6 Replies

use specific values obtained from an inputfile in different functions

Could you explain what do you mean by this ?

All your function declarations have the file path variable as a char argument.
for example :

double slopec (double dinner, double douter, char inputfilename)

But the char variable holds only one character. and you declare inputfilename as:
char inputfilename[50];
which is a 50 character array.

So first of all read this first and try to replace char arguments with either char arrays or pointers.

http://www.cs.bu.edu/teaching/c/string/intro/

No, it won't work that way.

Since you open the file(s) in main(), you have the file pointers in the fstream values. These are the values you pass to the functions. That gives the functions access to the files.

Could you dumb it down a little WaltP? I'm new to C++ and don't really know much about it, if you can't tell.

You pass the ifstream and ofstream variables the same way you do the int values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.