Converting Int to string

So far i managed to get up to 99 with my code. i need to get up to 9999 and it should be able to display as a string for example you input 345 it comes out "three hundred forty five"



#include <iostream>
#include <string>
using namespace std;

class Numbers {
private:
int numberToConvert;
public:
Numbers(int i);
string convert();
};
Numbers::Numbers(int i) {
numberToConvert = i;
}
string Numbers::convert() {
string lessThan20[] = {"zero","one","two","three","four","five",
"six","seven","eight","nine","ten","eleven","twelve",
"thirteen","fourteen","fifteen","sixteen","seventeen",
"eighteen","nineteen"};
string tens[] = {"twenty","thirty","forty","fifty","sixty",
"seventy","eighty","ninety"};
string hundreds[] = {"one hundred","two hundred","three hundred","four hundred",
"five hundred","six hundred","seven hundred","eight hundred","nine hundred"};
string thousands[] = {"one thousand","two thousand","three thousand","four thousand"
"five thousand","six thousand","seven thousand","eight thousand","nine thousand"};
string result;

if(numberToConvert > 19) {
result = tens[numberToConvert/10-2] + " ";
numberToConvert = numberToConvert%10;
}
if(numberToConvert != 0) {
result = result + lessThan20[numberToConvert];
}
return result;
}
int main() {
int number;
cout << "Enter a number between 0 and 9999: ";
cin >> number;
Numbers n(number);
cout << n.convert() << endl;
return 0;
}
please use code tags
try using the cstring header file and use strlen() to get how many characters are in the string. then based on how many numbers there are you just have to have to add the terms. i reccomend using a switch statement to get through each individual number
Last edited on
it is required that i do it this way.
You should use an algorithm of sorts, create one.

Anyway here is an idea:

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
#include <stdio.h>

int getnthterm(int num, int n)
{
  int x = 1, y, z = 0;
  while (z < n) {
    y = x;
    x *= 10;
    ++z;
  }
  if (y > num) return -999; /* error */
  return (num % x) / y;
}

int main()
{
  char *b[9][9] = {
    { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" },
    { "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }
  };

  int num, x, y;

  printf("Enter number: ");
  scanf("%d", &num);

  x = 10;
  while (x > 0) {
    y = getnthterm(num, x);
    if (y != 0 && y != -999) {
      if (x == 3) printf ("%s hundred ", b[0][y-1]);
      else
      if (x == 4) printf("%s thousand ", b[0][y-1]);
      else
	printf("%s ", b[x-1][y-1]);
    }
    --x;
  }
  putchar('\n');

  return 0;
}


Yeah, it's in C so u don't copy paste and cheat (it has a bug too). It'll only print upto 9999 but it's better structured has loops.
You should try something similar, in a neat way.

First write it down on a paper, think mathematically. I repeat, think mathematically. First do it on paper.
Good luck.

[reason for edit: typing mistake]
Last edited on
I am not looking for a differnet way to do this. Im trying to see how can I display the hundreds and the thousands like i did with the tens. Using the same code i have there
Topic archived. No new replies allowed.