C++ Question: Display Element1 Contents

  • Comp Sci
  • Thread starter chelsea9947
  • Start date
  • Tags
    C++
In summary, the program displays information for two elements, Boron and Tungsten, including their names, atomic numbers, atomic masses, and densities. The program is structured using a struct named ElementType and the information is outputted in main().
  • #1
chelsea9947
8
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
  • #2


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?
 
  • #3


Isn't the information for element1 and element2 being displayed in main()? I don't understand what the problem is.
 

What is C++?

C++ is a high-level, general-purpose programming language that was developed in the early 1980s by Bjarne Stroustrup. It is an extension of the C programming language and is commonly used for developing system software, application software, device drivers, and video games.

How do I display the contents of an element using C++?

To display the contents of an element in C++, you can use the "cout" function from the library. First, you need to include the library in your code. Then, use the "cout" function followed by the element name or variable you want to display. For example, "cout << element1;" will display the contents of element1.

What is the syntax for displaying the contents of an element in C++?

The syntax for displaying the contents of an element in C++ is: "cout << elementName;". This will print the contents of the specified element to the console.

Can I display the contents of multiple elements at once in C++?

Yes, you can display the contents of multiple elements at once in C++ by using the "cout" function multiple times. For example, "cout << element1 << element2 << element3;" will display the contents of all three elements in the specified order.

Is it possible to customize the display format of an element in C++?

Yes, it is possible to customize the display format of an element in C++ using the "setw" and "setfill" functions from the library. These functions allow you to set the width and fill character for the output. For example, "cout << setw(10) << setfill('-') << element1;" will display the contents of element1 with a width of 10 characters and the fill character as "-".

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
7K
Back
Top