janac

Multiplication table for single number

Apr 12th, 2022 (edited)
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cctype> // isdigit()
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. // This function returns true if a string represents a number.
  9. // Otherwise, it returns false.
  10. // There can be one decimal point. There can be a plus or minus sign, if followed by a digit.
  11. // All characters other than . + - must be a digit.
  12. bool validate_number(string number)
  13. {
  14.     int num_decimal_points = 0; // Count decimal points. There can only be one.
  15.     for (size_t i = 0; i < number.length(); ++i) // Check each character.
  16.     {
  17.         if (number[i] == '.') // If it's a decimal point,
  18.         {
  19.             ++num_decimal_points; // Count it.
  20.         }
  21.         else if ((number[i] == '+') || (number[i] == '-')) // If it's + or -
  22.         {
  23.             if (!isdigit(number[i + 1])) // If the next character is not a digit,
  24.             {
  25.                 return false; // This number is invalid.
  26.             }
  27.         }
  28.         else if (!isdigit(number[i])) // Else if it's not a digit,
  29.         {
  30.             return false; // This number is invalid.
  31.         }
  32.     }
  33.     if (num_decimal_points > 1) // If there's more than one decimal point,
  34.     {
  35.         return false; // This number is invalid.
  36.     }
  37.     // If there's a decimal point but no digits,
  38.     if ((num_decimal_points == 1) && (number.length() == 1))
  39.     {
  40.         return false; // This number is invalid.
  41.     }
  42.  
  43.     return true; // The number is valid.
  44. }
  45.  
  46. int main()
  47. {
  48.     string your_number = ""; // User input from the console is a string.
  49.     float your_number_float = 0.0; // User input converted to a float (has a decimal point).
  50.     vector<float> table; // This is a list of the values for the multiplication table (input times 1 to 12).
  51.  
  52.     cout << "\n This program will\n"
  53.         " print a multiplication table\n"
  54.         " of your number from 0 - 12\n\n";
  55.  
  56.     cout << " Enter a number: ";
  57.     getline(cin, your_number); // Store user input. User input from the console comes in as a string, not a number.
  58.  
  59.     while (!validate_number(your_number)) // Use validation function to make sure the string input is a number (see above).
  60.     {
  61.         cout << " " << your_number << " is not a number.\n";
  62.         cout << " Please enter a number: ";
  63.         getline(cin, your_number); // Get new user input.
  64.     }
  65.     your_number_float = stof(your_number); // Once you know the string number is valid, convert it to an actual number.
  66.  
  67.     for (int i = 0; i <= 12; ++i)
  68.         table.push_back(your_number_float * i); // Calculate the number times 1 to 12 and store them as a list.
  69.  
  70.     // Display the results.
  71.     cout << " \n\n---------------------------------\n"; // Begin title.
  72.     cout << " Number " << your_number_float
  73.         << " multiplied by 0 - 12\n\n"; // End of title.
  74.     cout << " " << your_number_float << " x\n" // Begin top of table.
  75.         " ---------------------"
  76.         << '\n'; // End top of table.
  77.     for (int j = 0; j <= 12; ++j) // Diplay each calculation, 1 to 12.
  78.         cout << " " << j << "  | "
  79.         << table[j] << '\n' // This is the calcualtion stored on the list.
  80.         << " ------------\n";
  81.  
  82. }
  83.  
Add Comment
Please, Sign In to add comment