functions and arrays

Hey i have my code finished but i can't figure out how to put it into a single array and function form. HELP PLZ


*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;
}
closed account (z05DSL3A)
For a single array, you could use a struct:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>


struct MetalData
{
    std::string met;
    float temp;
    float res;
};

int main( )
{
    MetalData metals[7];

    metals[0].met = "tungsten";
    metals[0].temp = 0.00450;
    metals[0].res = 0.0000000525;

    //...

    return 0;
}


Tip on posting code:
http://www.cplusplus.com/forum/articles/1624/
Last edited on
closed account (z05DSL3A)
//deleted on odd double post
Last edited on
Hey thanks for the help.
Topic archived. No new replies allowed.