{"id":97,"date":"2009-07-23T21:40:46","date_gmt":"2009-07-23T20:40:46","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=97"},"modified":"2009-07-23T21:59:35","modified_gmt":"2009-07-23T20:59:35","slug":"add_two_numbers","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/23\/add_two_numbers\/","title":{"rendered":"Add two numbers"},"content":{"rendered":"<p><span id=\"zipfile\"><a href='http:\/\/www.codingfriends.com\/wp-content\/uploads\/2009\/07\/addtwonumbers.zip'><\/a><\/span>This tutorial will read in two numbers from the console line and output the answer of the two integer values.<\/p>\n<p>The cin refers to the console input, the >> allows the console input value placed into the variable (in this case the val1, val2), NOTE. << would mean output.\n\nWith using the exception, and the try catch block of code allows for any errors in the conversion of the input into a integer value.\n\nWithin the printf, there are a few conversion types, the %d means using the corresponding parameter within printf and output a integer value, %s means a string. When I say the corresponding parameter, if there are two %d within the output string, it will display in order the values attached to the printf(...) call.\n\nThe code\n\n\n<pre lang=\"cpp\">\r\n#include <iostream>\r\n#include <exception>\r\n\r\n\/\/using the namespace std (standard) for the cin -> console input\r\nusing namespace std;       \r\n\r\nint main()\r\n{\r\n       \/\/ setup the defaul values.\r\n       int val1 =0, val2 =0;\r\n       try \r\n       {\r\n              \/\/ output to the console\r\n              printf(&#8220;Please enter number 1 : &#8220;);\r\n              \/\/ read in the input, if not a integer value, this will\r\n              \/\/ cause a error \r\n              cin >> val1;       \r\n              printf(&#8220;Please enter number 2 : &#8220;);\r\n              cin >> val2;\r\n       }\r\n       catch (exception&#038; e)\r\n       {\r\n              \/\/ write out any error exception\r\n              printf(&#8220;Not a valid input %s\\n&#8221;, e.what());\r\n       }\r\n       \/\/ output the answer of the two inputted values.\r\n       printf(&#8220;Answer : %d\\n&#8221;, (val1 + val2));\r\n       return 0;\r\n}\r\n<\/pre>\n<p>save this as cppaddtwonumbers.cpp. Once compiled (g++ cppaddtwonumbers.cpp -o cppaddtwonumbers (the -o means to output to the filename else a a.out file name would be created))<br \/>\nOnce executed the program, cppaddtwonumbers within Windows, or .\/cppaddtwonumbers within Linux\/Unix.<\/p>\n<p>The output would be <\/p>\n<pre class=\"consoleoutput\">\r\nPlease enter number 1 : 23\r\nPlease enter number 2 : 41\r\nAnswer : 64\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial will read in two numbers from the console line and output the answer of the two integer values. The cin refers to the console input, the >> allows the console input value placed into the variable (in this case the val1, val2), NOTE. console input using namespace std; int main() { \/\/ setup &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/23\/add_two_numbers\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Add two numbers<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[10,9,8,398,400],"class_list":["post-97","post","type-post","status-publish","format-standard","hentry","category-c_and_cpp","tag-c","tag-gcc","tag-gnu","tag-linux","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=97"}],"version-history":[{"count":6,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/97\/revisions"}],"predecessor-version":[{"id":113,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/97\/revisions\/113"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}