I am a complete novice in programming and am currently taking an introductory level class at my local university, I am currently stuck on a question and the prof provides no help whatsoever.
I am taking 3 inputs from an input file molecules.txt (the first two are element names, the third is the number of the surrounding atoms) and printing them into an output file called geometricalshapes.txt
When I run my program nothing gets printed into the output file
Here is the code I have so far that does not work:
/******************************************************************************************************
* Problem Statement: This program will calculate the molecular geometry of atom A surrounded by b atoms of element B and output the results into a file geometricalshapes.txt
*
* Input: A list of element pairs from molecules.txt, and the number of B atoms surrounding atom A
*
* Output: The determined shape of the element pairs
*
* Main Algorithm: Determine the number of valence electrons 'v' for atom A
* Subtract the number of bonding domains 'b' from 'v' to determine the number of nonbonding electrons
* Determine the number of bonding domains 'n'
* Determine the shape of the molecule
* Ouput the geometrical shape of the molecule
*
* Major Variables: string A - Central Atom
* string B - Surrounding Atom
* string shape - Shape of Molecule
* int b - Number of B atoms
* int v - Number of Valence Electrons
* int e - Number of Nonbonding Elctrons
* int n - Number of Bonding Domains
*
*
* Assumptions: Only single bonds are present
*
* Limitations: Only coded for select elements from given tables
*
* *********************************************************************************************************************************************************************************/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// Declaring Variables
string A, B, shape;
int b, v, e, n;
ifstream inputData;
ofstream outputData;
inputData.open("molecules.txt");
if (!inputData)
{
cout << "Problem opening input file. Closing program..." << endl;
return 1;
}
outputData.open("geometricalshape.txt");
if (!outputData)
{
cout << "Problem opening output file. Closing program..." << endl;
return 2;
}
// Gathering data for central atom A
inputData >> A; // priming read
while (inputData) // EOF loop
{
// If-else statements to determine number of valence electrons 'v'
if (A == "Be")
{
v = 3;
}
else if (A == "C")
{
v = 4;
}
else if (A == "Si")
{
v = 4;
}
else if (A == "N")
{
v = 5;
}
else if (A == "P")
{
v = 5;
}
else if (A == "As")
{
v = 5;
}
else if (A == "O")
{
v = 6;
}
else if (A == "S")
{
v = 6;
}
else if (A == "Se")
{
v = 6;
}
else if (A == "F")
{
v = 7;
}
else if (A == "Cl")
{
v = 7;
}
else if (A == "Br")
{
v = 7;
}
else if (A == "I")
{
v = 7;
}
else if (A == "Xe")
{
v = 8;
}
// Input data for surroudning atom and number of atoms
inputData >> B >> b;
// Calculating number of nonbonding electrons
e = v - b;
// Calculating number of bonding doamains
n = e / 2;
// If else statements to determine shape
if (b == 2 && n == 0)
{
shape == "linear";
}
else if (b == 2 && n == 1)
{
shape == "bent";
}
else if (b == 2 && n == 2)
{
shape == "bent";
}
else if (b == 2 && n == 3)
{
shape == "linear";
}
else if (b == 3 && n == 0)
{
shape == "trigonal planar";
}
else if (b == 3 && n == 1)
{
shape == "trigonal pyramidal";
}
else if (b == 3 && n == 2)
{
shape == "T-shaped";
}
else if (b == 4 && n == 0)
{
shape == "tetrahedral";
}
else if (b == 4 && n == 1)
{
shape == "seesaw";
}
else if (b == 4 && n == 2)
{
shape == "square planar";
}
else if (b == 5 && n == 0)
{
shape == "trigonal bipyramidal";
}
else if (b == 5 && n == 1)
{
shape == "square pyramidal";
}
else if (b == 6 && n == 0)
{
shape == "octahedral";
}
else
{
shape == "unknown";
}
// Outputting line data into output document
outputData << "The geometrical shape of one atom " << A << " surrounded by " << b << " "
<< B << " atoms is " << shape << endl;
// Getting next input for A
inputData >> A;
}
return 0;
}
The first few lines on the input file look like:
O F 2
S F 4
Be F 3
C P 1
And I am supposed to get results that look like:
The geometrical shape of one O atom surrounded by 2 F atoms is bent.
The geometrical shape of one S atom surrounded by 4 F atoms is seesaw.
The geometrical shape of one Be atom surrounded by 3 F atoms is trigonal planar.
The geometrical shape of one C atom surrounded by 1 P atoms is unknown.
Any and all help is appreciated! Please try and keep explanations simple as I am still very new to programming!
CodePudding user response:
Among the problems
- incorrect file init testing.
- untested extractions of
B
andb
- unused test expressions
shape ==
rather than assignments
Fixing all of the above:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// Declaring Variables
ifstream inputData("molecules.txt");
ofstream outputData("geometricalshape.txt");
if (!inputData.is_open())
{
cout << "Problem opening input file. Closing program..." << endl;
return 1;
}
if (!outputData.is_open())
{
cout << "Problem opening output file. Closing program..." << endl;
return 2;
}
string A, B, shape;
int b, v, e, n;
while (inputData >> A >> B >> b) // EOF loop
{
// If-else statements to determine number of valence electrons 'v'
if (A == "Be")
{
v = 3;
}
else if (A == "C")
{
v = 4;
}
else if (A == "Si")
{
v = 4;
}
else if (A == "N")
{
v = 5;
}
else if (A == "P")
{
v = 5;
}
else if (A == "As")
{
v = 5;
}
else if (A == "O")
{
v = 6;
}
else if (A == "S")
{
v = 6;
}
else if (A == "Se")
{
v = 6;
}
else if (A == "F")
{
v = 7;
}
else if (A == "Cl")
{
v = 7;
}
else if (A == "Br")
{
v = 7;
}
else if (A == "I")
{
v = 7;
}
else if (A == "Xe")
{
v = 8;
}
// Calculating number of nonbonding electrons
e = v - b;
// Calculating number of bonding doamains
n = e / 2;
// If else statements to determine shape
if (b == 2 && n == 0)
{
shape = "linear";
}
else if (b == 2 && n == 1)
{
shape = "bent";
}
else if (b == 2 && n == 2)
{
shape = "bent";
}
else if (b == 2 && n == 3)
{
shape = "linear";
}
else if (b == 3 && n == 0)
{
shape = "trigonal planar";
}
else if (b == 3 && n == 1)
{
shape = "trigonal pyramidal";
}
else if (b == 3 && n == 2)
{
shape = "T-shaped";
}
else if (b == 4 && n == 0)
{
shape = "tetrahedral";
}
else if (b == 4 && n == 1)
{
shape = "seesaw";
}
else if (b == 4 && n == 2)
{
shape = "square planar";
}
else if (b == 5 && n == 0)
{
shape = "trigonal bipyramidal";
}
else if (b == 5 && n == 1)
{
shape = "square pyramidal";
}
else if (b == 6 && n == 0)
{
shape = "octahedral";
}
else
{
shape = "unknown";
}
// Outputting line data into output document
outputData << "The geometrical shape of one atom " << A
<< " surrounded by " << b
<< " " << B
<< " atoms is " << shape
<< endl;
}
return 0;
}
Output (geometricalshape.txt)
The geometrical shape of one atom O surrounded by 2 F atoms is bent
The geometrical shape of one atom S surrounded by 4 F atoms is seesaw
The geometrical shape of one atom Be surrounded by 3 F atoms is trigonal planar
The geometrical shape of one atom C surrounded by 1 P atoms is unknown