C++ Program to Convert Number in Character

closed account (EbDhCMoL)
C++ Program to Convert Number in Character
// Numbers As Words.cpp : main project file.
#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
int number=1000;
do
{
cout<<"Enter an integer number< 1000:";
cin>>number;
} while (number < 1 || number >=1000); // Check for number to be >=1 and < 1000

int hundreds=number/100;
int tens=(number-(hundreds*100))/10;
int ones=number%10;

switch (hundreds)
{
case (9):
cout<<"Nine Hundred ";
break;
case (8):
cout<<"Eight Hundred ";
break;
case (7):
cout<<"Seven Hundred ";
break;
case (6):
cout<<"Six Hundred ";
break;
case (5):
cout<<"Five Hundred ";
break;
case (4):
cout<<"Four Hundred ";
break;
case (3):
cout<<"Three Hundred ";
break;
case (2):
cout<<"Two Hundred ";
break;
case (1):
cout<<"One Hundred ";
break;
}

switch (tens)
{
case (9):
cout<<"Ninety ";
break;
case (8):
cout<<"Eighty ";
break;
case (7):
cout<<"Seventy ";
break;
case (6):
cout<<"Sixty ";
break;
case (5):
cout<<"Fifty ";
break;
case (4):
cout<<"Fourty ";
break;
case (3):
cout<<"Thirty ";
break;
case (2):
cout<<"Twenty ";
break;

case (1):
{
if (ones==1)
{
cout<<"Eleven";
break;
}
else if (ones==2)
{
cout<<"Twelve";
break;
}
else if (ones==3)
{
cout<<"Thirteen";
break;
}
else if (ones==4)
{
cout<<"Fourteen";
break;
}
else if (ones==5)
{
cout<<"Fifteen";
break;
}
else if (ones==6)
{
cout<<"Sixteen";
break;
}
else if (ones==7)
{
cout<<"Seventeen";
break;
}
else if (ones==8)
{
cout<<"Eighteen";

break;
}else if (ones==9)
{
cout<<"Nineteen";
break;
}
else
break;
}

}
if (tens>1 || number <10) // if number greater than 19 or number less than 10
{
switch (ones)
{
case (9):
cout<<"Nine";
break;
case (8):
cout<<"Eight";
break;
case (7):
cout<<"Seven";
break;
case (6):
cout<<"Six";
break;
case (5):
cout<<"Five";
break;
case (4):
cout<<"Four";
break;
case (3):
cout<<"Three";
break;
case (2):
cout<<"Two";
break;
case (1):
cout<<"One";
break;
}
}
cout<<endl;
return 0;
}
Last edited on
Put the code that you have dumped here in code tags.

Then ask a question.
Last edited on
Please learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

M'ok, you've written some code, now what?

You obviously have questions/concerns about your code, but you didn't let us know.
Though the code looks quite clumsy and messy, it seems to work.
So what do you want ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <cmath>

using namespace std;

int main() {
	int number = 1000;

	do {
		cout << "Enter an integer number< 1000:";
		cin >> number;
	} while (number < 1 || number >= 1000); // Check for number to be >=1 and < 1000

	int hundreds = number / 100;
	int tens = (number - (hundreds * 100)) / 10;
	int ones = number % 10;

	switch (hundreds) {
		case (9):
			cout << "Nine Hundred ";
			break;
		case (8):
			cout << "Eight Hundred ";
			break;
		case (7):
			cout << "Seven Hundred ";
			break;
		case (6):
			cout << "Six Hundred ";
			break;
		case (5):
			cout << "Five Hundred ";
			break;
		case (4):
			cout << "Four Hundred ";
			break;
		case (3):
			cout << "Three Hundred ";
			break;
		case (2):
			cout << "Two Hundred ";
			break;
		case (1):
			cout << "One Hundred ";
			break;
	}

	switch (tens) {
		case (9):
			cout << "Ninety ";
			break;
		case (8):
			cout << "Eighty ";
			break;
		case (7):
			cout << "Seventy ";
			break;
		case (6):
			cout << "Sixty ";
			break;
		case (5):
			cout << "Fifty ";
			break;
		case (4):
			cout << "Fourty ";
			break;
		case (3):
			cout << "Thirty ";
			break;
		case (2):
			cout << "Twenty ";
			break;

		case (1):
			{
				if (ones == 1) {
					cout << "Eleven";
					break;
				} else if (ones == 2) {
					cout << "Twelve";
					break;
				} else if (ones == 3) {
					cout << "Thirteen";
					break;
				} else if (ones == 4) {
					cout << "Fourteen";
					break;
				} else if (ones == 5) {
					cout << "Fifteen";
					break;
				} else if (ones == 6) {
					cout << "Sixteen";
					break;
				} else if (ones == 7) {
					cout << "Seventeen";
					break;
				} else if (ones == 8) {
					cout << "Eighteen";

					break;
				} else if (ones == 9) {
					cout << "Nineteen";
					break;
				} else
					break;
			}
	}

	if (tens > 1 || number < 10) // if number greater than 19 or number less than 10
	{
		switch (ones) {
			case (9):
				cout << "Nine";
				break;
			case (8):
				cout << "Eight";
				break;
			case (7):
				cout << "Seven";
				break;
			case (6):
				cout << "Six";
				break;
			case (5):
				cout << "Five";
				break;
			case (4):
				cout << "Four";
				break;
			case (3):
				cout << "Three";
				break;
			case (2):
				cout << "Two";
				break;
			case (1):
				cout << "One";
				break;
		}
	}

	cout << endl;
	return 0;
}


so what's the C++ question? Is it clumsy - yes, can it be improved - yes.
One way could be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <array>

constexpr std::array Units { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
constexpr std::array Tens { "zero", "tens", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" };
constexpr std::array Special { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };

int main() {
	int number {};

	do {
		std::cout << "Enter an integer number (between 0 and 1000): ";
		std::cin >> number;
	} while (number < 1 || number >= 1000); // Check for number to be >=1 and < 1000

	const int hundreds { number / 100 };
	const int tens { (number - (hundreds * 100)) / 10 };
	const int ones { number % 10 };

	if (hundreds > 0)
		std::cout << Units[hundreds] << " hundred ";

	if (tens > 1)
		std::cout << Tens[tens] << ' ';
	else if (tens == 1)
		std::cout << Special[ones];

	if (tens != 1 && ones > 0)
		std::cout << Units[ones];

	std::cout << '\n';
}

@srpathak18, the original code was C++, why have C code that doesn't really fulfill the original requirements?

A helpful reminder:
Please learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

Your code, formatted:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<stdio.h>
void main()
{
   int num, i = 0, x, d;
   char* word_no[2000];
   printf("Enter an integer value: \n");
   scanf("%d", &num);
   while (num)
   {
      d = num % 10;
      num = num / 10;
      switch (d)
      {
      case 0: word_no[i++] = "zero";
         break;
      case 1: word_no[i++] = "one";
         break;
      case 2: word_no[i++] = "two";
         break;
      case 3: word_no[i++] = "three";
         break;
      case 4: word_no[i++] = "four";
         break;
      case 5: word_no[i++] = "five";
         break;
      case 6: word_no[i++] = "six";
         break;
      case 7: word_no[i++] = "seven";
         break;
      case 8: word_no[i++] = "eight";
         break;
      case 9: word_no[i++] = "nine";
         break;
      }
   }
   for (x = i - 1; x >= 0; x--)
   {
      printf("%s ", word_no[x]);
   }
}

Let me clarify, the requirement is to parse a number, say 235, into words. "two hundred thirty five".

What you are doing certainly doesn't create a valid C string (hint, hint, no terminating '\0').

void main is non-standard, it should be int. Even if the compiler accepts it, don't use it.
Last edited on
Don't sweat over it George, they're gone the way of all things...

The string literals are null terminated. word_no is an array of char pointers. So for the number 123 then word_no[0] is pointer to "one", word_no[1] is pointer to "two" and word_no[2] is pointer to "three". So that the final loop will print one two three
Visual Studio doesn't like the code, complains mightily about assigning a constant char* to a char* 10 times, ending up in a nice big compile fail of 10 errors.

Code::Blocks' MinGW 8.1.0 complains about the void main as an error; only non-fatally warns about the assignments, it is forbidden to assign a string constant to char*.

But again, this isn't fulfilling the requirements of the assignment.

I ain't "sweating" anything other than it is crap code for different reasons with two compilers.
> only non-fatally warns about the assignments, it is forbidden to assign a string constant to char*.

With g++ (and compatibles), we need to explicitly insist on C++ conformance,
for example with: -std=C++11 -pedantic-errors
http://coliru.stacked-crooked.com/a/d06f435520c732f5
@JLBorges, I understand that, really. :)

The quickie project I used for temp testing the code in C::B didn't have pedantic errors set, the IDE's defaults. I was too lazy to set things properly.

I just find it a bit irritating that MinGW whinges about void main and VS does not.
With -Za (Disable Language Extensions), the Microsoft compiler generates a warning:
warning C4326: return type of 'main' should be 'int' instead of 'void'
It's c code - not C++ code. As c code with VS it compiles OK and runs as expected (with language extensions enabled!).

For C++ code 5, L5 should be:

 
const char* word_no[2000];


as in C++ "one" is of type const char*

and obviously void main() should be int main()

and don't try using Microsoft's Windows headers with -Za. They don't compile!

Last edited on
Visual Studio's default warning level (3) is not set as high as it should when using language extensions, to get a "twitch" from VS about void main the warning level must be set to EnableAllWarnings (/Wall).

I'm not even going to get into "scanf is unsafe" religious debate, something C11 did by adding scanf_s long after MS "enhanced" C I/O.

No matter how ya slice it the code as was originally presented before disappearing into the aether isn't good C, let alone C++. Forget about whether it fulfilled the assignment's requirements, which it didn't.

On a side note VS doesn't like disabling language extensions when the C language standard is set to anything other than the default Legacy MSVC.

"D8016: '/Za' and '/std:c17' command-line options are incompatible." Same whinging with C11.

I think I'll stick to C++ code, I don't need to worry about legacy production code. Being a hobbyist at this.
> as in C++ "one" is of type const char*

In C++, the string literal "one" is of type array of 4 const char ie. const char[4].
It has a static storage duration.

In C, the string literal "one" is of type array of 4 char ie. char[4].
However, an attempt to modify an element of this array engenders undefined behaviour.

Also, in both C++ and C,
A string literal is not necessarily a null-terminated character sequence: if a string literal has embedded null characters, it represents an array which contains more than one (C-style) string.
https://en.cppreference.com/w/cpp/language/string_literal#Notes
This discussion and differing viewpoint is some good stuff to know! :)

Regarding the C format specifiers, I can find what %u is easily. But nothing "pings" when I do a search for %zu. Not even cppreference that I can see has it.*

I know what is used for, (https://stackoverflow.com/questions/2125845/platform-independent-size-t-format-specifiers-in-c), I'd just like to see some definitive documentation beyond a Linux man ref link buried in other pages. https://linux.die.net/man/3/printf

*Well, cppreference does have it, if one knows where to look and what to look for. z is a size modifier *ugh*
Last edited on
cppreference (see table 'The following format specifiers are available:')
Since C++11:
Length Modifier z, Conversion Specifier d ie. %zd : Expected Argument Type signed size_t
Length Modifier z, Conversion Specifier u ie. %zu : Expected Argument Type std::size_t
https://en.cppreference.com/w/cpp/io/c/fprintf
I got there about a minute before ya, JLBorges.

I guess my staff was too long, so I was looking in the wrong place.

https://youtu.be/Pk-B0s0jOwE?t=86
I prefer the Microsoft documentation:

https://docs.microsoft.com/en-us/cpp/c-runtime-library/format-specification-syntax-printf-and-wprintf-functions?view=msvc-170

This also documents the MS specific extensions.

z is also coming in C++23 as a literal suffix with the same 'meaning'.
Last edited on
Registered users can post here. Sign in or register to post.