Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
Struggling to get my switch statement to work

I'm trying to create a c++ program to return a a number (spelled out) when the user enters an integer value betwwen 0 and 9, can anyone work out where I've gone wrong? Thanks in advance!

#include <iostream>
using namespace std;

int main(){

    int numinput;
    string numberWritten;

    cout << "Enter a whole number between 0 and 9" << endl;
    cin >> numinput;

    switch (numinput) {
    case 0:
        numberWritten = "Zero";
        break;
    case 1:
        numberWritten = "One";
        break;
    case 2:
        numberWritten = "Two";
        break;
    case 3:
        numberWritten = "Three";
        break;
    case 4:
        numberWritten = "Four";
        break;
    case 5:
        numberWritten = "Five";
        break;
    case 6:
        numberWritten = "Six";
        break;
    case 7:
        numberWritten = "Seven";
        break;
    case 8:
        numberWritten = "Eight";
        break;
    case 9:
        numberWritten = "Nine";
        break;
    default: "Not a number I know";

    return numberWritten;
    }
}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Replace the switch with an array:

static const char * numbers_text[] = 
{
  "Zero", "One", "Two", "Three", "Four",
  "Five", "Six", "Seven", "Eight", "Nine",
};
//...
    cout << "Enter a whole number between 0 and 9" << endl;
    cin >> numinput;
    if ((num_input >= 0) && (num_input <= 9))
    {
        cout << numbers_text[numinput] << "\n";
    }
    else
    {
        cout << "Unsupported number.\n";
    }

The array or lookup method avoids any issues with a switch statement.

Fewer lines of code, fewer chances of injecting mistakes.

over 4 years ago · Santiago Trujillo Report

0

You can't return int if int main() return string or simply said by the compiler:

no suitable conversion function from "std::string" to "int" exists

this is how you fix it:

#include <iostream>
#include <string>

using namespace std;

int main() {
    int numinput;
    string numberWritten;

    cout << "Enter a whole number between 0 and 9" << endl;
    cin >> numinput;
switch (numinput) {
    case 0:
        numberWritten = "Zero";
        break;
    case 1:
        numberWritten = "One";
        break;
    case 2:
        numberWritten = "Two";
        break;
    case 3:
        numberWritten = "Three";
        break;
    case 4:
        numberWritten = "Four";
        break;
    case 5:
        numberWritten = "Five";
        break;
    case 6:
        numberWritten = "Six";
        break;
    case 7:
        numberWritten = "Seven";
        break;
    case 8:
        numberWritten = "Eight";
        break;
    case 9:
        numberWritten = "Nine";
        break;
    default:
        numberWritten = "Not a number I know";
        break;
}
    if(numinput >= 0 && numinput <= 9) {
        cout << "The number you entered is " << numberWritten << endl;
    } else {
        cout << numberWritten << endl;
    }
    cout << "The number you entered is " << numberWritten << endl;
    return 0;
}

I apologize I forgot to test the default now it works perfectly.

over 4 years ago · Santiago Trujillo Report

0

The switch will never arrive on the

return number

As it's still inside of the curly braces. In the switch it'll stop on the default.

Your function is int main and you're trying to return a string with numberWritten.

This way it works

#include <iostream>
using namespace std;

int main()
{
int numinput;
string numberWritten;

cout << "Enter a whole number between 0 and 9" << endl;
cin >> numinput;

switch (numinput)
{
case 0:
    numberWritten = "Zero";
    break;
case 1:
    numberWritten = "One";
    break;
case 2:
    numberWritten = "Two";
    break;
case 3:
    numberWritten = "Three";
    break;
case 4:
    numberWritten = "Four";
    break;
case 5:
    numberWritten = "Five";
    break;
case 6:
    numberWritten = "Six";
    break;
case 7:
    numberWritten = "Seven";
    break;
case 8:
    numberWritten = "Eight";
    break;
case 9:
    numberWritten = "Nine";
    break;
default: "Not a number I know";
}

cout << numberWritten << endl;

return 0;
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!