C++ HELP PLEASE!!!!!!!!!!!!!!!

Okay my code is below, and I was wondering how to let the user type in an output file name instead of it sending to one I made. In other words I created a file called output.txt, and I want to ask the user what there output file is called and then the program runs that one instead of mine. Also I would like to know how to display the total amount of data that has been written to the output file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
#define PI 3.14159
int main()
{
    int structure;
    cout << "Enter your 1 for Ibeam          ";
    cin >> structure;
    
    int num;
    double b1,b2,b3,d1,d2,d3,c2,I1,I2,I3,I,w,x,E,LO,theta,delta;
    
    string filename;
    char *outputfilename = "output.txt";
    
    ifstream inFile;
    ofstream outputfile(outputfilename);
    cout << "Please enter the name of the file you wish to open:  ";
    cin >> filename;
    inFile.open(filename.c_str());
    if (inFile.fail())
    {
       cout << "\nThe file named " << filename
            << " was not successfully opened"
            << "\n Please check that the file currently exists."
            <<endl;
       system("PAUSE");
       exit(1);
}
    cout << "\nThe file has been successfully opened for reading.\n";
    if (!outputfile){
    cout << "Error Opening Output File: " << outputfilename
           << " .\nCheck if file already exists. "<< endl;
    return 0 ;
}
    cout << "Output File Success fully Opened. " << endl;     
    
    inFile >> num;
    inFile >> b1;
    inFile >> b2;
    inFile >> b3;
    inFile >> d1;
    inFile >> d2;
    inFile >> d3;
    inFile >> w;
    inFile >> E;
    inFile >> LO;
    cout << "b1 from file:   " << b1<<endl;
    cout << "b2 from file:   " <<b2<<endl;
    cout <<"b3 from file:    " <<b3<<endl;
    cout <<"d1 from file:    " <<d1<<endl;
    cout <<"d2 from file:    " <<d2<<endl;
    cout <<"d3 from file:    " <<d3<<endl;
    cout <<"w from file:     " <<w<<endl;
    cout <<"E from file:     " <<E<<endl;
    cout <<"LO from file:    " <<LO<<endl;
    
    if (structure == 1)
    
    
    {
                  
    
    c2= (((b1 * d1) * (d1 / 2)) + ((b2 * d2) * (d1 + (d2/2))) + ((b3 * d3) * (d1 + d2 + (d3/2)))) / ((b1 * d1) + (b2 * d2) + (b3 * d3));
    cout << "c2:    " << c2 << "  m\n";    
    I1= ((b1 * (pow(d1,3))) / 12) + (b1 * d1) * (c2 - (d1 / 2)) * (c2 - (d1 / 2));
    cout << "I1:    " << I1 << "  m\n";
    I2= (((b2 * (pow(d2,3))) / 12) + ((b2 * d2) * ((d1 + (d2 / 2) - c2) * (d1 + (d2 / 2) - c2))));
    cout << "I2:    " << I2 << "  m\n";
    I3= ((b3 * (pow(d3,3))) / 12) + (b3 * d3) * (pow((d1 + d2 + (d3/2) - c2),2));
    cout << "I3:    " << I3 << "  m\n";
    cout << "Sum of I:   " << I1 + I2 + I3  << "    m\n"; 
    
    for (double x = 0.0; x <= 1; x +=0.1)
        
        
            {theta= ((((w * x) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * x) + (x * x)));
            cout << "theta:   " << theta << " m\n";
            delta= (((w * (x*x)) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(x))+((x)*(x))));
            cout << "delta:   " <<delta<< "  m\n";
            }
    
    outputfile <<"b1=        " <<b1<<endl;
    outputfile <<"b2=        " <<b2<<endl;
    outputfile <<"b3=        " <<b3<<endl;
    outputfile <<"d1=        " <<d1<<endl;
    outputfile <<"d2=        " <<d2<<endl;
    outputfile <<"d3=        " <<d3<<endl;
    outputfile <<"c2=        " <<c2<<endl;
    outputfile <<"I1=        " <<I1<<endl;
    outputfile <<"I2=        " <<I2<<endl;
    outputfile <<"I3=        " <<I3<<endl;
    outputfile<<"Sum of I=       " <<(I1+I2+I3)<<endl;
    outputfile <<"w=         "<<w<<endl;
    outputfile <<"E=         "<<E<<endl;
    outputfile <<"LO=        "<<LO<<endl;
    outputfile << "    Distance x(m)                      Deflection delta(m)                    Rotation theta   "<<endl;
    outputfile <<"\n      " <<(.1*LO);                       outputfile <<"                                 "<<((((w * ((.1*LO)*(.1*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(.1*LO))+((.1*LO)*(.1*LO)))));               outputfile<<"                             "<<(((((w * (.1*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (.1*LO)) + ((.1*LO) * (.1*LO)))))<<endl;
    outputfile <<"\n      " <<(.2*LO);                       outputfile <<"                                 "<<((((w * ((.2*LO)*(.2*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(.2*LO))+((.2*LO)*(.2*LO)))));               outputfile<<"                             "<<(((((w * (.2*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (.2*LO)) + ((.2*LO) * (.2*LO)))))<<endl;
    outputfile <<"\n      " <<(.3*LO);                       outputfile <<"                                 "<<((((w * ((.3*LO)*(.3*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(.3*LO))+((.3*LO)*(.3*LO)))));               outputfile<<"                             "<<(((((w * (.3*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (.3*LO)) + ((.3*LO) * (.3*LO)))))<<endl;
    outputfile <<"\n      " <<(.4*LO);                       outputfile <<"                                 "<<((((w * ((.4*LO)*(.4*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(.4*LO))+((.4*LO)*(.4*LO)))));               outputfile<<"                             "<<(((((w * (.4*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (.4*LO)) + ((.4*LO) * (.4*LO)))))<<endl;
    outputfile <<"\n      " <<(.5*LO);                       outputfile <<"                                 "<<((((w * ((.5*LO)*(.5*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(.5*LO))+((.5*LO)*(.5*LO)))));               outputfile<<"                             "<<(((((w * (.5*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (.5*LO)) + ((.5*LO) * (.5*LO)))))<<endl;
    outputfile <<"\n      " <<(.6*LO);                       outputfile <<"                                 "<<((((w * ((.6*LO)*(.6*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(.6*LO))+((.6*LO)*(.6*LO)))));               outputfile<<"                             "<<(((((w * (.6*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (.6*LO)) + ((.6*LO) * (.6*LO)))))<<endl;
    outputfile <<"\n      " <<(.7*LO);                       outputfile <<"                                 "<<((((w * ((.7*LO)*(.7*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(.7*LO))+((.7*LO)*(.7*LO)))));               outputfile<<"                             "<<(((((w * (.7*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (.7*LO)) + ((.7*LO) * (.7*LO)))))<<endl;
    outputfile <<"\n      " <<(.8*LO);                       outputfile <<"                                 "<<((((w * ((.8*LO)*(.8*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(.8*LO))+((.8*LO)*(.8*LO)))));               outputfile<<"                             "<<(((((w * (.8*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (.8*LO)) + ((.8*LO) * (.8*LO)))))<<endl;
    outputfile <<"\n      " <<(.9*LO);                       outputfile <<"                                 "<<((((w * ((.9*LO)*(.9*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(.9*LO))+((.9*LO)*(.9*LO)))));               outputfile<<"                             "<<(((((w * (.9*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (.9*LO)) + ((.9*LO) * (.9*LO)))))<<endl;
    outputfile <<"\n      " <<(1*LO);                        outputfile <<"                                 "<<((((w * ((1*LO)*(1*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(1*LO))+((1*LO)*(1*LO)))));                    outputfile<<"                             "<<(((((w * (1*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (1*LO)) + ((1*LO) * (1*LO)))))<<endl;
              
    
    
    
    
    
    system("PAUSE");
    return 0;
}
    else if (structure>1 || structure<1)
    {
        cout <<"You did not type a valid number." <<endl;
        system("PAUSE");
        return 0;
    }
    system ("PAUSE");
    return 0;
}



Thanks
Last edited on
Okay my code is below, and I was wondering how to let the user type in an output file name instead of it sending to one I made.


You've already done this for the input file, add the appropriate instructions for this logic and apply it to your output file.
Okay I did that, and now its saying my output file doesn't exist when I run the program. I already made sure that it was in the same location as the source file. I also made sure the file was typed in correctly.
You need to find where the executable is not the source file
This is the first part of my code, and my second will be next
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
#define PI 3.14159
int main()
{
   
    int structure;
    cout << "Enter your 1 for Ibeam          ";
    cin >> structure;
    
    int num;
    double b1,b2,b3,d1,d2,d3,c2,I1,I2,I3,I,w,x,E,LO,theta,delta;
    
    string filename;
    char *outputfilename = "output.txt";
    
    ifstream inFile;
    cout << "Please enter the name of the file you wish to open:  ";
    cin >> filename;
    inFile.open(filename.c_str());
    if (inFile.fail())
    {
       cout << "\nThe file named " << filename
            << " was not successfully opened"
            << "\n Please check that the file currently exists."
            <<endl;
       system("PAUSE");
       return 0;
}
    
    if (!inFile){
    cout << "Error Opening Output File: " << outputfilename
           << " .\nCheck if file already exists. "<< endl;
    return 0 ;
}
    ofstream outputfile(outputfilename);
    cout << "Please enter the name of the file you wish to open:  ";
    cin >> filename;
    outputfile.open(filename.c_str());
    if (outputfile.fail())
    {
       cout << "\nThe file named " << filename
            << " was not successfully opened"
            << "\n Please check that the file currently exists."
            <<endl;
       system("PAUSE");
       return 0;
}
    
    if (!outputfile){
    cout << "Error Opening Output File: " << outputfilename
           << " .\nCheck if file already exists. "<< endl;
    return 0 ;
}    
    
    inFile >> num;
    inFile >> b1;
    inFile >> b2;
    inFile >> b3;
    inFile >> d1;
    inFile >> d2;
    inFile >> d3;
    inFile >> w;
    inFile >> E;
    inFile >> LO;
    cout << "b1 from file:   " << b1<<endl;
    cout << "b2 from file:   " <<b2<<endl;
    cout <<"b3 from file:    " <<b3<<endl;
    cout <<"d1 from file:    " <<d1<<endl;
    cout <<"d2 from file:    " <<d2<<endl;
    cout <<"d3 from file:    " <<d3<<endl;
    cout <<"w from file:     " <<w<<endl;
    cout <<"E from file:     " <<E<<endl;
    cout <<"LO from file:    " <<LO<<endl;
    
    if (structure == 1)
    
    
    {
                  
    
    c2= (((b1 * d1) * (d1 / 2)) + ((b2 * d2) * (d1 + (d2/2))) + ((b3 * d3) * (d1 + d2 + (d3/2)))) / ((b1 * d1) + (b2 * d2) + (b3 * d3));
    cout << "c2:    " << c2 << "  m\n";    
    I1= ((b1 * (pow(d1,3))) / 12) + (b1 * d1) * (c2 - (d1 / 2)) * (c2 - (d1 / 2));
    cout << "I1:    " << I1 << "  m\n";
    I2= (((b2 * (pow(d2,3))) / 12) + ((b2 * d2) * ((d1 + (d2 / 2) - c2) * (d1 + (d2 / 2) - c2))));
    cout << "I2:    " << I2 << "  m\n";
    I3= ((b3 * (pow(d3,3))) / 12) + (b3 * d3) * (pow((d1 + d2 + (d3/2) - c2),2));
    cout << "I3:    " << I3 << "  m\n";
    cout << "Sum of I:   " << I1 + I2 + I3  << "    m\n"; 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
for (double x = 0.0; x <= 1; x +=0.1)
        
        
            {theta= ((((w * x) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * x) + (x * x)));
            cout << "theta:   " << theta << " m\n";
            delta= (((w * (x*x)) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(x))+((x)*(x))));
            cout << "delta:   " <<delta<< "  m\n";
            }
    
    outputfile <<"b1=        " <<b1<<endl;
    outputfile <<"b2=        " <<b2<<endl;
    outputfile <<"b3=        " <<b3<<endl;
    outputfile <<"d1=        " <<d1<<endl;
    outputfile <<"d2=        " <<d2<<endl;
    outputfile <<"d3=        " <<d3<<endl;
    outputfile <<"c2=        " <<c2<<endl;
    outputfile <<"I1=        " <<I1<<endl;
    outputfile <<"I2=        " <<I2<<endl;
    outputfile <<"I3=        " <<I3<<endl;
    outputfile<<"Sum of I=       " <<(I1+I2+I3)<<endl;
    outputfile <<"w=         "<<w<<endl;
    outputfile <<"E=         "<<E<<endl;
    outputfile <<"LO=        "<<LO<<endl;
    outputfile << "    Distance x(m)                      Deflection delta(m)                    Rotation theta   "<<endl;
    outputfile <<"\n      " <<(.1*LO);                       outputfile <<"                                 "<<((((w * ((.1*LO)*(.1*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(.1*LO))+((.1*LO)*(.1*LO)))));               outputfile<<"                             "<<(((((w * (.1*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (.1*LO)) + ((.1*LO) * (.1*LO)))))<<endl;
    outputfile <<"\n      " <<(.2*LO);                       outputfile <<"                                 "<<((((w * ((.2*LO)*(.2*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(.2*LO))+((.2*LO)*(.2*LO)))));               outputfile<<"                             "<<(((((w * (.2*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (.2*LO)) + ((.2*LO) * (.2*LO)))))<<endl;
    outputfile <<"\n      " <<(.3*LO);                       outputfile <<"                                 "<<((((w * ((.3*LO)*(.3*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(.3*LO))+((.3*LO)*(.3*LO)))));               outputfile<<"                             "<<(((((w * (.3*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (.3*LO)) + ((.3*LO) * (.3*LO)))))<<endl;
    outputfile <<"\n      " <<(.4*LO);                       outputfile <<"                                 "<<((((w * ((.4*LO)*(.4*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(.4*LO))+((.4*LO)*(.4*LO)))));               outputfile<<"                             "<<(((((w * (.4*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (.4*LO)) + ((.4*LO) * (.4*LO)))))<<endl;
    outputfile <<"\n      " <<(.5*LO);                       outputfile <<"                                 "<<((((w * ((.5*LO)*(.5*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(.5*LO))+((.5*LO)*(.5*LO)))));               outputfile<<"                             "<<(((((w * (.5*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (.5*LO)) + ((.5*LO) * (.5*LO)))))<<endl;
    outputfile <<"\n      " <<(.6*LO);                       outputfile <<"                                 "<<((((w * ((.6*LO)*(.6*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(.6*LO))+((.6*LO)*(.6*LO)))));               outputfile<<"                             "<<(((((w * (.6*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (.6*LO)) + ((.6*LO) * (.6*LO)))))<<endl;
    outputfile <<"\n      " <<(.7*LO);                       outputfile <<"                                 "<<((((w * ((.7*LO)*(.7*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(.7*LO))+((.7*LO)*(.7*LO)))));               outputfile<<"                             "<<(((((w * (.7*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (.7*LO)) + ((.7*LO) * (.7*LO)))))<<endl;
    outputfile <<"\n      " <<(.8*LO);                       outputfile <<"                                 "<<((((w * ((.8*LO)*(.8*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(.8*LO))+((.8*LO)*(.8*LO)))));               outputfile<<"                             "<<(((((w * (.8*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (.8*LO)) + ((.8*LO) * (.8*LO)))))<<endl;
    outputfile <<"\n      " <<(.9*LO);                       outputfile <<"                                 "<<((((w * ((.9*LO)*(.9*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(.9*LO))+((.9*LO)*(.9*LO)))));               outputfile<<"                             "<<(((((w * (.9*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (.9*LO)) + ((.9*LO) * (.9*LO)))))<<endl;
    outputfile <<"\n      " <<(1*LO);                        outputfile <<"                                 "<<((((w * ((1*LO)*(1*LO))) / (24 * E * (I1 +I2+I3)))) *(((6*(LO * LO))-(4*LO*(1*LO))+((1*LO)*(1*LO)))));                    outputfile<<"                             "<<(((((w * (1*LO)) / (6 * E * (I1+I2+I3)))) * ((3 * (LO * LO)) - (3 * LO * (1*LO)) + ((1*LO) * (1*LO)))))<<endl;
              
    
    
    
    
    
    system("PAUSE");
    return 0;
}
    else if (structure>1 || structure<1)
    {
        cout <<"You did not type a valid number." <<endl;
        system("PAUSE");
        return 0;
    }
    system ("PAUSE");
    return 0;
}
You need to find where the executable is not the source file


What do you mean by this?
Depending on your OS, the .exe file - The thing you click to start your program. It should be in the same directory
Okay, but if that was the case wouldn't my input file not open either? I have them all in the same place. But my input file opens fine.
Last edited on
Does no one know how to ask the user for an output file, and then it bring it in the program without any trouble?
Topic archived. No new replies allowed.