A program that converts numbers into words

I'm writing a program that will convert any number less than 10 million into words. It divides the numbers into substrings of hundreds as the first step i.e. (string s=2450769 into s1=2, s2=450, s3=769). I've written my code to do this step below. I get an error on line 35:

ERROR
worderator.cpp: In function ‘void centize(std::string, std::string*, std::string*, std::string*, std::string*)’:
worderator.cpp:42: error: cannot convert ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >’ to ‘std::string*’ in assignment
ubuntu@ubuntu-laptop:~/Desktop$


CODE
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
#include <iostream>
#include <string>
using namespace std;

int getNumber();
void centize(string, string*, string*,string*,string*);
char* itoa(int, int);

int main()
{
	string s_number=itoa(getNumber(),10);
	string* cent4 = new string;
	string* cent3 = new string;
	string* cent2 = new string;
	string* cent1 = new string;

	centize(s_number, cent4, cent3, cent2, cent1); //break down into sets of hundreds */
	return 0;	
}

int getNumber()
{
//code
}

void centize(string sNum, string* c4, string* c3, string* c2, string* c1)
{
	//starting fromt the last digit, substring three digits and pop them into a cent string.

	int length = sNum.length();
	cout<<length<<endl;

	c1 = sNum.substr((length-3),length);
	cout<<c1<<endl;

}


Thanks.
Last edited on
c1 is a pointer right? So the error is that your trying to give a pointer the value of a string. Just change c1 to *c1 (the dereference operator) and that should fix the problem.
In addition, you'll also need to change the next statement into
 
cout << *c1 << endl;
why pointer but not just pass by reference?
Because when you say "string *c1" you are declaring a pointer named c1, that points to the address of a string object. To pass by reference in C++ you can use either pointers or pass by reference (with the use of the & operator)
.
If you use pointers you need to dereference them each time you want the value stored at the memory location stored by the pointer.

The way I'm sure your trying to do is send something so it is by reference. Unfortunately, in your call, you send pointers so on the receiving end you will need pointers.

But luckily pointers and pass by reference will work the way you want. Just by using the dereference operator you change the value just like if c1 was a variable. When the function ends the data now stored at c1 will remained changed just like pass by reference. In fact, pass by reference is just like working with pointers under the surface.
Thanks. I get where I went wrong but then realized the program was more complicated than it need be. This is the revised program - in which i append the number (one, two, thirteen e.t.c) and the collective (hundred, thousand, hundred thousand) to the final string.

The function wordedNumber() returns a string containing the name of the number in words and it stores this number into the variable append.

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
#include <iostream>
#include <string>
using namespace std;

int getNumber();
string wordedNumber(char,char,int );
char* itoa(int, int);

int main()
{
	string Number=itoa(getNumber(),10);
	string inWords;
	int length = Number.length();
	cout<<"length: "<<length<<endl;
	for(int i=0;i<length;i++)
	{	
		string append;
		int pos=length - i;
		cout<<"At Position="<<pos<<", s="<<Number[i]<<" and next="<<Number[i+1]<<"."<<endl;
		append = wordedNumber(Number[i],Number[i+1],pos);
		cout<<append<<endl;
	}
}

int getNumber()
{
//returns number
}

string wordedNumber(char s,char next, int p)
{
	string word;
	if(s=='1' && p!='2' && p!='5'){word = "one"; return word;}
	if(s=='1' && (p=='2' || p=='5'))
	{

		switch(next)
		{
		case 0:{word = "ten"; return word; break;}
		case 1:{word = "eleven"; return word; break;}
		case 2:{word = "twelve"; return word; break;}
		case 3:{word = "thirteen"; return word; break;}
		case 4:{word = "fourteen"; return word; break;}

		case 5:{word = "fifteen"; return word; break;}
		case 6:{word = "sixteen"; return word; break;}
		case 7:{word = "seventeen"; return word; break;}
		case 8:{word = "eightteen"; return word; break;}
		case 9:{word = "ninteen"; return word; break;}
		}
	}
}


when i input 12, the output is still one. I've looked at the conditions and I can't find a mistake. Why doesn't it pick out twelve from the cases?
You're comparing a position with a character, which makes no sense.
And you don't need the temporary string. You can just write return "one"; etc.
You're comparing a position with a character, which makes no sense.

I'm sorry, I'm not quite sure what you mean.
I mean this part: && p!='2' && p!='5' and again in the line below.
Oh right right right! Thanks Alot!
Topic archived. No new replies allowed.