Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 12th, 2004, 3:58 PM   #1
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5 TecBrain is on a distinguished road
Here is another that I did, but there is a little bug which won’t take you more than less minute to fix it:

When user selects 0 I want to exit….

Where is my mistake?


#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
 
 int option,number;
 double result;
 // menu option
 cout << "Program to convert from Inches, feet and yards to meters" <<endl;
 cout << "--------------------------------------------------------" <<endl; 
 cout << "Enter 1 to convert from Inches to meters" <<endl;
 cout << "----------------------------------------" <<endl;
 cout << "Enter 2 to convert from feet to meters" <<endl;
 cout << "----------------------------------------" <<endl;
 cout << "Enter 3 to convert from inch to meters" <<endl;
 cout << "----------------------------------------" <<endl;
 cout << "Enter 0 to exit" << endl;
 cout << "****************************************" <<endl;
 cin >> option;
 
 //end here
 
 // select kind of calc
 cout << "Enter the length" <<endl;
 cout << "----------------" <<endl;
  cin >> number;
 
 //start calc here
 
 switch (option)
 {
 case 0:
 return (0);
 case 1:
 result = ( number * 0.9144 );
 break;
 case 2:
 result = ( number * 0.3048 );
 break;
 case 3:
 result = ( number * 0.0254 );
 break;
 }
 cout << "The result of the converstion is: " << result <<endl;
  
 system("PAUSE");	
 return 0;
__________________
Personal Portfolio
TecBrain Support Forum
Linux VS Windows ... Dont Even Think of it ..
Distribution: Slackware
if (OS==Linux) return success
There are 10 kinds of people, those who can read binary numbers and those who can't.
TecBrain is offline   Reply With Quote
Old Oct 12th, 2004, 4:05 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
You need to put the switch before you input number.

Edit: Actually, that would cause a problem, because you would be performing all the calculations before you enter number. Instead of testing for 0 in the switch, take it out and put an if statement before you enter number to test for 0. Leave the switch statement where you originally had it.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Oct 12th, 2004, 4:07 PM   #3
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5 TecBrain is on a distinguished road
Well everything works correctly except the case 0 part.
__________________
Personal Portfolio
TecBrain Support Forum
Linux VS Windows ... Dont Even Think of it ..
Distribution: Slackware
if (OS==Linux) return success
There are 10 kinds of people, those who can read binary numbers and those who can't.
TecBrain is offline   Reply With Quote
Old Oct 12th, 2004, 4:09 PM   #4
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Check the edit. That should do it.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Oct 12th, 2004, 4:12 PM   #5
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5 TecBrain is on a distinguished road
Perfect.


#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
 
 int option,number;
 double result;
 // menu option
 cout << "Program to convert from Inches, feet and yards to meters" <<endl;
 cout << "--------------------------------------------------------" <<endl; 
 cout << "Enter 1 to convert from Inches to meters" <<endl;
 cout << "----------------------------------------" <<endl;
 cout << "Enter 2 to convert from feet to meters" <<endl;
 cout << "----------------------------------------" <<endl;
 cout << "Enter 3 to convert from inch to meters" <<endl;
 cout << "----------------------------------------" <<endl;
 cout << "Enter 0 to exit" << endl;
 cout << "****************************************" <<endl;
 cin >> option;
 
 //end here
 
 // select kind of calc
 cout << "Enter the length" <<endl;
 cout << "----------------" <<endl;
 if (option == 0 )return (0);
 cin >> number;
 
 //start calc here
 
 switch (option)
 {
 case 1:
 result = ( number * 0.9144 );
 break;
 case 2:
 result = ( number * 0.3048 );
 break;
 case 3:
 result = ( number * 0.0254 );
 break;
 }
 cout << "The result of the converstion is: " << result <<endl;
  
 system("PAUSE");	
 return 0;
}


Thanks....
__________________
Personal Portfolio
TecBrain Support Forum
Linux VS Windows ... Dont Even Think of it ..
Distribution: Slackware
if (OS==Linux) return success
There are 10 kinds of people, those who can read binary numbers and those who can't.
TecBrain is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:52 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC