1 Replies - 794 Views - Last Post: 29 July 2009 - 05:32 PM Rate Topic: -----

#1 Surefall   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-July 09

Exception throws

Posted 29 July 2009 - 05:04 PM

Hello, I am having problems with throwing an exception. I have tried it a few ways and am still not able to get a good throw if the user enters a negative interger for either the Foot or Inches
Can someone kindly take a look and tell me what I am doing wrong?

#include <iostream> 


//This program prompts the user to enter
// Feet and Inches and calculates the Cenimeters
// It also checks to make sure the user doesnt
// enter a negative number

using namespace std; 
double inches; // number of inches 
double foot; //number of feet 
double centimeters; // number of centimeters 
double totalinch; //used to calculate total inches
double totalfoot; //used to calcuate total centimeters in foot
int response; // users menu response 

int main() 
{ 
	

	do
	{ 
	  
	   
		cout << " Feet and inches into Centimeters" << endl; 
		cout << endl; 
		cout << "1 - Run Program" << endl; 
		cout << "0 - End Program" << endl; 
		cout << endl; 
		cout << "Enter Selection : "; 

		// get users response 
		cin >> response; 

		if (response == 1) 
		{ 
			// process inches to centimeters 

			cout << "Enter length in feet: "<<endl; 
			cin >> foot; 
			cout<<"Enter inches as well:"<<endl; 
			cin>>inches;
			cout<<endl;
		  
			try
			{ 
				 
				totalinch = inches * 2.54;
				totalfoot = foot * 30.48;
				
				
				

				centimeters = totalfoot+totalinch; 

				if(foot<=0)  
					throw -1; 

			
			}
			catch (int error) 
			{ 
				 
			} 

			cout << "Centimeters in "<<foot<<" foot is "<<totalfoot<<endl;
			cout <<"Centimeters in " <<inches<<" inches is "<<totalinch<<endl;
			cout <<"Total Centimeters is "<<centimeters<<endl; 
			cout <<endl;


		} 

	}while (response !=0);


return 0;

} 



Is This A Good Question/Topic? 0
  • +

Replies To: Exception throws

#2 redhotfire0   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 210
  • Joined: 13-July 09

Re: Exception throws

Posted 29 July 2009 - 05:32 PM

Why throw if you can just loop it back to where it asked for input. If anything, check whether or not it is '<= 0' before going that far.

This post has been edited by redhotfire0: 29 July 2009 - 05:33 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1