Join us on IRC @ irc.r00tsecurity.org #r00tsecurity [ Web Client ] [ Log In | Register ]  
Resource Database Index -> Source Code -> Simple Calculator Example
Description // Info



Source Code

  1. #include <iostream>
  2. #include <cctype>
  3. #include <limits>
  4. #include <string>
  5.  
  6. using namespace std;
  7. bool checkNum(string);
  8.  
  9. void main() {
  10.     string nums;
  11.     string dNum1, dNum2, dResult;
  12.     char cCode;
  13.  
  14.     for(int iCount=0;iCount<=2;iCount++) {
  15.         cout << "enter a number: ";
  16.         cin >> nums;
  17.         dNum1 = nums;
  18.  
  19.         //cout << checkNum(nums) << "\n";
  20.         if(checkNum(nums)) {
  21.             cout << "enter another number: ";
  22.             cin >> nums;
  23.             dNum2 = nums;
  24.  
  25.             cout << "enter a code, +, -, *, or / : ";
  26.             cin >> cCode; //get the code
  27.  
  28.                 switch(cCode) { //determine cCode
  29.                     case '+': dResult = dNum1+dNum2; break;
  30.                     case '-': dResult = dNum1-dNum2; break;
  31.                     case '*': dResult = dNum1*dNum2; break;
  32.                     case '/': dResult = dNum1/dNum2; break;
  33.                     default: cout << "error..." << endl;
  34.                 }
  35.  
  36.             cout << "result is " << dResult << endl;
  37.  
  38.         } else {
  39.             cout << "error!";
  40.         }
  41.     }
  42. }
  43.  
  44. bool checkNum(string num) {
  45.     for(int i=0; i<(signed) num.length(); i++) {
  46.         int j = num[i] - '0';
  47.         if(j<0 || j>9) {
  48.             return false;
  49.         }
  50.     }
  51.     return true;
  52. }


Comments

You must be logged in to post comments.

 Network Access...
USER ID
PASSWORD

 Code Information
Language:
C/C++

Version:
1


Submitted:
2008-07-15 - 09:49:42


Author:
sunjester
E-Mail
Website

Greetz:


[ Download | Report Issue ]

 Code Search
Search by Language
+ Assembly
+ ASP
+ ASP.NET
+ C#
+ C/C++
+ Cobol
+ Delphi
+ Java
+ Javascript
+ Pascal
+ Perl
+ PHP
+ Python
+ VB6
+ VB.NET

Advanced Search




 
By continuing past this page, and by your continued use of this site, you agree to be bound by and abide by the User Agreement.

© 2008 r00tsecurity network. All rights reserved.
[ About Us | Contact Us | Support Us | Legal | Advertise | User Agreement | Privacy Policy ]