I'm trying to use a switch statement in my code but I'm having a bit of difficulty. I have an error message I do not understand and do not know how to fix. The message reads, "switch quantity not an integer". I'm new to C++ and would appreciate any help given. Thanks in advance!

*Delilah*

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    string ssNum = "";
    int numChars = 0;
    string currentChar = 0;
    int subscript = 0;
 
    cout << "Enter a 9-digit social security number without dashes: ";
    getline(cin, ssNum);
    while (ssNum.length() != 9)
    {
          cout << "Item number length is not valid." << endl;
          cout << "Enter a 9-digit social security number without dashes: ";
          getline(cin, ssNum);
    }
    numChars = static_cast<int>(ssNum.length());
    
while (subscript < numChars)
{
      currentChar = ssNum.substr(subscript, 1);
switch(currentChar)
{
                    case '0':
                    case '1':
                    case '2':
                    case '3':
                    case '4':
                    case '5':
                    case '6':
                    case '7':
                    case '8':
                    case '9':
                    break;
                    default :cout<<"Please enter numeric digits only.\n\n";
 }   
}
   
   ssNum.insert(5, "-");
   ssNum.insert(3, "-");
     
   cout << ssNum << endl;
   
   system("Pause");
   return 0; 
    
}

>> string currentChar = 0;

should be

char currentChar;


Question: how to finish this loop:

>> while (subscript < numChars) ?

so what about subscript++ ?

Greetings to Cotton State! I've often been to Huntsville :)

-- tesu

Edited 6 Years Ago by tesuji: n/a

The gentlemen above me have done a great job & answered well. I'd just like to add that chars inside switches are in fact converted to their ASCII equivalent ints. :)

The gentlemen above me have done a great job & answered well. I'd just like to add that chars inside switches are in fact converted to their ASCII equivalent ints. :)

Not quite. A char is just an integer with (usually) severe constraints on its value. Operations that involve chars and other integral types have nothing to do with ASCII or any other encoding.

This question has already been answered. Start a new discussion instead.