•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# Rookie's Problem about variable type

variable type

4 replies to this topic

### #1 wangjingzhi8848

wangjingzhi8848

CC Lurker

• Just Joined
• 1 posts

Posted 29 January 2009 - 04:12 PM

I write a cute program that convert centimeter to feet and vice versa.
My problem is that
1.if I input 172cm I will get right answer which is 5feet and 7inches. But if I input 5feet 7inches I can only get 152cm.
2.how can I define "factor" as a fixed number in global using?
3.I don't know how to return to the beginning of the main function
Would any of you could help me with that?
My script is as follow.
```#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace std;

void cm2feet ()
{
int cm ;
int feet ;
int inch ;
double factor = 0.032808;

cout << " Enter the Length in Centimeters: " ;
cin >> cm  ;

feet = (int)(cm*factor) ;
inch = ((double)(cm*factor) - (int)(cm*factor))*12;

cout << "Feet value is :" ;
cout << feet << " feets " << inch << " inches " << endl;

};

void feet2cm ()
{
int feet ;
int inch ;
int cm ;
double factor = 0.032808;

cout << " Enter the Length in feets and inches" ;
cin >> feet;
cin >> inch  ;

cm = (int)((double)(feet + inch/12)/factor) ;

cout << "Cm value is :" ;
cout << cm << " cms "  << endl;

};

int main(int nNumberofArgs, char *pszArgs[])
{
int choice ;
int exitchoice ;
cout << "If you want convert centimeters to feets please press 1" << endl ;
cout << "If you want convert centimeters to feets please press 2" << endl ;
cin >> choice ;

if (choice == 1)
{
cm2feet () ;
}
else if (choice ==2)
{
feet2cm () ;
}

cout << "Do you want to do another conversion? If yes press 1, no press 2" << endl ;
cin >> exitchoice ;

if (exitchoice ==1)
{
cout << "redo";
}
else if (exitchoice ==2)
{
cout << "exit";
}

system("PAUSE");
return EXIT_SUCCESS;
}```

Edited by WingedPanther, 29 January 2009 - 05:39 PM.
add code tags (the # button)

• 0

### #2 Lance

Lance

• 270 posts

Posted 29 January 2009 - 05:37 PM

cm = (int)((double)(feet + inch/12)/factor) ;

should be

cm = (int)((double)(feet + inch/12.)/factor) ;

See that extra dot, it makes 12 a float point number , hence inch/12. will produce expected result. Otherwise inch/12 will always be 0, unless you give a unnormalized inch value, say 5 feet 13 inch.
• 0

### #3 Lance

Lance

• 270 posts

Posted 29 January 2009 - 05:40 PM

```
int main(int nNumberofArgs, char *pszArgs[])

{

int choice ;

int exitchoice ;

redo:

cout << "If you want convert centimeters to feets please press 1" << endl ;

cout << "If you want convert centimeters to feets please press 2" << endl ;

cin >> choice ;

if   (choice == 1)

{

cm2feet () ;

}

else if (choice ==2)

{

feet2cm () ;

}

cout << "Do you want to do another conversion? If yes press 1, no press 2"

<<  endl ;

cin >> exitchoice ;

if (exitchoice ==1)

{

// cout << "redo";

goto redo;

}

else if (exitchoice ==2)

{

//cout << "exit";

// do nothing

}

system("PAUSE");

return EXIT_SUCCESS;

}

```

• 0

### #4 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts
• Location:Upstate, South Carolina
• Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
• Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 29 January 2009 - 05:43 PM

1) inch/12 ==0, because inch is less than 12. Use inch/12.0 instead (which will also remove your need for the cast).
2) place
`const double factor = 0.032808;`
outside all the functions.
3) Use a while loop.
• 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

### #5 Lance

Lance

• 270 posts

Posted 29 January 2009 - 05:44 PM

I heard sb saying "you should not use that ****'d goto", all right, I do it because it represents the least change to your original code. If goto is to be avoid, you can use a while loop or do while loop.

```
do{
prompt_user_to_select_a_conversion();

do_conversion();

exit_choice=choose_exit();
}while(exit_choice != EXIT_CHOSEN );

```

• 0

### Also tagged with one or more of these keywords: variable type

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download