Advertisement
TheMusiKid

Untitled

Jan 31st, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. // References to other code that I can use here
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std; // So I don't have to keep typing std:: before everything
  7.  
  8. bool is_digits(const string &str) // A boolean true or false function I create, with the parameter of a string (text)
  9. {
  10.     // Does the string only contain characters the teacher wants?
  11.     return str.find_first_not_of("0123456789") == string::npos; // If so, return true. Otherwise return false
  12. }
  13.  
  14. int _tmain(int argc, _TCHAR* argv[]) // This function/method gets run as soon as the program starts
  15. {
  16.     string sNums; // Variable to hold the string they enter
  17.     bool isValid; // Whether or not the string they've entered is a valid positive integer
  18.  
  19.     do { // Do the following code until the condition next to (while) is false
  20.         cout << "Enter a positive integer: "; // Ask for a string (text) from the user
  21.         getline(cin, sNums); // Get the string and store it into our variable (sNums)
  22.         cout << endl; // Display everything after this on a new line
  23.  
  24.         isValid = sNums.length() && is_digits(sNums); // Set our boolean (true or false) variable to the result (return value) of our is_digits function
  25.  
  26.         if (!isValid) { // If the entered string is not (!) valid, then do this:
  27.             cout << "That is not a valid integer. Please try again." << endl << endl; // Tell them it's not valid and put two blank lines there
  28.         }
  29.     } while (!isValid); // While !isValid [in other words, while the entered string is not (denoted by exclamation point) valid], ask for a new string
  30.  
  31.     int iCurrInt, iSum = 0, iProduct = 1; // Create three new integer variables to store our desired output
  32.  
  33.     cout << "Will parse these integers: "; //Output that we will list the integers back
  34.  
  35.     for (int i = 0, l = sNums.length(); i < l; i++) { // Loop through the characters (numbers) in the string
  36.         iCurrInt = sNums[i] - '0'; // Complicated to explain, but this will change the character into an integer we can use and store the new value
  37.  
  38.         cout << iCurrInt << " "; // Output the character and then a space
  39.  
  40.         iSum += iCurrInt; // Increment our iSum variable by the value of our current integer variable and then store it back into iSum
  41.         iProduct *= iCurrInt; // Multiple our iProduct value by the value of our current integer variable and then store it back into iProduct
  42.     }
  43.  
  44.     cout << endl; // Output an end-of-line character
  45.  
  46.     cout << "Sum of the given integers: " << iSum << endl; // Output text, then the sum, then an end-of-line character
  47.     cout << "Product of given integers: " << iProduct << endl << endl; // Output text, then the product, then two end-of-line characters
  48.  
  49.     system("pause"); // Wait for the user to press a key before finishing the program and closing it
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement