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; 
    
}

Dani AI

Generated

Quick diagnosis and a minimal plan of action: the compiler message "switch quantity not an integer" means the expression you handed to switch isn't an integral type. In the original code substr(...) returns a std::string, so switch rejects it. Follow ’s hint — use a char (take the character by indexing the string) or test characters with std::isdigit, and make sure the loop index is advanced (or use a for / range-for so you can't forget the ++).

A simple, robust validation pattern (avoids a long switch) is:

#include <cctype>

for (std::string::size_type i = 0; i < ssNum.size(); ++i) {
    unsigned char uc = static_cast<unsigned char>(ssNum[i]);
    if (!std::isdigit(uc)) {
        std::cout << "Please enter numeric digits only.\n";
        break;
    }
}

Notes on that snippet: cast to unsigned char before calling std::isdigit to avoid undefined behavior on platforms where char is signed. Use std::string::size_type (or size_t) for the index to avoid signed/unsigned mismatches.

If a switch is still desired, it must receive a char or integral value (as pointed out). Also remember the semantics mentioned — char values are compared by their numeric code — and ’s clarification: char is an integer type; code-point encoding is an implementation detail, not a switch mechanism.

Final tips: prefer a for or range-based loop to avoid missing the increment, validate all characters before inserting hyphens, and prefer std::all_of / std::isdigit for concise, readable checks in modern C++.

Recommended Answers

All 4 Replies

>> 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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.