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
|