I am trying to get the program to read the number that the user puts in (eg. user inputs 835, my program says "eight three five ". I can get it to say it if it is a single digit (I just need to put in the breaks). I want to be able to do 2+ digits. What am I doing wrong?
#include <iostream>
#include <string.h>
usingnamespace std;
int number;
int main()
{
cout << "Give me a number, and I will convert it to a word: ";
cin >> number;
while (1)
{
switch (number)
{
case 0:
cout << "zero ";
case 1:
cout << "one ";
case 2:
cout << "two ";
case 3:
cout << "three ";
case 4:
cout << "four ";
case 5:
cout << "five ";
case 6:
cout << "six ";
case 7:
cout << "seven ";
case 8:
cout << "eight ";
case 9:
cout << "nine ";
}
break;
}
Ok I think I changed it, but it is still not working. I changed it to a for loop, and I think I made "number" an array. It is basically doing the same thing.
This stores the input integer into the location after the end of your array (so you're trashing memory too, which is nice). I expect you were hoping that each digit in the input integer would be taken and stored as a different integer in the array. it won't. If you input one integer, it gets stored as one integer.
Input a string (remember to include <string> - string.h is a C header that you shouldn't ever use in C++ anyway).
string inputValue;
then take each char in the string one at a time:
1 2 3 4 5 6 7 8 9 10
for ( int i =0; i < inputValue.length(); i++)
{
switch (inputValue[i])
{
case'1': // Note that this is '1', the char
cout << "one ";
break;
// and so on
}
}