My problem is that I need to write a program that asks the user to input a number from 1-99 and outputs the number spelled in english. I believe that I have the general outline down in that I will break it up into statements using If and Switch statements. I want the computer to recognize on class by having all numbers bigger than 9 but less than 20 in a class, which would require recognizing the first digit of a number. Thus the 20's would be in one class, the 30's in another, the 40's in another, etc.

Without having the user input two digits for a number separately, is there a way to recognize a first digit so that a general format would be If (first digit equals 1), else?

People here know more than I do. I would like to keep this to using only these basic directives.

#include <iostream>
using namespace std;
int main()
{}

Recommended Answers

All 14 Replies

You can use the % (mod) operator to get the remainder of one number divided by another.

ie
72 % 10 = 2 <--- your first digit

You can use the % (mod) operator to get the remainder of one number divided by another.

ie
72 % 10 = 2 <--- your first digit

That would be the second digit, correct? And then I can have an output of the input # divided by ten to serve as the first number?

Makes sense to me. Now just have to figure out how to write it...I am unfortunately very slow at creating syntax thus far. Thank you!

Or you can get the input as a string and just access the first digit from there

Ok, so now I have code but have run into a snag. This is just for numbers from 10-19. I will have to write more statements for 20-99. But if I can figure this out I believe that I can figure out the rest. My code is:

/*
 * numberspell.cpp
 *
 *  Created on: Feb 2, 2011
 *      Author: Steve Robbins
 */
#include <iostream>
using namespace std;
int main()
{
	int input_number, first_digit, second_digit;

	cin >> input_number;

	first_digit=input_number/=10;
	second_digit=input_number % 10;

	if (first_digit<2){
		switch (second_digit)
		{
		case '0':
			cout << "ten";
			break;
		case '1':
			cout << "eleven";
			break;
		case '2':
			cout << "twelve";
			break;
		case '3':
			cout << "thirteen";
			break;
		case '4':
			cout << "fourteen";
			break;
		case '5':
			cout << "fifteen";
			break;
		case '6':
			cout << "sixteen";
			break;
		case '7':
			cout << "seventeen";
			break;
		case '8':
			cout << "eighteen";
			break;
		case '9':
			cout << "nineteen";
			break;
		default:
			cout << "that is not a two digit number" <<endl;
		}
		cout <<endl;
	}
	return 0;
}

The program keeps refering to my default no matter what I input. For example,
I input the number 15, but it outputs "that is not a two digit number" as per my default. I think that it is because of my first two statements (first_digit=input_number/=10; and second_digit=input_number % 10; ) where the first changes the second not to fit the switch statement for the second digit (i.e. it will not be an integer). How can I solve this, assuming that that is my problem?

Or you can get the input as a string and just access the first digit from there

We have learned about strings in the class that this is for, but haven't necessarily learned how one would access the first digit. i.e. it is off limits for this problem. I'm sure there are much better ways. Mine, at this point, have to be incredibly basic.

Thank you, though. I do appreciate the comment.

Dude, u r doin it whole wrong
for example:

first_digit=input_number/=10;// Change this to first_digit=input_number/10;(no /=10)

Now

switch (second_digit)
{
case '0':
cout << "ten";
break;
case '1':
cout << "eleven";
break;
.
.
.

case '0': means that you want to judge some CHARactor not an INTeger, for this you have to write

case 0 : 
cout << "ten";
break;

instead of

case '0':
cout << "ten";
break;

now this code of yours will work:

int input_number, first_digit, second_digit;
 cin >> input_number;
 first_digit=input_number/10;
 second_digit=input_number % 10;
 
 if (first_digit<2 && input_number>9)
 {
   switch (second_digit)
     {
        case 0:
             cout << "ten";
             break;
 
       case 1:
             cout << "eleven";
             break;
       case 2:
             cout << "twelve";
             break;
       case 3:
             cout << "thirteen";
             break;
       case 4:
             cout << "fourteen";
             break;
       case 5:
             cout << "fifteen";
             break;
       case 6:
            cout << "sixteen";
            break;
       case 7:
            cout << "seventeen";
            break;
       case 8:
            cout << "eighteen";
            break;
       case 9:
            cout << "nineteen";
            break;
       default:
               cout << "that is not a two digit number" <<endl;
 
     }
     cout <<endl;
 }
 else cout<<"Number not in range \n";

Its very easy to seperate numbers in string you can do it by:

string number,firstNo,secondNo;
cin>>number;

firstNo=number.at(0);
secondNo=number.at(1);

you can convert this to an INT by:

int firstNo=atoi(number.c_str());

Fair enough...thank you for the help. It lives!

Ok, hopefully this the last you'll have to here of this program. I believe that my syntax is just missing something but I can't figure out what it is exactly. The error messages I get are denoted by a //.

The user should be able to input a two digit number and it will output the number spelled out in english. I know that there are better ways to write this, like using a string, but we were told to use only ifs and switches.

#include <iostream>
using namespace std;
int main()
{
	int input_number, first_digit, second_digit;

	cin >> input_number;

	first_digit=input_number/10;

	if ((first_digit<2) && (input_number>9)){//sytax error

		second_digit=input_number % 10;

		switch (second_digit)
		{

		case 0:
			cout << "ten";
			break;
		case 1:
			cout << "eleven";
			break;
		case 2:
			cout << "twelve";
			break;
		case 3:
			cout << "thirteen";
			break;
		case 4:
			cout << "fourteen";
			break;
		case 5:
			cout << "fifteen";
			break;
		case 6:
			cout << "sixteen";
			break;
		case 7:
			cout << "seventeen";
			break;
		case 8:
			cout << "eighteen";
			break;
		case 9:
			cout << "nineteen";
			break;
		default:
			cout << "that is not a two digit number" <<endl;
		}
		cout <<endl;
	}

	else ((first_digit >= 2) && (input_number>9)){//expected `;' before '{' token

		switch (fist_digit)
		{

		case 2:
			cout << "twenty";
			break;
		case 3:
			cout << "thirty";
			break;
		case 4:
			cout << "fourty";
			break;
		case 5:
			cout << "fifty";
			break;
		case 6:
			cout << "sixty";
			break;
		case 7:
			cout << "seventy";
			break;
		case 8:
			cout << "eighty";
			break;
		case 9:
			cout << "ninety";
			break;
		default:
			cout << "that is not correct input" <<endl;
		}
	cout <<" ";
	}
	if ((first_digit >= 2) && (input_number>9)){//statement has no effect

		second_digit=input_number % 10;

		switch (second_digit)
				{

				case 0:
					cout << " ";
					break;
				case 1:
					cout << "one";
					break;
				case 2:
					cout << "two";
					break;
				case 3:
					cout << "three";
					break;
				case 4:
					cout << "four";
					break;
				case 5:
					cout << "five";
					break;
				case 6:
					cout << "six";
					break;
				case 7:
					cout << "seven";
					break;
				case 8:
					cout << "eight";
					break;
				case 9:
					cout << "nine";
					break;
				default:
					cout << "this is not the droid that you are looking for" <<endl;
	}
	}

	return 0;
}

From what you posted there are 2 problems.

Line 54 you need it to be else if not just else.
You cannot use else then a statement else acts as an "if the statement is not true do this instead" but else if checks the statement for another condition if the first one fails.

Then Line 56 you have mistyped first_digit.

From what you posted there are 2 problems.

Line 54 you need it to be else if not just else.
You cannot use else then a statement else acts as an "if the statement is not true do this instead" but else if checks the statement for another condition if the first one fails.

Then Line 56 you have mistyped first_digit.

ok then. The whole thing works. Thanks. I just had to get rid of the second if statement and make it into another switch. I dont know why I made it like that in the first place.

Just an FYI, to make your code cleaner, you don't need to check if your input number is larger than nine, since logically a digit cannot be higher than 9. You only need to make sure your division doesn't result in zero. Also, remember what your division and modulation is doing. Modulus gives you the remainder of a division, it's giving you your first digit. The division takes your number and moves everything down one digit, and since an int(you should use a short by the way, you don't need a whole int) cannot hold a decimal, the first digit is dropped leaving you only your second digit, or tens place.

And one final thing (to close the logical deal). If you divide the number by 100, and it's not zero, that means that the number is too large (since the number would clearly be 100+). I'm sure you can figure out how to integrate that into your code. After that, I'd say your code would be airtight, as long as the user enters the proper syntax.

Since he wants it 1-99 you would divide by 99 not 100.

edit ** red goose is right this would give 1 for 99 and anything above. I don't know what I was thinking =)

Since he wants it 1-99 you would divide by 99 not 100.

Well, if he wants 1-99, he would divide by 100 and see if the result is a non-zero(true), and if so, print out an error. Of course, I suppose you could also just check is the number is less than 100, though that seems to go against the spirit of the exercise.

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.