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;
}
}
CodePudding user response:
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.
CodePudding user response:
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.
CodePudding user response:
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;
}