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!
Among the problems
incorrect file init testing.
untested extractions of B and b
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
Related
I want to store the values produced by a recursive function in a string, but I am not sure how to keep after each iteration of the loop. Im not necessarily looking for you to solve it particular to the attached code, but I figured it would give it some context. Simply commenting resources where I can learn this is, of course, welcome.
Thanks
int HailstoneNumbers(int N)
{
vector <char> sequence;
static int c;
cout << N << " ";
if (N == 1 && c == 0) {
// N is initially 1.
return c;
}
else if (N == 1 && c != 0) {
// N is reduced to 1.
c++;
return c;
}
else if (N % 2 == 0) {
// If N is Even.
c++;
HailstoneNumbers(N / 2);
}
else if (N % 2 != 0) {
// N is Odd.
c++;
HailstoneNumbers(3 * N + 1);
}
}
If you want to store N values, you can store them in a vector as follows
int HailstoneNumbers(int N, vector<int>& sequence)
{
int c;
sequence.push_back(N);
if (N == 1 && c == 0) {
// N is initially 1.
return c;
}
else if (N == 1 && c != 0) {
// N is reduced to 1.
c++;
return c;
}
else if (N % 2 == 0) {
// If N is Even.
c++;
HailstoneNumbers(N / 2, sequence);
}
else if (N % 2 != 0) {
// N is Odd.
c++;
HailstoneNumbers(3 * N + 1, sequence);
}
}
declare a vector before calling your function as vector<int> sequence; then call your function using your N and this vector
I need to make a program that will check whether or not the number typed in (a) and its mirrored self (a1) are both prime numbers. I got it to work up to the point where I input a multiplier of 10, in which case it declares it as a prime number, which it clearly isn't.
I've already tried setting the condition:
if ( a % 10 = 0 ) {//declare it as non prime}
After having done that, I would always get a return value of 0 after entering the number. Also tried declaring :
if ( a == 1 ) {//declare it as a non prime}
which fixed it for multipliers of 10 up to 100, but the rest would give me the previously stated error.
My go at it:
#include <iostream>
using namespace std;
int main() {
int a, a1, DN;
cin >> a;
DN = a;
a1 = 0;
for (; a != 0;) {
a1 *= 10;
a1 = a1 + a % 10;
a /= 10;
}
int este_prim, i, este_prim2;
este_prim = 1;
i = 2;
este_prim2 = 1;
while (i < DN && i < a1) {
if (DN % i == 0) {
este_prim = 0;
}
++i;
}
if (a1 > i && a1 % i == 0) {
este_prim2 = 0;
}
++i;
if (a == 1) {
este_prim = 0;
}
if (a1 == 1) {
este_prim2 = 0;
}
if (este_prim2 == 1 && este_prim == 1) {
cout << "DA";
} else {
cout << "NU";
}
return 0;
}
I'm a complete newbie at this so any help would be appreciated. Cheers!
Your loop checks if DN is prime, but it doesn't check if a1 is prime. And this block of code is something I do not understand.
if (a1 > i && a1 % i == 0) {
este_prim2 = 0;
}
So just remove that.
Use this worthy helper function to detect if a positive number is prime:
bool isPrime(int x)
{
if (x <= 1)
return false;
// 2 is the only even prime
if (x == 2)
return true;
// any other even number is not prime
if ((x % 2) == 0)
return false;
// try dividing by all odd numbers from 3 to sqrt(x)
int stop = sqrt(x);
for (int i = 3; i <= stop; i += 2)
{
if ((x % i) == 0)
return false;
}
return true;
}
And then your code to detect if DN and it's mirror, a1 are both prime is this:
int main() {
int a, a1, DN;
cin >> a;
DN = a;
a1 = 0;
for (; a != 0;) {
a1 *= 10;
a1 = a1 + a % 10;
a /= 10;
}
bool este_prim, este_prim2;
este_prim = isPrime(DN);
este_prim2 = isPrime(a1);
if (este_prim2 && este_prim) {
cout << "DA";
} else {
cout << "NU";
}
}
My first job as an intern was to write a program to compare certain characters in the filenames of two different directories, and if they match, rename them. I wrote a custom code to match the characters. The initial few files get renamed in both directories, but it breaks after a point, giving a vector subscript out of range error.
I have an idea of how to fix such a vector range error from all the other posts, but nothing seemed to work. Any input would be appreciated!
PS: I am not a coder and this is my third official program. I understand the code is a bit messy.
Here is the code:
#include<dirent.h>
#include<vector>
#include<sstream>
int main()
{
cout << "Comparer - Renamer v.0.1.beta\n\n";
string dr1, dr2;
int x, y;
DIR *d1;
struct dirent *dir1;
vector<string> a;
a.reserve(25000);
int i = 0;
cout << "Enter the first directory (format : log_2017...) : ";
cin >> dr1;
d1 = opendir(dr1.c_str());
if (d1){
while ((dir1 = readdir(d1)) != NULL){
i++;
a.push_back(dir1->d_name);
}
closedir(d1);
}
x = a.size();
cout << "\nEnter the second directory (format : 2017.12...) : ";
cin >> dr2;
DIR *d2;
struct dirent *dir2;
vector<string> b;
b.reserve(25000);
int j = 0;
d2 = opendir(dr2.c_str());
if (d2){
while ((dir2 = readdir(d2)) != NULL){
j++;
b.push_back(dir2->d_name);
}
closedir(d2);
}
y = b.size();
ostringstream osa, nsa, osb, nsb;
string oldname_a, newname_a, oldname_b, newname_b;
int u, v, w;
for (int l = 2; l < x; l++){
for (int k = l; k < y; k++){
int c = a[l][20] * 10 + a[l][21];
int d = b[k][14] * 10 + b[k][15];
int e = a[l][17] * 10 + a[l][18];
int f = b[k][11] * 10 + b[k][12];
if (a[l][4] == b[k][0] && a[l][5] == b[k][1] && a[l][6] == b[k][2] && a[l][7] == b[k][3] && a[l][9] == b[k][5] && a[l][10] == b[k][6] && a[l][12] == b[k][8] && a[l][13] == b[k][9]){
u = 0;
}
else{
u = 1;
}
if ((e - f) == 0 && abs(c - d) < 12){
v = 0;
}
else{
v = 1;
}
if ((e - f) == 1 && ((c == 58) || (c == 59) || (c == 0) || (c == 1) || (c == 2))){
w = 0;
}
else{
w = 1;
}
if (u == 0 && (v == 0 || w == 0)){
osa.str(string());
osa << dr1 << "\\" << a[l];
nsa.str(string());
nsa << dr1 << "\\" << l - 1 << ". " << a[l];
oldname_a = osa.str();
newname_a = nsa.str();
osb.str(string());
osb << dr2 << "\\" << b[k];
nsb.str(string());
nsb << dr2 << "\\" << l - 1 << ". " << b[k];
oldname_b = osb.str();
newname_b = nsb.str();
rename(oldname_a.c_str(), newname_a.c_str())
rename(oldname_b.c_str(), newname_b.c_str())
break;
}
}
}
return 0;
}
Presently the code is set such that it shows me how the comparison between the filenames is made.
It turns out I was not debugging properly, and the problem was in this part of the code:
int c = a[l][20] * 10 + a[l][21];
int d = b[k][14] * 10 + b[k][15];
int e = a[l][17] * 10 + a[l][18];
int f = b[k][11] * 10 + b[k][12];
I did not know that I couldn't assign an integer from a string/char directly to an int. I converted the char to int (which would give me the ASCII value of the char) and then subtracted it by 48 to convert it to decimal (I do not know if there is an easier way to do this, but this seemed to have worked for me!) The modified part looks like this:
c = ((int)a[l][20] - 48) * 10 + ((int)a[l][21] - 48);
d = ((int)b[k][14] - 48) * 10 + ((int)b[k][15] - 48);
e = ((int)a[l][17] - 48) * 10 + ((int)a[l][18] - 48):
f = ((int)b[k][11] - 48) * 10 + ((int)b[k][12] - 48);
There was also a small manual error in the conditions, which I also rectified.
I'm rather new to C/C++. I have a segment of my application which doesn't seem to work as I'd want but I cannot understand why.
What I'm looking to do is when the 4 key is in the status of down, I'd like it to carry out the 'idle' function. I'd like the idle function to have 2 outcomes.
If the Up OR Down OR Left OR Right OR LMouse AND RButton then carry out the 'movement rotation operation' code else just carry out the standard idle function.
However within my code, it'll loop this while it's down but the moving() will only ever return 0
I've been messing with it for some time and trying to look on google for answers but I cannot understand why.
Here's my segment of code:
int moving()
{
int u = GetAsyncKeyState(VK_UP);
int d = GetAsyncKeyState(VK_DOWN);
int l = GetAsyncKeyState(VK_LEFT);
int r = GetAsyncKeyState(VK_RIGHT);
int mr = GetAsyncKeyState(VK_RBUTTON);
int ml = GetAsyncKeyState(VK_LBUTTON);
if(u == 1 || d == 1 || l == 1 || r == 1 || mr == 1 && ml == 1)
{
return 1;
}
}
void idle()
{
cout << "moving = " << moving() << endl;
if(moving() == 1)
{
cout << "Movement rotation operating." << endl;
}
else
{
cout << "This is the idle statement" << endl;
}
}
int main()
{
while(1)
{
if(GetAsyncKeyState('4'))
{
cout << "4 Pressed" << endl;
idle();
}
}
}
Thank you in advance.
Your logic to determine the button combination needs an extra set of parentheses.
if(u == 1 || d == 1 || l == 1 || r == 1 || (mr == 1 && ml == 1))
Also, 1 will evaluate to true so you can say
if(u || d || l || r || (mr && ml))
You could also make the function return a bool since that is really what you're after.
bool moving()
{
// ...
// code for getting button states
// ...
return (u || d || l || r || (mr && ml))
}
#include <iostream>
#include <string>
using namespace std;
// Turns a digit between 1 and 9 into its english name
// Turn a number into its english name
string int_name(int n)
{
string digit_name;
{
if (n == 1) return "one";
else if (n == 2) return "two";
else if (n == 3) return "three";
else if (n == 4) return "four";
else if (n == 5) return "five";
else if (n == 6) return "six";
else if (n == 7) return "seven";
else if (n == 8) return "eight";
else if (n == 9) return "nine";
return "";
}
string teen_name;
{
if (n == 10) return "ten";
else if (n == 11) return "eleven";
else if (n == 12) return "twelve";
else if (n == 13) return "thirteen";
else if (n == 14) return "fourteen";
else if (n == 14) return "fourteen";
else if (n == 15) return "fifteen";
else if (n == 16) return "sixteen";
else if (n == 17) return "seventeen";
else if (n == 18) return "eighteen";
else if (n == 19) return "nineteen";
return "";
}
string tens_name;
{
if (n == 2) return "twenty";
else if (n == 3) return "thirty";
else if (n == 4) return "forty";
else if (n == 5) return "fifty";
else if (n == 6) return "sixty";
else if (n == 7) return "seventy";
else if (n == 8) return "eighty";
else if (n == 9) return "ninety";
return "";
}
int c = n; // the part that still needs to be converted
string r; // the return value
if (c >= 1000)
{
r = int_name(c / 1000) + " thousand";
c = c % 1000;
}
if (c >= 100)
{
r = r + " " + digit_name(c / 100) + " hundred";
c = c % 100;
}
if (c >= 20)
{
r = r + " " + tens_name(c /10);
c = c % 10;
}
if (c >= 10)
{
r = r + " " + teen_name(c);
c = 0;
}
if (c > 0)
r = r + " " + digit_name(c);
return r;
}
int main()
{
int n;
cout << endl << endl;
cout << "Please enter a positive integer: ";
cin >> n;
cout << endl;
cout << int_name(n);
cout << endl << endl;
return 0;
}
I Keep getting this Error code:
intname2.cpp: In function âstd::string
int_name(int)â:
intname2.cpp:74: error: no match for
call to â(std::string) (int)â
intname2.cpp:80: error: no match for
call to â(std::string) (int)â
intname2.cpp:86: error: no match for
call to â(std::string) (int&)â
intname2.cpp:91: error: no match for
call to â(std::string) (int&)â
You are using digit_name, teen_name, etc as functions, when they are defined as variables. If you want to use them like that, you need to define them before your int_name function like this:
string digit_name(int n)
{
if (n == 1) return "one";
else if (n == 2) return "two";
else if (n == 3) return "three";
else if (n == 4) return "four";
else if (n == 5) return "five";
else if (n == 6) return "six";
else if (n == 7) return "seven";
else if (n == 8) return "eight";
else if (n == 9) return "nine";
return "";
}
Timothy, it looks like you're confused about the requirements of the assignment. Please make sure you understand the requirements, because at this stage it doesn't look like you know what's expected of you. You're trying to move the body of one function into the body of another function and that's simply not possible to do.
Please post the exact words that your teacher used in order for us to give you proper advice on the question.
Here are some tips for you:
If your teacher has covered switch statements then use switch statements.
Check if your teacher is not asking you to do function declarations.
Check if your teacher is not asking you to put the functions in libraries (a header file and source file).
OK scrap the tips... given your teacher's requirements I think it might look a little bit like this:
string int_name(int n)
{
int c = n; // the part that still needs to be converted
string r; // the return value
if (c >= 1000)
{
r = int_name(c / 1000) + " thousand";
c = c % 1000;
}
if (c >= 100)
{
// If you have covered switch statements then it will look like this
string digitName;
switch(c/100) // <- instead of calling digit_name(c/100), we call switch(c/100)
{
case 1:
// assign the digit name
digitName = "one";
break;
case 2:
//... fill here with your own code
break;
case 3:
//... fill here with your own code
break;
// write all the cases through 9
default:
digitName = "";
break;
}
// in the result string use the digitName variable
// instead of calling the digit_name function
r = r + " " + digitName + " hundred";
c = c % 100;
}
if (c >= 20)
{
r = r + " " + tens_name(c /10);
c = c % 10;
}
if (c >= 10)
{
r = r + " " + teen_name(c);
c = 0;
}
if (c > 0)
r = r + " " + digit_name(c);
return r;
}
Note that I'm using a switch statement, but if you your teacher hasn't shown you switch statements yet, then you can still use if/else statements:
string int_name(int n)
{
int c = n; // the part that still needs to be converted
string r; // the return value
if (c >= 1000)
{
r = int_name(c / 1000) + " thousand";
c = c % 1000;
}
if (c >= 100)
{
// declare a digitName
string digitName;
// declare a temporary value
int temp = c/100;
if(1 == temp)
{
// assign the digit name
digitName = "one";
}
else if( 2 == temp )
{
digitName = "two";
}
else if( 3 == temp )
{
// fill in the rest
}
else if( 4 == temp )
{
// fill in the rest
}
// write all the other else if statements
else
{
digitName = "":
}
// in the result string use the digitName variable
// instead of calling the digit_name function
r = r + " " + digitName + " hundred";
c = c % 100;
}
if (c >= 20)
{
r = r + " " + tens_name(c /10);
c = c % 10;
}
if (c >= 10)
{
r = r + " " + teen_name(c);
c = 0;
}
if (c > 0)
r = r + " " + digit_name(c);
return r;
}
You're going to have to take the first example with digit_name and apply it to tens_name and teen_name functions.
WARNING:
In reality you don't want to repeat the same code and clutter a single function with a bunch of code that could be in its own function. You ALWAYS want to break out repeating code into functions... if she's asking you to repeat code when you can use functions then you should be concerned. Ask your teacher if this is what she REALLY wants you to do!