Multiple problems...

For some reason, there are multiple problems with my program, that is supposed to print data from a physics project (both onscreen, and to a text file). I encounter multiple errors. The first one (which I think is inconsequential) is the fact that it always seems to skip a lab partner. (I take advantage of this by adding 1 to the number of lab partners.) The second error is that it only prints the names of the lab partners to the text file, and not the data of the equipment! Here is my code:
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
#include <string>
#include <fstream>
#include <new>
#include <time.h>
#include <sstream>
//#include <stdio.h>

using namespace std;

class intro {
    private:
        string yourname;
        int pnum;
        string * people;
        string * rows;
        string * getrows;
        int a;
        string temp;
        string numstr;
        long pos;
    public:
        intro(int);
        long names() {
            //This function will input the names of the lab participants and the date into a .txt file.
            //It will do this by first asking for YOUR name, and then asking for the number of lab partners.
            //We must open a .txt file for output as we want the names, which are in the dynamic array, to be written to it.
            ofstream myfile ("physicslab.txt");     //This should probably be done in the main function.
            myfile<<'\t'<<"Physics 15200: Torque Lab Report"<<endl;     //Printing the title...
            cout<<"Please enter your first and last name: ";
            getline(cin, yourname);         //used to ensure the program gets an entire line (the full input)
            cout<<"How many people are in your group? ";
            cin>>pnum;
            pnum++;
            people = new(nothrow) string[pnum];
            if (people==0)  //if our request for dynamic memory was denied...
            {
                cout<<"An error occured.";
            }   //end if
            else            //Otherwise...
            {
                //Everyone gets to enter their names...
                if (pnum==1)        //If there is only one lab partner, this statement is executed.
                {
                    cout<<"Enter the lab partner's name: ";
                    getline(cin, people[0]);
                }   //end if
                else {              //Otherwise, we need a for loop to account for everyone...
                    for (int turn=0;turn<pnum; turn++)      //This for loop is faulty for some reason, as it always skips
                    //the first person...
                    {
                        cout<<"Person number "<<turn<<" enter your name: "<<endl;
                        getline(cin,temp);
                        people[turn] = temp;
                    }   //end for
                }   //end else
                //These names will be all printed...
                cout<<"Your name is: "<<yourname<<" and your partners' names are:";   //onscreen for verification purposes...
                myfile<<"Name: "<<yourname<<endl;
                myfile<<"Partners' names:";
                for (int i = 0; i<pnum; i++)    //This loop crashed the program, but the problem has been fixed.
                //The loop will print the array entries onscreen, and in the file.
                {
                    cout<<people[i]<<endl;
                    myfile<<people[i];
                    //Controlling the times when the ", " is printed...
                    if (i == 0|| i == pnum-1)
                    {
                        myfile<<' ';
                    }   //end if
                    else
                    {
                        myfile<<", ";
                    }
                }   //end for
            delete[] people;    //the dynamic memory will be turned off to free it
            myfile<<endl;
            myfile<<' ';
            }   //end else
            pos = myfile.tellp();   //This will return the position of the last character in the file.
            myfile.close();     //We are forced to close the file.
            return pos;
        }   //end names

        void equip() {
            //In this function, the actual input for the experiment starts.
            //First, we must get all the lines and reprint them (again).
            ofstream myfile;
            //myfile.open();
            //names()pos = myfile.tellp();
            myfile.seekp(pos);
            cout<<pos;

            //Get ready to measure and input.
            float rcrossbar, dshaft, rshaft, mass;
            cout<<"Please enter the radius of the crossbar (in meters): ";
            getline(cin, numstr);           //All input will be handled using the stringstream method.
            stringstream(numstr)>>rcrossbar;
            cout<<"Now enter the diameter of the vertical shaft (in meters): ";
            getline(cin, numstr);
            stringstream(numstr)>>dshaft;
            cout<<"Enter the mass of the hanger (in kilograms): ";
            getline(cin, numstr);
            stringstream(numstr)>>mass;
            rshaft = dshaft/2;  //Duh!
            //Writing all this data to the text file...
            myfile<<"Radius"<<'\t'<<'\t'<<'|'<<rcrossbar<<" m "<<endl;
            myfile<<"Diameter of the shaftbar"<<'|'<<dshaft<<" m "<<endl;
            myfile<<"Radius of the shaftbar"<<"  |"<<rshaft<<" m "<<endl;
            myfile<<"Hanging Mass"<<'\t'<<'|'<<mass<<" kg "<<endl;
            //And testing it by showing it onscreen:
            cout<<"rcrossbar="<<rcrossbar<<endl;
            cout<<"dshaft="<<dshaft<<endl;
            cout<<"rshaft="<<rshaft<<endl;
            cout<<"mass="<<mass<<endl;
        }   //end equip
};   //end intro

intro::intro(int x) {
    a = x;
}   //end constructor

class tdata {
    private:
        int b;
        float elapse[12],height[12],acceleration[12];
        float m;
        string mystr;
    public:
        tdata(int);
        tablea(b)  //This parametric function will take on three arrays of data
        //at once and be called four times. Each time it is called, a new table is constructed...
        {
            m = (intro::mass)*b+0.2;  //Let's start by defining the mass for the time it is called...
            for (int counter = 0; counter <3; counter++)    //Now, for the rows of data, we can let a for loop handle that.
            {
                cout<<"Enter the drop time: ";
                getline(cin, mystr);
                stringstream(mystr)>>elapse[3*b+counter];
                cout<<"Enter the height: ";
                stringstream(mystr)>>height[3*b+counter];
                acceleration[3*b+counter] = (2*height)/(elapse^2);  //formula for acceleration
            }   //end for

        }   //end parametric function

        //We want a function that will print the tables.
        //A different table will be printed out for each b value. The rows will, of course be printed one at a time.
        //This means that each element in the arrays will be handled one at a time.
};   //end tdata

tdata::tdata(int tnum){
    int b = tnum;
}   //end constructor

int main ()
{

    intro i(3);
    //tdata t;
    //i.intro(3);
    i.names();
    i.equip();
    /*for (int n = 0; n<4;n++)
    {
        tdata t(n);
    }   //end for
*/

    return 0;
}   //end main 


Your data is supposed to be written by the function equip. This function creates an ofstream named myfile, but never actually assigns that ofstream to an actual file. Your function names does it like this: ofstream myfile ("physicslab.txt"); Your function equip needs to do something like that.
Last edited on
So, generally, if I am to write stuff to a text file, it has to be done by one function?
I'm surprised you could get it compiled at all. I'd say take it bit by bit and make sure each and every part does what you think it does. I found one area with a lot of issues:

1
2
3
4
5
6
7
8
cout<<"Enter the drop time: ";
getline(cin, mystr);
stringstream(mystr)>>elapse[3*b+counter];
cout<<"Enter the height: ";
stringstream(mystr)>>height[3*b+counter];
// the following line has some issues, I cleaned it up a bit. '^' is not an operator you can use for power.
//acceleration[3*b+counter] = (2*height)/(elapse^2);  //formula for acceleration
acceleration[3*b+counter] = (2*height[3*b+counter])/(elapse[3*b+counter]*elapse[3*b+counter]);  //formula for acceleration 


Also, I had to comment out the mass calculations to get it to compile, I'm not completely sure what you're trying to do there.
I am getting a message like:"Physics Program - Debug" uses an invalid compiler. Probably the toolchain path within the compiler options is not setup correctly?! Skipping...". As for the mass calculations, I was trying to get data for a new table upon the function being called again. The new tables would be based on a new mass.
Last edited on
So, generally, if I am to write stuff to a text file, it has to be done by one function?


No. Not at all. If you want to write stuff to a file, when you create the ofstream object, you also have to tell it the name of the file.

ofstream myfile; HERE - it needs to know the name of the file.
Last edited on
I attempted to resolve the mass problem by using inheritance. However, it is giving me a new set of errors:
1
2
3
4
5
6
7
8
9
10
11
22|error: 'b' is not a type;22|error: ISO C++ forbids declaration of 'tablea' with no type;
In member function 'int tdata::tablea(int)':
36|warning: no return statement in function returning non-void;
In member function 'void tdata::convertmass(intro)':
47|error: 'in' has incomplete type;
11|error: forward declaration of 'struct intro';
In function 'int main()':
174|error: no matching function for call to 'tdata::tdata()';
43|note: candidates are:tdata::tdata(int);
13|note:tdata::tdata(const tdata&)error: expected primary-expression before '[' token|
||=== Build finished: 6 errors, 1 warnings ===|

Here is my revised code:
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <iostream>
#include <string>
#include <fstream>
#include <new>
#include <time.h>
#include <sstream>
//#include <stdio.h>

using namespace std;

class intro;   //My attempt to use inheritance (friend classes)...

class tdata {
    private:
        int b;
        float elapse[12],height[12],acceleration[12];
        float m;
        string mystr;
    public:
        tdata(int);
        void convertmass (intro a);
        tablea(b)  //This parametric function will take on three arrays of data
        //at once and be called four times. Each time it is called, a new table is constructed...
        {

            for (int counter = 0; counter <3; counter++)    //Now, for the rows of data, we can let a for loop handle that.
            {
                cout<<"Enter the drop time: ";
                getline(cin, mystr);
                stringstream(mystr)>>elapse[3*b+counter];
                cout<<"Enter the height: ";
                stringstream(mystr)>>height[3*b+counter];
                acceleration[3*b+counter] = (2*height[3*b+counter])/(elapse[3*b+counter]*elapse[3*b+counter]);  //formula for acceleration
            }   //end for

        }   //end parametric function

        //We want a function that will print the tables.
        //A different table will be printed out for each b value. The rows will, of course be printed one at a time.
        //This means that each element in the arrays will be handled one at a time.
};   //end tdata

tdata::tdata(int tnum){
    b = tnum;
}   //end constructor

void tdata::convertmass(intro in) {
    m = b*(in.mass)+ 0.2;
}   //end friend operation

class intro {
    private:
        string yourname;
        int pnum;
        string * people;
        string * rows;
        string * getrows;
        int a;
        string temp;
        string numstr;
        long pos;
        float rcrossbar, dshaft, rshaft, mass;
    public:
        intro(int);
        long names() {
            //This function will input the names of the lab participants and the date into a .txt file.
            //It will do this by first asking for YOUR name, and then asking for the number of lab partners.
            //We must open a .txt file for output as we want the names, which are in the dynamic array, to be written to it.
            ofstream myfile ("physicslab.txt");     //This should probably be done in the main function.
            myfile<<'\t'<<"Physics 15200: Torque Lab Report"<<endl;     //Printing the title...
            cout<<"Please enter your first and last name: ";
            getline(cin, yourname);         //used to ensure the program gets an entire line (the full input)
            cout<<"How many people are in your group? ";
            cin>>pnum;
            pnum++;
            people = new(nothrow) string[pnum];
            if (people==0)  //if our request for dynamic memory was denied...
            {
                cout<<"An error occured.";
            }   //end if
            else            //Otherwise...
            {
                //Everyone gets to enter their names...
                if (pnum==1)        //If there is only one lab partner, this statement is executed.
                {
                    cout<<"Enter the lab partner's name: ";
                    getline(cin, people[0]);
                }   //end if
                else {              //Otherwise, we need a for loop to account for everyone...
                    for (int turn=0;turn<pnum; turn++)      //This for loop is faulty for some reason, as it always skips
                    //the first person...
                    {
                        cout<<"Person number "<<turn<<" enter your name: "<<endl;
                        getline(cin,temp);
                        people[turn] = temp;
                    }   //end for
                }   //end else
                //These names will be all printed...
                cout<<"Your name is: "<<yourname<<" and your partners' names are:";   //onscreen for verification purposes...
                myfile<<"Name: "<<yourname<<endl;
                myfile<<"Partners' names:";
                for (int i = 0; i<pnum; i++)    //This loop crashed the program, but the problem has been fixed.
                //The loop will print the array entries onscreen, and in the file.
                {
                    cout<<people[i]<<endl;
                    myfile<<people[i];
                    //Controlling the times when the ", " is printed...
                    if (i == 0|| i == pnum-1)
                    {
                        myfile<<' ';
                    }   //end if
                    else
                    {
                        myfile<<", ";
                    }
                }   //end for
            delete[] people;    //the dynamic memory will be turned off to free it
            myfile<<endl;
            myfile<<' ';
            }   //end else
            pos = myfile.tellp();   //This will return the position of the last character in the file.
            myfile.close();     //We are forced to close the file.
            return pos;
        }   //end names
        //Today's date will already be entered for you via this function...

        void equip() {
            //In this function, the actual input for the experiment starts.
            //First, we must get all the lines and reprint them (again).
            ofstream mfile("physicslab.txt");
            //mfile.open();
            //names()pos = mfile.tellp();
            mfile.seekp(pos);
            cout<<pos;

            //Get ready to measure and input.

            cout<<"Please enter the radius of the crossbar (in meters): ";
            getline(cin, numstr);           //All input will be handled using the stringstream method.
            stringstream(numstr)>>rcrossbar;
            cout<<"Now enter the diameter of the vertical shaft (in meters): ";
            getline(cin, numstr);
            stringstream(numstr)>>dshaft;
            cout<<"Enter the mass of the hanger (in kilograms): ";
            getline(cin, numstr);
            stringstream(numstr)>>mass;
            rshaft = dshaft/2;  //Duh!
            //Writing all this data to the text file...
            mfile<<"Radius"<<'\t'<<'\t'<<'|'<<rcrossbar<<" m "<<endl;
            mfile<<"Diameter of the shaftbar"<<'|'<<dshaft<<" m "<<endl;
            mfile<<"Radius of the shaftbar"<<"  |"<<rshaft<<" m "<<endl;
            mfile<<"Hanging Mass"<<'\t'<<'|'<<mass<<" kg "<<endl;
            //And testing it by showing it onscreen:
            cout<<"rcrossbar="<<rcrossbar<<endl;
            cout<<"dshaft="<<dshaft<<endl;
            cout<<"rshaft="<<rshaft<<endl;
            cout<<"mass="<<mass<<endl;
        }   //end equip
        friend class tdata;
};   //end intro

intro::intro(int x) {
    a = x;
}   //end constructor

int main ()
{

    intro i(3);
    //tdata t;
    //i.intro(3);
    i.names();
    //i.equip();
    tdata * nums = new tdata[4];
    for (int n = 0; n<4;n++)
    {
        nums = tdata[n];
    }   //end for

    return 0;
}   //end main 
tablea(b) //This parametric function will take on three arrays of data
This is an attempt at defining a function; when you define a function, you must put the type of each parameter in the first line. For example,
void tablea(int b)


36|warning: no return statement in function returning non-void;

If your function tablea isn't going t ot return anything, you must declare it as follows:

void tablea (int b)
You need to indicate what it will be returning, or if it returns nothing, say so using void.

47|error: 'in' has incomplete type;
The code here is confused because you've used the term intro before you've defined it. The compiler has to know about your new class types before you use them. You've forward declared it, but at this point the compiler needs more than that.

error: no matching function for call to 'tdata::tdata()';

Your tdata object has no default constructor. This - new tdata[4] - wants to use the default constructor, which sadly does not exist.

I don't see any inheritance in this. I think you've misunderstood what inheritance is.




I got the problems fixed and am now trying to repetitively call a constructor. I run the program, and the only error is in the main function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main ()
{

    intro i;
    i.names();
    i.equip();
    tdata tnums[4];
    for (int n = 0; n<4;n++)
    {
        tnums[n].tdata(n);
        tnums[n].convertmass(i);
        tnums[n].tabled(n);
    }   //end for
    //Unfortunately, we must terminate our loop and call the aoutline function. We will make another for loop to deal with
    //the rows of data...
    tnums[0].aoutline();
    for (int ntwo=0;ntwo<4;ntwo++)
    {
        tnums[ntwo].convertmass(i);    //initializing m and r, if necessary...
        tnums[ntwo].convertr(i);
        tnums[ntwo].tablea(ntwo);     //Calling tablea...
    }   //end row caller
    return 0;
}   //end main 


It's telling me something like: "invalid use of 'class tdata'".
How can I correct these mistakes?
Last edited on
please answer, and keep in mind that my code has changed since my first post. tabled() is now before tablea() and is fulfilling the purpose that tablea() previously had. So it is structured: convertmass(intro p),convertr(intro p), tabled(), aoutline(), tablea()
Last edited on
You are not supposed to call a constructor like you call a member function. The constructor is only used to when creating a new object, not to modify existing objects. I think this should work tnums[n] = tdata(n); tdata(n); creates a new object that you then assign (copy) to tnums[n] using the implicitly defined operator=.
Thanks a ton! Good timing too! (It's Thanksgiving, get it?) This actually solved the problem!! I will now try to incorporate this into a larger program.
Last edited on
Topic archived. No new replies allowed.