<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alexandru</title>
    <description>The latest articles on DEV Community by Alexandru (@alexandruv156).</description>
    <link>https://dev.to/alexandruv156</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3982557%2F864581d8-c476-4809-8c92-a0c0cafecfa3.png</url>
      <title>DEV Community: Alexandru</title>
      <link>https://dev.to/alexandruv156</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alexandruv156"/>
    <language>en</language>
    <item>
      <title>Stop Using Meters in C++ Graphics: Building a Real-World Material Mass Calculator</title>
      <dc:creator>Alexandru</dc:creator>
      <pubDate>Tue, 16 Jun 2026 11:34:33 +0000</pubDate>
      <link>https://dev.to/alexandruv156/stop-using-meters-in-c-graphics-building-a-real-world-material-mass-calculator-4ln</link>
      <guid>https://dev.to/alexandruv156/stop-using-meters-in-c-graphics-building-a-real-world-material-mass-calculator-4ln</guid>
      <description>&lt;p&gt;When building software for engineers, architects, or machinists, you quickly learn one golden rule: &lt;strong&gt;nobody uses meters on the shop floor or construction site.&lt;/strong&gt; Everything is measured in millimeters (mm).&lt;/p&gt;

&lt;p&gt;Today, we will build a practical C++ console application that estimates the mass of various 3D structural shapes based on their material density and dimensions—optimized for real-world blueprints.&lt;/p&gt;




&lt;h2&gt;
  
  
  📐 The Physics &amp;amp; Math Behind It
&lt;/h2&gt;

&lt;p&gt;The core formula is straightforward:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Mass = Density × Volume
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since density is typically given in kg/m³ and structural dimensions are in mm, our calculated volume will initially be in cubic millimeters (mm³). To get the correct mass in kilograms, we must divide the final volume by 1,000,000,000 (10⁹) to convert mm³ to m³.&lt;/p&gt;

&lt;p&gt;Here are the geometric shapes we will support:&lt;/p&gt;

&lt;p&gt;Rectangular Prism: V = l × w × h&lt;/p&gt;

&lt;p&gt;Solid Cylinder: V = (π × d² × l) / 4&lt;/p&gt;

&lt;p&gt;Hollow Tube: V = [π × (D² - d²) × l] / 4&lt;/p&gt;

&lt;p&gt;Hexagonal Prism: V = (3 × √3 / 2) × a² × l&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;cmath&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;solid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;mat_density&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;mass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Enter the solid type: r(rectangular prism), c(cylinder), t(tube), h(hexagonal prism): "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;cin&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;solid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;solid&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="sc"&gt;'r'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;solid&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="sc"&gt;'c'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;solid&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="sc"&gt;'t'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;solid&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="sc"&gt;'h'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Invalid solid type! Enter r, c, t, or h: "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;cin&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;solid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Enter the material density [kg/m3]: "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;cin&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mat_density&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;solid&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="sc"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Enter the length of the prism [mm]: "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;cin&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Enter the width of the prism [mm]: "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;cin&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Enter the height of the prism [mm]: "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;cin&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="n"&gt;mass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mat_density&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;1000000000.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="sc"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Enter the length of the cylinder [mm]: "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
            &lt;span class="n"&gt;cin&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Enter the diameter of the base [mm]: "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;cin&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="n"&gt;mass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mat_density&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;M_PI&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;4.0&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;1000000000.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="sc"&gt;'t'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;D&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Enter the length of the tube [mm]: "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
            &lt;span class="n"&gt;cin&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Enter the outer diameter [mm]: "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;cin&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;D&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Enter the inner diameter [mm]: "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;cin&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;D&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"[Error] Outer diameter must be larger than inner diameter!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="n"&gt;mass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mat_density&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;M_PI&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;D&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;D&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;4.0&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;1000000000.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="sc"&gt;'h'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Enter the length of the prism [mm]: "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
            &lt;span class="n"&gt;cin&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Enter the side of the base [mm]: "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;cin&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="n"&gt;mass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mat_density&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.0&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;2.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;1000000000.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"The mass of the solid is: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;mass&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" kg"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;⚡ Key Takeaways for Clean Code&lt;br&gt;
Hardcoded Constants over pow(): Using 1000000000.0 directly instead of pow(10, 9) avoids runtime floating-point overhead, making compile-time math cleaner.&lt;/p&gt;

&lt;p&gt;Early Returns: Catching the impossible case where a pipe's inner diameter is larger than its outer diameter prevents negative mass artifacts.&lt;/p&gt;

&lt;p&gt;Initialization Safeguards: Forcing mass = 0.0 at the top ensures that if something catastrophic happens, your system won't output garbage data stored in your RAM.&lt;/p&gt;

&lt;p&gt;What shape should we add next? Steel I-beams or asymmetric channels? Let me know in the comments!&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>math</category>
      <category>engineering</category>
      <category>programming</category>
    </item>
    <item>
      <title>Building a Simple Circuit Resistance Calculator in C++ (and a logic trap to avoid)</title>
      <dc:creator>Alexandru</dc:creator>
      <pubDate>Mon, 15 Jun 2026 08:38:40 +0000</pubDate>
      <link>https://dev.to/alexandruv156/building-a-simple-circuit-resistance-calculator-in-c-and-a-logic-trap-to-avoid-4p5b</link>
      <guid>https://dev.to/alexandruv156/building-a-simple-circuit-resistance-calculator-in-c-and-a-logic-trap-to-avoid-4p5b</guid>
      <description>&lt;p&gt;Every programmer has, at some point, fallen into a logical trap. While building a simple console-based electrical circuit simulator in C++, I stumbled upon a classic boolean logic mistake that completely broke my code. &lt;/p&gt;

&lt;p&gt;Here is how to build this useful engineering tool and, more importantly, how to avoid the infinite loop trap!&lt;/p&gt;




&lt;h2&gt;
  
  
  ⚡ The Goal: An Equivalent Resistance Calculator
&lt;/h2&gt;

&lt;p&gt;In physics, calculating the total resistance depends on how components are linked:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Series:&lt;/strong&gt; R_total = R1 + R2 + ... + Rn&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parallel:&lt;/strong&gt; R_total = (R1 * R2) / (R1 + R2)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We want a program where the user specifies the number of resistors, inputs their values, and chooses the connection mode (&lt;code&gt;s&lt;/code&gt; for serial, &lt;code&gt;p&lt;/code&gt; for parallel) interactively.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚨 The Logical Trap: The Double Non-Equality Bug
&lt;/h2&gt;

&lt;p&gt;When validating user input, my first instinct was to create a &lt;code&gt;while&lt;/code&gt; loop that kept asking for the link mode if the user typed an invalid character. It looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ❌ THE BUGGY CODE&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="sc"&gt;'s'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;link&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="sc"&gt;'p'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Invalid link mode. Enter 's' or 'p': "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;cin&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why this creates an Infinite Loop:&lt;br&gt;
The || (OR) operator means the condition is true if either side is true.&lt;br&gt;
If the user types 's', the first check (link != 's') is False. But the second check (link != 'p') is True (because 's' is not 'p').&lt;br&gt;
Since False OR True = True, the loop runs forever, blocking the program even if you type the correct letter!&lt;/p&gt;

&lt;p&gt;🛠️ The Final, Bug-Proof Solution&lt;br&gt;
To fix this, we must use the &amp;amp;&amp;amp; (AND) operator. The loop should only run if the input is not 's' AND it is also not 'p'.&lt;/p&gt;

&lt;p&gt;Here is the complete, working C++ source code:&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#include &amp;lt;iostream&amp;gt;

using namespace std;

int main()
{ 
    int n;
    double R, Rc;
    char link;

    cout &amp;lt;&amp;lt; "Enter the number of the resistances in the circuit: ";
    cin &amp;gt;&amp;gt; n;
    cout &amp;lt;&amp;lt; "Enter the value of the first resistance (ohms): ";
    cin &amp;gt;&amp;gt; R;
    Rc = R;

    for(int i = 2; i &amp;lt;= n; i++){
        cout &amp;lt;&amp;lt; "Enter the value of the current resistance (ohms): ";
        cin &amp;gt;&amp;gt; R;
        cout &amp;lt;&amp;lt; "Enter the link mode (s-serial/p-parallel): ";
        cin &amp;gt;&amp;gt; link;

        while(link != 's' &amp;amp;&amp;amp; link != 'p'){
            cout &amp;lt;&amp;lt; "Invalid link mode. Enter the link mode (s-serial/p-parallel): ";
            cin &amp;gt;&amp;gt; link;
        }

        if(link == 's'){
            Rc = Rc + R;
        }
        else {
            Rc = Rc * R / (Rc + R);
        }
    }

    cout &amp;lt;&amp;lt; "The equivalent resistance of the circuit is: " &amp;lt;&amp;lt; Rc &amp;lt;&amp;lt; " ohms" &amp;lt;&amp;lt; endl;

    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>electronics</category>
      <category>cpp</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Beyond Calculators: Building Automated Motion Reports in SMath Studio</title>
      <dc:creator>Alexandru</dc:creator>
      <pubDate>Sun, 14 Jun 2026 19:12:40 +0000</pubDate>
      <link>https://dev.to/alexandruv156/beyond-calculators-building-automated-motion-reports-in-smath-studio-2al1</link>
      <guid>https://dev.to/alexandruv156/beyond-calculators-building-automated-motion-reports-in-smath-studio-2al1</guid>
      <description>&lt;p&gt;💻 &lt;strong&gt;The SMath Cheat Sheet: How to Actually Type It&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;SMath Studio looks like a text editor, but it behaves like a visual math canvas. If you just type normally, you will get errors. Here is the exact shorthand formula to make everything look like the screenshot:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;How to insert Units of Measurement (km/hr, hr, m)&lt;/strong&gt;&lt;br&gt;
Do not just type the text. SMath needs to know it's a physical unit. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The Secret:&lt;/strong&gt; Type a single quote &lt;code&gt;'&lt;/code&gt; right before the unit. &lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Example for Velocity:&lt;/em&gt; Type &lt;code&gt;v:60*'km/hr&lt;/code&gt;. SMath will instantly shade the unit in blue, meaning it recognizes it dynamically.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Difference Between &lt;code&gt;:&lt;/code&gt; and &lt;code&gt;=&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The Assignment (:=):&lt;/strong&gt; Press the colon key &lt;code&gt;:&lt;/code&gt; when you want to store a value or formula inside a variable (e.g., &lt;code&gt;v:60&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Evaluation (=):&lt;/strong&gt; Press the standard equals key &lt;code&gt;=&lt;/code&gt; only when you want SMath to calculate and show you the final result (e.g., &lt;code&gt;d=&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;How to create the if/else block&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Type &lt;code&gt;f(x):&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Look at the right sidebar panel called &lt;strong&gt;"Functions"&lt;/strong&gt; (or press &lt;code&gt;Ctrl + E&lt;/code&gt; if it's hidden).&lt;/li&gt;
&lt;li&gt;Click on the &lt;strong&gt;if&lt;/strong&gt; button. It will automatically generate the logical branch structure with the placeholders on your canvas.&lt;/li&gt;
&lt;li&gt;For the "greater than or equal to" sign (≥), simply type &lt;code&gt;&amp;gt;=&lt;/code&gt; on your keyboard.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;How to spawn the Graph&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Click on an empty space on your grid.&lt;/li&gt;
&lt;li&gt;Press the &lt;code&gt;@&lt;/code&gt; key (Shift + 2 on most keyboards). This is the universal shortcut that instantly creates the 2D plot box. &lt;/li&gt;
&lt;li&gt;Type &lt;code&gt;f(x)&lt;/code&gt; in the bottom-left input field and press &lt;strong&gt;Enter&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzada5ilcp640nwbilfm7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzada5ilcp640nwbilfm7.png" alt=" " width="758" height="788"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>smath</category>
      <category>engineering</category>
      <category>datavisualization</category>
    </item>
    <item>
      <title>🚀 Math vs. Strings: Two Ways to Manipulate Digits in C++</title>
      <dc:creator>Alexandru</dc:creator>
      <pubDate>Sun, 14 Jun 2026 11:03:37 +0000</pubDate>
      <link>https://dev.to/alexandruv156/math-vs-strings-two-ways-to-manipulate-digits-in-c-geb</link>
      <guid>https://dev.to/alexandruv156/math-vs-strings-two-ways-to-manipulate-digits-in-c-geb</guid>
      <description>&lt;p&gt;In programming, there’s rarely just one way to solve a problem. In this article, we’ll take a classic coding challenge—replacing even digits in a number with 1—and tackle it using two completely different paradigms: pure mathematical logic and modern string manipulation.&lt;/p&gt;

&lt;p&gt;Along the way, we’ll explore how to handle tricky edge cases (like 0 and negative numbers) and highlight a common, dangerous C++ rookie mistake: accidentally destroying your variables inside a loop. Whether you are looking for raw performance or clean, readable code, this quick guide has got you covered!&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#include &amp;lt;iostream&amp;gt;
#include &amp;lt;string&amp;gt;

using namespace std;

int main()
{
    int number;
    int number_copy;
    int nt = 0; // Stores the result for the first method

    cout &amp;lt;&amp;lt; "Enter your number: ";
    cin &amp;gt;&amp;gt; number;

    // --- EDGE CASE HANDLING ---
    // Rule 1: We only accept positive numbers for this algorithm
    if (number &amp;lt; 0) {
        cout &amp;lt;&amp;lt; "Please enter a positive number!" &amp;lt;&amp;lt; endl;
        return 0;
    }

    // Rule 2: Handing zero separately because it's an even number 
    // and mathematical division loops won't process it.
    if (number == 0) {
        cout &amp;lt;&amp;lt; "The changed number for the first method is: 1" &amp;lt;&amp;lt; endl;
        cout &amp;lt;&amp;lt; "The changed number for the second method is: 1" &amp;lt;&amp;lt; endl;
        return 0;
    }
    else {
        // CRITICAL STEP: Back up the original number.
        // The while loop below will destroy the 'number' variable (reducing it to 0).
        number_copy = number;

        long long p = 1; // Base 10 multiplier to reconstruct the number in the correct order

        // --- METHOD 1: THE MATHEMATICAL APPROACH ---
        while (number != 0) {
            int l = number % 10; // Extract the last digit

            // Check if the digit is odd
            if (l % 2 != 0) {
                nt = nt + l * p; // Keep the odd digit as it is
            }
            else {
                nt = nt + 1 * p; // Replace the even digit with 1
            }

            p = p * 10;       // Move to the next decimal place (units, tens, hundreds...)
            number = number / 10; // Remove the last digit from the number
        }
    }
    cout &amp;lt;&amp;lt; "The changed number for the first method is: " &amp;lt;&amp;lt; nt &amp;lt;&amp;lt; '\n';

    // --- METHOD 2: THE STRING MANIPULATION APPROACH ---
    // We use 'number_copy' because 'number' is now 0.
    string numberStr = to_string(number_copy); // Convert the integer to a string

    // Loop through each character of the string
    for (int i = 0; i &amp;lt; numberStr.length(); i++) {
        // Check if the current character represents an even digit
        if (numberStr[i] == '0' || numberStr[i] == '2' || numberStr[i] == '4' || numberStr[i] == '6' || numberStr[i] == '8') {
            numberStr[i] = '1'; // Replace the character with '1'
        }
    }

    // Convert the modified string back into a 64-bit integer (long long) to prevent overflow
    long long nt2 = stoll(numberStr);
    cout &amp;lt;&amp;lt; "The changed number for the second method is: " &amp;lt;&amp;lt; nt2 &amp;lt;&amp;lt; endl;

    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>cpp</category>
      <category>beginners</category>
      <category>algorithms</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>C++ Basics: How to Determine a Triangle Type Using IF-ELSE Statements</title>
      <dc:creator>Alexandru</dc:creator>
      <pubDate>Sat, 13 Jun 2026 11:11:55 +0000</pubDate>
      <link>https://dev.to/alexandruv156/c-basics-how-to-determine-a-triangle-type-using-if-else-statements-ccf</link>
      <guid>https://dev.to/alexandruv156/c-basics-how-to-determine-a-triangle-type-using-if-else-statements-ccf</guid>
      <description>&lt;p&gt;A classic problem when learning C++ is checking what type of triangle you have based on 3 sides given by the user. Let's see how to write this logic cleanly using if-else structures without nesting too many conditions.&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#include &amp;lt;iostream&amp;gt;
using namespace std;

int main() {
    double a, b, c;
    cout &amp;lt;&amp;lt; "Enter three sides of the triangle: ";
    cin &amp;gt;&amp;gt; a &amp;gt;&amp;gt; b &amp;gt;&amp;gt; c;

    // Check if the sides can actually form a triangle
    if (a + b &amp;gt; c &amp;amp;&amp;amp; a + c &amp;gt; b &amp;amp;&amp;amp; b + c &amp;gt; a) {
        if (a == b &amp;amp;&amp;amp; b == c) {
            cout &amp;lt;&amp;lt; "The triangle is Equilateral" &amp;lt;&amp;lt; '\n';
        }
        else if (a == b || b == c || a == c) {
            cout &amp;lt;&amp;lt; "The triangle is Isosceles" &amp;lt;&amp;lt; '\n';
        }
        else {
            cout &amp;lt;&amp;lt; "The triangle is Scalene" &amp;lt;&amp;lt; '\n';
        }
    } else {
        cout &amp;lt;&amp;lt; "These sides cannot form a valid triangle" &amp;lt;&amp;lt; '\n';
    }

    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>cpp</category>
      <category>learning</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
