Hi everyone, I'll start by saying that I'm pretty horrible at programming, so
please bear with me. OK, I am writing a program that will open a data file,
search the values in the first column, and return the value associated with it
to use in calculations. The first column of data are the symbols for elements
in the periodic table of elements, and the second column is the atomic mass
associated with it. I want the user to input an element's symbol by typing it
in, and then the program to search the file, locate the atomic mass unit
associated with it, and then perform some calculations. After performing the
calculations, I want it to write the results to the output file, and to the
display. Here is a summary of what I have so far:
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
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
double grams(0), mols(0), atoms(0), avagadro_num(6.022e23);
double atomic_mass[111];
string table_symbol[111], input_symbol;
ifstream fin("table.dat");
ofstream fout ("result.dat");
int i(0), N(0);
// Open file and check for errors
if (fin.fail())
{
cerr << "table.dat did not open. \n";
exit(1);
}
if (fout.fail())
{
cerr << "result.dat did not open. \n";
exit (1);
}
cout << "Enter the element's symbol. ";
cin >> input_symbol;
cout << "Enter the weight of sample. (in grams) ";
cin >> grams;
// Read data and compute
fin >> N; // N is the total number of records in the table.dat file.
for (i=o; i<N; i++)
{
fin >> table_symbol[i] >>atomic_mass[i];
}
for (i=0; i<N; i++)
{
if (table_symbol[i] = input_symbol)
{
mols = grams / atomic_mass;
atoms = mols * avagadro_num;
}
}
// Set format flags
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(6);
fout.setf(ios::fixed);
fout.setf(ios::showpoint);
fout.precision(6);
cout << "The # of moles in the sample is: " << mols << endl;
cout << "The # of atoms in the sample is: " << atoms << endl;
fout << mols << atoms << endl;
return 0;
}
|
This is what my input data file looks like:
1 2 3 4 5 6
|
111
H 1.008
He 4.003
Li 6.941
Be 9.012
...etc..
|
To compile:
g++ -c stepb.cc
g++ -o stepb stepb.o
To execute: stepb
This is the error message that I get when I try to compile it.
Note: I don't know how to make the funny looking 'a' symbol that
surrounds the portion of code being identified, so I will use '@' here.
1 2 3 4 5 6 7 8
|
stepb.cc: In function @int main()@;
stepb.cc:44: error: could not convert @table)symbol[i].std::basic_string<_CharT, _Traits, _Alloc>::operator=
[with_CharT = char, +Traits = std::char_traits<char>, Alloc =
std::allocator<char>](((const std::basic_string<char, std::char_traits<char>,
std::allocator<char> >&)((const std::basic_string<char,
std::char_traits<char>,- std::allocator<char> >*)(& input_symbol))))@to @bool@
stepb.cc:46: error: invalid operands of types @double@ and @double [111]@ to
binary @operator/@
|
Alright, if there's anyone out there that could help me out with this, I would really appreciate it. I'm pretty horrible at programming, and for some reason I don't seem to be able to quite grasp it yet, so please keep that in mind when answering. lol. OK, thanks again and take care.
PS. I forgot to mention, although I don't think that it matters, that this
program will be a function that is called from the main() function. The
program starts off with a switch statement asking what action the user
would like to perform, and if they choose this option, then it will call
the function
void mols_and_atoms(char start);
The main program
looks like this:
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
|
#include <iostream>
#include <fstream>
using namespace std;
// Function prototypes
void ideal_gas_law(char start);
void mols_and_atoms(char start);
void mol_mass_form(char start);
void solve_gas_press(char unk);
void solve_gas_vol(char unk);
void solve_gas_mols(char unk);
void solve_gas_temp(char unk);
const float r=0.08206;
int main()
{
// Declare and initialize
char start;
// Main body of program
cout << "Please choose from the following choices. (A, B, or C): \n";
cout << "A. Use the ideal gas law. \n";
cout << "B. Calculate # of mols and atoms. \n";
cout << "C. Calculate molar mass of compound. \n";
cin >> start;
switch(start)
{
case 'a': ideal_gas_law(start); break; // Function call
case 'b': mols_and_atoms(start); break; // Function call
case 'c': mol_mass_form(start); break; // Function call
default: cout << main(); break;
}
// Exit main
return 0;
}
|
The reason that my code above is in 'main()' is simply because I like to test them out individually first, and then put them into the program. Thanks again, I appreciate any help that you could give me on this.