structures

Hi, i have my code completed, but it needs to be in structured form. However i have no lcue how to go about doing that. Any help is welcome.
thanks
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
//Resistivity
 

//Preprocessor directives needed for this program
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <math.h>
#include <fstream.h>
#include <cstring.h>



int main()

{
ofstream outfile;

//Assigning the variables in the program as floats and choice as an int.
float tungsten, copper, aluminum, mercury, lead, silver, res, temp;
float temp1[7], res1[7];
string met[7];
int choice, t0=20;


//Entering the metal in an array form
met[1]="tungsten";
met[2]="copper";
met[3]="aluminum";
met[4]="mercury";
met[5]="lead";
met[6]="silver";

//Entering the temperatures of the metals
temp1[1]=.00450;
temp1[2]=.00393;
temp1[3]=.00390;
temp1[4]=.00088;
temp1[5]=.00430;
temp1[6]=.00380;

//Entering the resistivity of the metals which was giving at t0=20
res1[1]=.0000000525;
res1[2]=.0000000172;
res1[3]=.0000000275;
res1[4]=.0000000950;
res1[5]=.0000000220;
res1[6]=.0000000147;



//opening the file
outfile.open("resistivity.txt");

//setting the header for the file
outfile.setf(ios::left);
outfile << setw(10) << "Tempature(C)" << setw(10) << "Resistivity" << "\n";

    //User chooses which metal he/she wishes to see the resistivity for.
    cout << "Choose a metal from the following and I will list the metals resistivity" "\n";
    cout << "\n" "\n" "\n";

      cout << "1.   Tungsten " "\n";
      cout << "2.   Copper   " "\n";
      cout << "3.   Aluminum " "\n";
      cout << "4.   Mercury  " "\n";
      cout << "5.   Lead     " "\n";
      cout << "6.   Silver   " "\n" "\n";
      cin >> choice;

      //used if user enters an invalid choice not between 1 and 6
      if (choice < 1 || choice > 6)
      {
      cout << "Enter a choice between 1 and 6 please" "\n";
      cin >> choice;
      }

     //Displaying the temp and resistivity for the metal the user chooses
    cout << "The Resistivity for: " << met[choice] << "\n";
    cout << "Temp(c)" << "     " << "Resistivity" << "\n";
    cout << "\n";

    //Sending the resistivity data to an outfile
    outfile << "The Resisitivity for: " << met[choice] << "\n";
    outfile << "Temp(c)" << "      " << "Resistivity" << "\n";

    //loop used to increment the tempature to 5 degrees
    for (temp=0; temp<=100; temp+=5)
    {
      //Equation calculating the resistivity for the metal chosen
      res = temp1[choice] * (1.0 + temp1[choice] * (temp - t0));

      //Displaying the tempature and resistivity from temp=0 to temp=100
      cout << setw(3)<< temp << setw(10) << "   " << res << "\n";

      outfile << setw(3) << temp << setw(10) << "   " << res << "\n";
    }

outfile.close();

cout << "\n";


      system("PAUSE");
      return 0;
}
ok, i will have it soon. Also i will fix your header to C++. Oh and does this work. because it should be std::cout or using namespace std
I dont this this compiles write cause i tried on dev and it didnt. I get it to you in 10 mins
here it is i fixed some stuff.
i added namespace std so cout and cins work now.
fixed some depreciated header like cstring.h
and change the c header to c++
<stdlib.h> to <cstdlib>
i structured it and changed most comments to comment blocks so people can understand better.
a good idea is to put date and name and info at top where it says relativity or something like that.
NOTE: nice code im keeping this for science......hehehe:)
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
/*Resistivity*/
 
//Preprocessor directives needed for this program
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
ofstream outfile;
/*Assigning the variables in the program as floats and choice as an int.*/
float tungsten, copper, aluminum, mercury, lead, silver, res, temp;
float temp1[7], res1[7];
string met[7];
int choice, t0=20;
/*Entering the metal in an array form*/
met[1]="tungsten";
met[2]="copper";
met[3]="aluminum";
met[4]="mercury";
met[5]="lead";
met[6]="silver";
/*Entering the temperatures of the metals*/
temp1[1]=.00450;
temp1[2]=.00393;
temp1[3]=.00390;
temp1[4]=.00088;
temp1[5]=.00430;
temp1[6]=.00380;
/*Entering the resistivity of the metals which was giving at t0=20*/
res1[1]=.0000000525;
res1[2]=.0000000172;
res1[3]=.0000000275;
res1[4]=.0000000950;
res1[5]=.0000000220;
res1[6]=.0000000147;
/*opening the file*/
outfile.open("resistivity.txt");
/*setting the header for the file*/
outfile.setf(ios::left);
outfile << setw(10) << "Tempature(C)" << setw(10) << "Resistivity" << "\n";
/*User chooses which metal he/she wishes to see the resistivity for.*/
cout << "Choose a metal from the following and I will list the metals resistivity" "\n";
cout << "\n" "\n" "\n";
cout << "1.   Tungsten " "\n";
cout << "2.   Copper   " "\n";
cout << "3.   Aluminum " "\n";
cout << "4.   Mercury  " "\n";
cout << "5.   Lead     " "\n";
cout << "6.   Silver   " "\n" "\n";
cin >> choice;
/*used if user enters an invalid choice not between 1 and 6*/
if (choice < 1 || choice > 6)
      {
      cout << "Enter a choice between 1 and 6 please" "\n";
      cin >> choice;
      }
/*Displaying the temp and resistivity for the metal the user chooses*/
cout << "The Resistivity for: " << met[choice] << "\n";
cout << "Temp(c)" << "     " << "Resistivity" << "\n";
cout << "\n";
/*Sending the resistivity data to an outfile*/
outfile << "The Resisitivity for: " << met[choice] << "\n";
outfile << "Temp(c)" << "      " << "Resistivity" << "\n";
/*loop used to increment the tempature to 5 degrees*/
for (temp=0; temp<=100; temp+=5)
    {
    //Equation calculating the resistivity for the metal chosen
    res = temp1[choice] * (1.0 + temp1[choice] * (temp - t0));

    //Displaying the tempature and resistivity from temp=0 to temp=100
    cout << setw(3)<< temp << setw(10) << "   " << res << "\n";
    outfile << setw(3) << temp << setw(10) << "   " << res << "\n";
    }
outfile.close();

cout << "\n";
system("PAUSE");
return 0;
}

Thanks i have one more question. Would it be possible to put this into a function form??

thanks again
Topic archived. No new replies allowed.