Last line is printed twice on output file

#include <iostream>
#include <fstream>
#include <string>
// returnDataType functionName(DataTypeOfParameterList);

using namespace std;
int main()
{
int v; // declare int variable for number of valence electrons of central atom A
int b; // declare int variable for number of B atoms
int e; // number of nonbonding electrons
int n; // declare int variable for number of bonding domains
string A; // declare variable for central Atom A Element
string B; // declare variable for Atom B Element
string s; // declare variable for shape of one A atom surrounded by b B atoms


//declare file I/O streams
ifstream inFile;
ofstream outFile;

inFile.open("molecules.txt"); //open input file

if (!inFile) //error check input file
{
cout << "Can't open input file successfully.";
return 1;
}

outFile.open("geometricalshapes.txt"); //open output file

if (!outFile) //error check output file
{
cout << " cant open";
return 2;
}

while(inFile) // loop to read all data in input file
{
inFile >> A; // get string data A from input file
inFile >> B; // get string data B from input file
inFile >> b; // get string data b from input file

// Nested-Ifs to assign v value for correct element
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;
else
cout << "Unlisted Element";

e = v - b; // number of nonbonding electrons is number of bonding domains of B subtracted from number of valence electrons of central atom A
n = e / 2; // number of bonding domains is the number of nonbonding electron pairs

//Nested If table outputs correct shape
if (b==2)
{
if(n==0)
{
s = "linear";
}
else if (n==1)
{
s = "bent";
}
else if (n==2)
{
s = "bent";
}
else if (n==3)
{
s = "linear";
}
else
{
s = "unknown";
}
}
else if (b==3)
{
if(n==0)
{
s = "trigonal planar";
}
else if (n==1)
{
s = "trigonal pyramidal";
}
else if(n==2)
{
s = "T-shaped";
}
else
{
s = "unknown";
}
}
else if (b==4)
{
if(n==0)
{
s = "tetrahedral";
}
else if (n==1)
{
s = "seesaw";
}
else if(n==2)
{
s = "square planar";
}
else
{
s = "unknown";
}
}
else if (b==5)
{
if(n==0)
{
s = "trigonal bipyramidal";
}
else if (n==1)
{
s = "square pyramidal";
}
else
{
s = "unknown";
}
}
else if (b==6)
{
if (n==0)
{
s = "octahedral";
}
else
{
s = "unknown";
}
}
else
{
s = "unknown";
}

// Print output to outFile
outFile << "The geometrical shape of one " << A << " atom surrounded by " << b << ' ' << B << " atoms is " << s << ".\n";

}

return 0;

}
I have faced similar problems. Not sure why it happens , but I have a solution

instead of
while(inFile) // loop to read all data in input file
{
inFile >> A; // get string data A from input file
inFile >> B; // get string data B from input file
inFile >> b; // get string data b from input file

try all what you want read in while
while( inFile >> A >> B >> b)
{
....rest of processing
}
or otherwise
check for stream error before processing like
while(inFile) // loop to read all data in input file
{
inFile >> A; // get string data A from input file
inFile >> B; // get string data B from input file
inFile >> b; // get string data b from input file
if(!infile) break;
...processing
}


Last edited on
Oh wow. it worked thank you.
Topic archived. No new replies allowed.