Comp Sci C++ Question: Display Element1 Contents

  • Thread starter Thread starter chelsea9947
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
The discussion centers around displaying the contents of a C++ struct named ElementType, specifically for an instance called element1, which represents the element Boron. The provided code correctly initializes element1 with its properties: name, atomic number, atomic mass, and density, and uses cout to display these attributes. A user questions the necessity of displaying element1's information since it appears to be already displayed in the main function. The conversation highlights a potential misunderstanding about the program's output and the structure of the code. Ultimately, the code effectively demonstrates how to display the properties of an element in C++.
chelsea9947
Messages
7
Reaction score
0
#include <iostream>

#include <iostream>
#include <string>


using namespace std;

struct ElementType
{
string Element;
int AtomicNumber;
float AtomicMass;
float Density;

};
int main()
{


ElementType element1, element2;

element1.Element = "Boron";
element1.AtomicNumber = 5;
element1.AtomicMass = 10.811;
element1.Density = 2.34;

cout << "Name = " << element1.Element << endl;
cout << "Atomic Number = " << element1.AtomicNumber << endl;
cout << "Atomic Mass = " << element1.AtomicMass << endl;
cout << "Density = " << element1.Density << endl;


element2.Element = "Tungsten";
element2.AtomicNumber = 74;
element2.AtomicMass = 183.85;
element2.Density = 19.30;

cout << "Name = " << element2.Element << endl;
cout << "Atomic Number = " << element2.AtomicNumber << endl;
cout << "Atomic Mass = " << element2.AtomicMass << endl;
cout << "Density = " << element2.Density << endl;
}
return 0;
}

I just want to display element1 contents

element1.Element = "Boron";
element1.AtomicNumber = 5;
element1.AtomicMass = 10.811;
element1.Density = 2.34;

cout << "Name = " << element1.Element << endl;
cout << "Atomic Number = " << element1.AtomicNumber << endl;
cout << "Atomic Mass = " << element1.AtomicMass << endl;
cout << "Density = " << element1.Density << endl;


arsenal44 is online now Add to arsenal44's Reputation Report Post Reply With Quote
 
Physics news on Phys.org


chelsea9947 said:
#include <iostream>
#include <string>


using namespace std;

struct ElementType
{
string Element;
int AtomicNumber;
float AtomicMass;
float Density;

};
int main()
{


ElementType element1, element2;

element1.Element = "Boron";
element1.AtomicNumber = 5;
element1.AtomicMass = 10.811;
element1.Density = 2.34;

cout << "Name = " << element1.Element << endl;
cout << "Atomic Number = " << element1.AtomicNumber << endl;
cout << "Atomic Mass = " << element1.AtomicMass << endl;
cout << "Density = " << element1.Density << endl;


element2.Element = "Tungsten";
element2.AtomicNumber = 74;
element2.AtomicMass = 183.85;
element2.Density = 19.30;

cout << "Name = " << element2.Element << endl;
cout << "Atomic Number = " << element2.AtomicNumber << endl;
cout << "Atomic Mass = " << element2.AtomicMass << endl;
cout << "Density = " << element2.Density << endl;
}
return 0;
}

I just want to display element1 contents

element1.Element = "Boron";
element1.AtomicNumber = 5;
element1.AtomicMass = 10.811;
element1.Density = 2.34;

cout << "Name = " << element1.Element << endl;
cout << "Atomic Number = " << element1.AtomicNumber << endl;
cout << "Atomic Mass = " << element1.AtomicMass << endl;
cout << "Density = " << element1.Density << endl;


arsenal44 is online now Add to arsenal44's Reputation Report Post Reply With Quote

Well, how do you display information from a C++ program?
 


Isn't the information for element1 and element2 being displayed in main()? I don't understand what the problem is.
 
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Replies
3
Views
1K
Replies
2
Views
3K
Replies
8
Views
1K
Replies
3
Views
2K
Replies
9
Views
2K
Replies
23
Views
3K
Replies
7
Views
2K
Replies
2
Views
2K
Replies
13
Views
2K
Back
Top