function calls ,functions prototype

so this is my question.>>
Write program that uses a function digit that generates a random number between 100 and 999 and calls a function printWords print it out digit by digit, as a series of words.


and this is the output.>>
Sample run:
The number is 523, in other word five two three



and this is my code!!!
and i need help to complete it and see if the order is right??

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
#include <iostream>

using namespace std;


#include <cstdlib>
#include <ctime>

using namespace std;

int digit()
{
int num;
int count = 1;
unsigned seed = time(0);

srand(seed);

while(count <= 1)
{
num = 100 + rand() % 899;
cout <<"the number is: "<< num <<" ,\t";
count++;
}

return num;
}


char printwords()
{
int n;

n = num/10;
n = n%10;

	if (n <10)
	{
	switch (n)

	{
	case 0:

		cout<< " zero \t'";
			break;
	case 1:

		cout<< " one \t";
		break;

	case 2:

		cout<< " two \t";
		break;

    case 3:

		cout<< " three \t";
		break;

	case 4:

		cout<< " four \t";
		break;

	case 5:

		cout<< " five \t";
		break;


	case 6:

		cout<< " six \t";
		break;

    case 7:

		cout<< " seven \t";
		break;

	case 8:

		cout<< " eight \t";
		break;

	case 9:

		cout<< " nine \t";
		break;
	}
	}
	else
	{
return n=n%10;

	}

return n;

}



int main()

{

	cout << "in other words: " << n;
return 0;
}
Last edited on
i really just need to know where i got it wrong here??

thanks whom tries
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
// Integers only:

#include <iostream>
#include <vector>
#include <map>
#include <string>

using namespace std; 


void printwords(long num)
{
	map<int, string> numbers;
	int i = 0;
	numbers[i++] = "zero";
	numbers[i++] = "one";
	numbers[i++] = "two";
	numbers[i++] = "three";
	numbers[i++] = "four";
	numbers[i++] = "five";
	numbers[i++] = "six";
	numbers[i++] = "seven";
	numbers[i++] = "eight";
	numbers[i] = "nine";

	bool minus = false;
	if (num < 0)
	{
		minus = true;
		num *= -1;
	}

	vector<string> words;
	do
	{
		long mod = num % 10;
		num /= 10;
		words.push_back(numbers[mod]);
	} while (num > 0);
	
	if (minus)
	{
		words.push_back("minus");
	}

	for (vector<string>::const_reverse_iterator crit = words.rbegin(); crit != words.rend(); ++crit)
	{
		cout << *crit << "\t";
	}
	
	cout << endl;
}

int main()
{
	printwords(3745);
	printwords(0);
	printwords(1);
	printwords(-1);
	printwords(-3745);
	printwords(123456789);
	printwords(-123456789);
}
tipaye
i thank yew alot for you work ,, but can`t it be with the use of switch?? as it`s my extra class and we still didn`t get to the level you reached in programing...


i`ll be so thankful for yew if you can explain why not using switch or 2 functions other than main .. and if posiible with my junior way,,, how is it possible to make it right????


ad it has to be random....
Last edited on
:-) I wasn't actually trying to solve it for you, I was hoping you'd look for clues in my program.

Also, the stuff in main is just for testing the function.

You could use your switch for printing out number values (i used map), and , you could first determine the number of digits in the number, then create an array of the same size as that number of digits, then extract each digit starting from the lowest, but storing them in your array, then finally print out all members of the array in reverse order (i used vector)
Without arrays is much clumsier, see if this gives you any clues:

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
void print_num(int x)
{
	// if (!isdigit(x))
	if ((x < 0) || (x > 9))
	{
		cerr << "error in input";
		exit(1);
	}

	switch (x)
	{
		case 0:
			cout << "zero\t";
			break;
			
		case 1:
			cout << "one\t";
			break;
			
		//		...
		
	default:
		break;
	}
}

void eval_num(int num)
{
	int tmp = num;
	int num_digs = 0;
	
	while (tmp != 0)
	{
		tmp /= 10;
		++num_digs;
	}
		
	int div = num_digs - 1;
	while (div > 0)
	{
		tmp = num;
		int mul = 1;
		
		for (int i = 0; i < div; ++i)
		{
			tmp /= 10;
			mul *= 10;
		}

		print_num(tmp);
		num -= tmp * mul;
		div -= 1;
	}
	
	print_num(num);
}
tipaye


thankss~

i`ll post my resulted code later...

:)
so here is the solution code for the questio,, sorry for being late:) thanks to whom helped!!!



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
#include <iostream>

using namespace std;


#include <cstdlib>
#include <ctime>

using namespace std;

int digit()
{
int num;
int count = 1;
unsigned seed = time(0);

srand(seed);

while(count <= 1)
{
num = 100 + rand() % 899;
cout <<"the number is: "<< num <<" ,\t";
count++;
}

return num;
}


void printwords(int num)
{
int n;
int m;
n = num/100;
m = num%100;

	for (int i=1; i <=3 ;i++)
	{
	switch (n)

	{
	case 0:

		cout<< "zero \t'";
			break;
	case 1:

		cout<< "one \t";
		break;

	case 2:

		cout<< "two \t";
		break;

    case 3:

		cout<< "three \t";
		break;

	case 4:

		cout<< "four \t";
		break;

	case 5:

		cout<< "five \t";
		break;


	case 6:

		cout<< "six \t";
		break;

    case 7:

		cout<< " seven \t";
		break;

	case 8:

		cout<< " eight \t";
		break;

	case 9:

		cout<< " nine \t";
		break;
	}

 if (m > 10)
 { n = m /10;
 m%=10;
 }
 else
	 n = m;
	}




}



int main()

{	
	int d = digit(); 
	 
	cout << "in other words: "  ;
	printwords(d);;
return 0;
}



/*
the number is: 882 ,    in other words:  eight   eight  two     Press any key to
 continue
 */
/*
the number is: 996 ,    in other words:  nine    nine   six     Press any key to
 continue
 */
Topic archived. No new replies allowed.