number to words conversion > called by a function

Dec 5, 2011 at 12:00pm
closed account (1wk4izwU)
hi guys ! :) . I just started using c++ and I can't seem to understand how to use functions :( . My professor revised the problem . He said that we should use a function with argument to get the converted words . How can I do that actually? been trying for two days now !!:(( . help please? :>
#include <iostream>

using namespace std;

int get_number(int ones, int tens, int hundreds, int thousands, int tenthousands);

void main()
{
int num,
char ans;

cout<<" select a code "<<endl;
cout<<"*************CHOICES***************"<<endl;
cout<<"\a [1]Number to Words "<<endl;
cout<<"\a [2]EXIT "<<endl;
cout<<"***********************************"<<endl;
cin>>num;

switch(num)
{
case 1:
int get_number(int ones, int tens, int hundreds, int thousands, int tenthousands);

case 2:

break;


}// end switch

}// end main


int get_number(int one, int ten, int hundreds, int thousands, int tenthousands)
{
int num, ones, tens, hund, thd, tenthd;

cout<<" enter a number between 1-100000"<<endl;
cin>>num;

tenthd=num/10000;
thd=(num/1000)%10;
hund=((num/100)%100)%10;
tens=(num/10)%10;
ones=num%10;

if (num<=1&&num>100000)
cout<<" Invalid Input ";
if(num==0)
cout<<" Zero "<<endl;
if (num==100000)
cout<<" One Hundred Thousand "<<endl;



if (tenthd==1&&thd==0)
cout<<" ten thousand ";
else if (tenthd==2&&thd==0)
cout<<" twenty thousand ";
else if (tenthd==3&&thd==0)
cout<<" thirthy thousand ";
else if (tenthd==4&&thd==0)
cout<<" fourty thousand ";
else if (tenthd==5&&thd==0)
cout<<" fifty thousand ";
else if (tenthd==6&&thd==0)
cout<<" sixty thousand ";
else if (tenthd==7&&thd==0)
cout<<" seventy thousand ";
else if (tenthd==8&&thd==0)
cout<<" eighty thousand ";
else if (tenthd==9&&thd==0)
cout<<" nienty thousand ";
else if (tenthd==2)
cout<<" twenty ";
else if (tenthd==3)
cout<<" thirty ";
else if (tenthd==4)
cout<<" forty ";
else if (tenthd==5)
cout<<" fifty ";
else if (tenthd==6)
cout<<" sixty ";
else if (tenthd==7)
cout<<" seventy ";
else if (tenthd==8)
cout<<" eighty ";
else if (tenthd==9)
cout<<" ninety ";

if (thd==1)
cout<<" one thousand ";
else if (tenthd==1&&thd==1)
cout<<" eleven thousand ";
else if (tenthd==1&&thd==2)
cout<<" twelve thousand ";
else if (tenthd==1&&thd==3)
cout<<" thirtheen thousand ";
else if (tenthd==1&&thd==4)
cout<<" fourteen thousand ";
else if (tenthd==1&&thd==5)
cout<<" fifteen thousand ";
else if (tenthd==1&&thd==6)
cout<<" sixteen thousand ";
else if (tenthd==1&&thd==7)
cout<<" seventeen thousand ";
else if (tenthd==1&&thd==8)
cout<<" eighteen thousand ";
else if (tenthd==1&&thd==9)
cout<<" nineteen thousand ";

if (thd==2&&tenthd!=1)
cout<<" two thousand ";
else if (thd==3&&tenthd!=1)
cout<<" three thousand ";
else if (thd==4&&tenthd!=1)
cout<<" four thousand ";
else if (thd==5&&tenthd!=1)
cout<<" five thousand ";
else if (thd==6&&tenthd!=1)
cout<<" six thousand ";
else if (thd==7&&tenthd!=1)
cout<<" seven thousand ";
else if (thd==8&&tenthd!=1)
cout<<" eight thousand ";
else if (thd==9&&tenthd!=1)
cout<<" nine thousand ";

if (hund==1)
cout<<" one hundred ";
else if (hund==2)
cout<<" two hundred ";
else if (hund==3)
cout<<" three hundred ";
else if (hund==4)
cout<<" four hundred ";
else if (hund==5)
cout<<" five hundred ";
else if (hund==6)
cout<<" six hundred ";
else if (hund==7)
cout<<" seven hundred ";
else if (hund==8)
cout<<" eight hundred ";
else if (hund==9)
cout<<" nine hundred ";


if (tens==1&&ones!=1)
cout<<" ten ";
else if (tens==2&&ones!=1)
cout<<" twenty ";
else if (tens==3&&ones!=1)
cout<<" thirty ";
else if (tens==4&&ones!=1)
cout<<" fourtY ";
else if (tens==5&&ones!=1)
cout<<" fifty ";
else if (tens==6&&ones!=1)
cout<<" sixty ";
else if (tens==7&&ones!=1)
cout<<" seventy ";
else if (tens==8&&ones!=1)
cout<<" eighty ";
else if (tens==9&&ones!=1)
cout<<" ninety ";

if (tens==1&&ones==1)
cout<<" eleven ";
else if (tens==1&&ones==2)
cout<<" twelve ";
else if (tens==1&&ones==3)
cout<<" thirteen ";
else if (tens==1&&ones==4)
cout<<" fourteen ";
else if (tens==1&&ones==5)
cout<<" fifteen ";
else if (tens==1&&ones==6)
cout<<" sixteen ";
else if (tens==1&&ones==7)
cout<<" seventeen ";
else if (tens==1&&ones==8)
cout<<" eighteen ";
else if (tens==1&&ones==9)
cout<<" nineteen ";




cout<<endl;

return num;


}// end int get_number

what should i revise? itsss soo hardd >_< . need help please !
<3 steph
Dec 5, 2011 at 1:24pm
A function is basically a set of commands to execute with specified parameters.
http://www.cplusplus.com/doc/tutorial/functions/
That should answer your questions.
Dec 5, 2011 at 1:36pm
1
2
3
4
5
6
7
switch(num)
{
case 1:
    int get_number(int ones, int tens, int hundreds, int thousands, int tenthousands);
case 2:
    break;
}// end switch 


This is not going to do what you want.
First you need to actually declare the variables "ones, tens, hundreds, thousands, and tenthousands" before you can pass them to a function. Second, the function does not even use these variables. I would get rid of that method since get_number() actually prompts for the user input. Either take the user input out of get_number() and move it in to main, then pass the number to get_number such as get_number(user_input), or just make get_number take no arguments at all. Or you can even make the input and the task seperated.

You will also want to break your case 1 in the switch. I know it does not make a difference in this code, but you had better get in the habit of doing it now or else it will bite you in the bum later. ;)

You should also review you knowledge of how to properly call a function. When you declare the function outside of main, it is correct to have the data types defined before the argument names, but when you call the function you should not include the data types.
e.g.,
1
2
case 1:
    int get_number(int ones, int tens, int hundreds, int thousands, int tenthousands);

should be...
1
2
case 1:
    get_number(ones, tens, hundreds, thousands, tenthousands);


The below changes might get you going in the right direction.

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

int prompt_for_input()
{
    int num;
    cout<<" enter a number between 1-100000"<<endl;
    cin>>num;
    return num;
}

// main()...
int number_to_convert = 0;
switch(num)
{
case 1:
    number_to_convert = prompt_for_input();
    get_number(number_to_convert);
    break;
case 2:
    break;
}// end switch

...

int get_number(int number_to_convert)
{
tenthd=number_to_convert/10000;
thd=(number_to_convert/1000)%10;
hund=((number_to_convert/100)%100)%10;
tens=(number_to_convert/10)%10;
ones=number_to_convert%10;
...
Dec 6, 2011 at 7:19am
closed account (1wk4izwU)
oh my , thank you very much !!! :> . I have also studied about functions last night . This really made me understand how to use those functions ! I might as well read in advance so that I may not look foolish here, sorryyy ! thank you for the link strongdrink . thank you very much bradw :)) . I understood your explanation very well. My professor doesn't explain alll this stuff >_< . anyway, thank you again! :)
Topic archived. No new replies allowed.