{"id":37847,"date":"2025-06-26T06:42:19","date_gmt":"2025-06-26T06:42:19","guid":{"rendered":"https:\/\/gaviki.com\/blog\/?p=37847"},"modified":"2025-06-26T07:22:19","modified_gmt":"2025-06-26T07:22:19","slug":"use-the-online-c-compiler-https-www-onlinegdb-com-online-c-compiler-delete-all-the-existing-code-from-main-cpp","status":"publish","type":"post","link":"https:\/\/gaviki.com\/blog\/use-the-online-c-compiler-https-www-onlinegdb-com-online-c-compiler-delete-all-the-existing-code-from-main-cpp\/","title":{"rendered":"Use the Online C++ compiler: https:\/\/www.onlinegdb.com\/online c++ compiler Delete all the existing code from main.cpp."},"content":{"rendered":"\n<p>Use the Online C++ compiler: https:\/\/www.onlinegdb.com\/online c++ compiler Delete all the existing code from main.cpp. You will write the code in the file main.cpp. Write a C++ program to input cost price (int) and sell price (int) of an item. Display the correct message as shown in the sample output, if the seller makes a profit, a loss, or no profit or loss. When you are done: Download your code. It will save the file main.cpp and rename the file to Lab3_Ex2.cpp. This is very important so that you can always access the code later. How to Submit: Select all the code using Ctrl + A \/ Cmd + A. Copy the code using Ctrl + C \/ Cmd + C. Paste the code in the answer box using Ctrl + V \/ Cmd + V on the blackboard. Ignore if the formatting of the code is different. Move to the next question or press the Save and Submit button. Sample Input \/ Output: Enter cost price: 100 Enter sell price: 120 You made a profit of 20 dirhams Enter cost price: 200 Enter sell price: 100 You made a loss of 100 dirhams Enter cost price: 60 Enter sell price: 60 No profit, no loss.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"708\" src=\"https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/06\/image-356.png\" alt=\"\" class=\"wp-image-37857\" srcset=\"https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/06\/image-356.png 1024w, https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/06\/image-356-300x207.png 300w, https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/06\/image-356-768x531.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">The Correct Answer and Explanation is:<\/mark><\/strong><\/p>\n\n\n\n<p>Here&#8217;s the C++ code you should enter into <strong>main.cpp<\/strong> using the OnlineGDB compiler:<\/p>\n\n\n\n<p>cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n    int costPrice, sellPrice;\n    \n    cout &lt;&lt; \"Enter cost price: \";\n    cin &gt;&gt; costPrice;\n    \n    cout &lt;&lt; \"Enter sell price: \";\n    cin &gt;&gt; sellPrice;\n    \n    if (sellPrice &gt; costPrice) {\n        int profit = sellPrice - costPrice;\n        cout &lt;&lt; \"You made a profit of \" &lt;&lt; profit &lt;&lt; \" dirhams\" &lt;&lt; endl;\n    } else if (sellPrice &lt; costPrice) {\n        int loss = costPrice - sellPrice;\n        cout &lt;&lt; \"You made a loss of \" &lt;&lt; loss &lt;&lt; \" dirhams\" &lt;&lt; endl;\n    } else {\n        cout &lt;&lt; \"No profit, no loss\" &lt;&lt; endl;\n    }\n    \n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p>This C++ program helps a seller determine if they have made a profit, a loss, or neither after selling an item. It begins by including the <code>iostream<\/code> header file, which allows the program to perform input and output operations. The <code>main()<\/code> function is where the program&#8217;s execution begins.<\/p>\n\n\n\n<p>The user is prompted to enter two integers: the <strong>cost price<\/strong> and the <strong>sell price<\/strong>. The program stores these inputs in variables named <code>costPrice<\/code> and <code>sellPrice<\/code>. A conditional structure (<code>if<\/code>, <code>else if<\/code>, and <code>else<\/code>) is used to compare the two prices.<\/p>\n\n\n\n<p>If the sell price is greater than the cost price, the seller has made a profit. The profit is calculated by subtracting the cost from the selling price, and the result is displayed as part of a descriptive sentence. If the cost price is greater than the sell price, the seller has made a loss, and the program calculates and displays the loss amount similarly. If the cost price and sell price are equal, the program states that there is no profit and no loss.<\/p>\n\n\n\n<p>This solution is concise and uses only fundamental constructs of C++, including input\/output operations and conditional statements. It does not use advanced data types or structures, making it ideal for beginners learning decision-making in programming.<\/p>\n\n\n\n<p>Make sure to delete the existing code in <strong>main.cpp<\/strong>, paste this new code, and run it using OnlineGDB. After confirming it works, download the file and rename it to <code>Lab3_Ex2.cpp<\/code> as instructed. Once ready, copy and paste the code into your submission portal.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"852\" height=\"1024\" src=\"https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/06\/learnexams-banner8-983.jpeg\" alt=\"\" class=\"wp-image-37858\" srcset=\"https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/06\/learnexams-banner8-983.jpeg 852w, https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/06\/learnexams-banner8-983-250x300.jpeg 250w, https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/06\/learnexams-banner8-983-768x923.jpeg 768w\" sizes=\"auto, (max-width: 852px) 100vw, 852px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Use the Online C++ compiler: https:\/\/www.onlinegdb.com\/online c++ compiler Delete all the existing code from main.cpp. You will write the code in the file main.cpp. Write a C++ program to input cost price (int) and sell price (int) of an item. Display the correct message as shown in the sample output, if the seller makes a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-37847","post","type-post","status-publish","format-standard","hentry","category-quiz-questions"],"_links":{"self":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/37847","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/comments?post=37847"}],"version-history":[{"count":1,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/37847\/revisions"}],"predecessor-version":[{"id":37860,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/37847\/revisions\/37860"}],"wp:attachment":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/media?parent=37847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/categories?post=37847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/tags?post=37847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}